Skip to content

Commit 3a6064d

Browse files
authored
Merge pull request #17 from adhocore/16-modernize-plus-github-action
2 parents 40f070f + 4d4c912 commit 3a6064d

17 files changed

+213
-370
lines changed

.github/workflows/build.yml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
defaults:
8+
run:
9+
shell: bash
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13+
cancel-in-progress: true
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
20+
tests:
21+
name: Tests
22+
env:
23+
extensions: pcov
24+
25+
strategy:
26+
matrix:
27+
include:
28+
- php: '8.0'
29+
- php: '8.1'
30+
- php: '8.2'
31+
fail-fast: true
32+
33+
runs-on: ubuntu-20.04
34+
35+
steps:
36+
- name: Checkout
37+
uses: actions/checkout@v2
38+
with:
39+
fetch-depth: 2
40+
41+
- name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
coverage: "none"
45+
ini-values: date.timezone=Asia/Bangkok,memory_limit=-1,default_socket_timeout=10,session.gc_probability=0,zend.assertions=1
46+
php-version: "${{ matrix.php }}"
47+
extensions: "${{ env.extensions }}"
48+
tools: flex
49+
50+
- name: Before run
51+
run: |
52+
echo COLUMNS=120 >> $GITHUB_ENV
53+
for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \;; done
54+
55+
- name: Install dependencies
56+
run: composer install --no-progress --ansi -o
57+
58+
- name: Run tests
59+
run: composer test:cov
60+
61+
- name: Codecov
62+
run: bash <(curl -s https://codecov.io/bash)

.travis.yml

-21
This file was deleted.

composer.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
}
2323
},
2424
"require": {
25-
"php": ">=5.4.0"
25+
"php": ">=8.0"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^4.8.0|^5.7.0|^6.5"
28+
"phpunit/phpunit": "^9.5"
2929
},
3030
"config": {
3131
"optimize-autoloader": true,
@@ -34,8 +34,7 @@
3434
}
3535
},
3636
"scripts": {
37-
"post-root-package-install": [
38-
],
39-
"test": "vendor/bin/phpunit"
37+
"test": "vendor/bin/phpunit",
38+
"test:cov": "phpunit --coverage-text --coverage-clover coverage.xml"
4039
}
4140
}

phpunit.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false"
11-
syntaxCheck="false"
1211
bootstrap="tests/bootstrap.php"
1312
>
1413
<testsuites>

src/Arrayizes.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait Arrayizes
2121
*
2222
* @return array
2323
*/
24-
public function asArray($data, $cast = true)
24+
public function asArray(mixed $data, bool $cast = true): array
2525
{
2626
if (\is_array($data)) {
2727
return $data;
@@ -41,7 +41,7 @@ public function asArray($data, $cast = true)
4141
return $data->jsonSerialize();
4242
}
4343

44-
if (\method_exists($data, 'toArray')) {
44+
if (\is_object($data) && \method_exists($data, 'toArray')) {
4545
return $data->toArray();
4646
}
4747

@@ -50,17 +50,12 @@ public function asArray($data, $cast = true)
5050

5151
/**
5252
* Convert the data items to array.
53-
*
54-
* @return array
5553
*/
56-
public function toArray()
54+
public function toArray(): array
5755
{
58-
return \array_map(function ($value) {
59-
if (\is_scalar($value)) {
60-
return $value;
61-
}
62-
63-
return $this->asArray($value, false);
64-
}, $this->getData());
56+
return \array_map(
57+
fn ($value) => \is_scalar($value) ? $value : $this->asArray($value, false),
58+
$this->getData()
59+
);
6560
}
6661
}

src/HigherOrderMessage.php

+5-16
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,17 @@
1818
*/
1919
class HigherOrderMessage
2020
{
21-
protected $underscore;
22-
protected $method;
23-
24-
public function __construct(UnderscoreBase $underscore, $method)
21+
public function __construct(protected UnderscoreBase $underscore, protected string $method)
2522
{
26-
$this->underscore = $underscore;
27-
$this->method = $method;
2823
}
2924

30-
public function __call($method, $args)
25+
public function __call(string $method, array $args): mixed
3126
{
32-
return $this->underscore->{$this->method}(function ($item) use ($method, $args) {
33-
return \call_user_func_array([$item, $method], $args);
34-
});
27+
return $this->underscore->{$this->method}(static fn ($item) => \call_user_func_array([$item, $method], $args));
3528
}
3629

37-
public function __get($prop)
30+
public function __get($prop): mixed
3831
{
39-
return $this->underscore->{$this->method}(function ($item) use ($prop) {
40-
$props = \array_column([$item], $prop);
41-
42-
return empty($props) ? null : $props[0];
43-
});
32+
return $this->underscore->{$this->method}(static fn ($item) => \array_column([$item], $prop)[0] ?? null);
4433
}
4534
}

src/Underscore.php

+8-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class Underscore extends UnderscoreFunction
1818
*
1919
* @param array|mixed $data
2020
*/
21-
public function __construct($data = [])
21+
public function __construct(mixed $data = [])
2222
{
2323
parent::__construct($data);
2424
}
@@ -30,44 +30,31 @@ public function __construct($data = [])
3030
*
3131
* @return self
3232
*/
33-
public static function _($data = null)
33+
public static function _($data = null): self
3434
{
3535
return new static($data);
3636
}
3737

3838
/**
3939
* Generates a function that always returns a constant value.
40-
*
41-
* @param mixed $value
42-
*
43-
* @return callable
4440
*/
45-
public function constant($value)
41+
public function constant(mixed $value): callable
4642
{
47-
return function () use ($value) {
48-
return $value;
49-
};
43+
return fn () => $value;
5044
}
5145

5246
/**
5347
* No operation!
54-
*
55-
* @return void
5648
*/
57-
public function noop()
49+
public function noop(): void
5850
{
5951
// ;)
6052
}
6153

6254
/**
6355
* Run callable n times and create new collection.
64-
*
65-
* @param int $n
66-
* @param callable $fn
67-
*
68-
* @return self
6956
*/
70-
public function times($n, callable $fn)
57+
public function times(int $n, callable $fn): self
7158
{
7259
$data = [];
7360

@@ -80,25 +67,16 @@ public function times($n, callable $fn)
8067

8168
/**
8269
* Return a random integer between min and max (inclusive).
83-
*
84-
* @param int $min
85-
* @param int $max
86-
*
87-
* @return int
8870
*/
89-
public function random($min, $max)
71+
public function random(int $min, int $max): int
9072
{
9173
return \mt_rand($min, $max);
9274
}
9375

9476
/**
9577
* Generate unique ID (unique for current go/session).
96-
*
97-
* @param string $prefix
98-
*
99-
* @return string
10078
*/
101-
public function uniqueId($prefix = '')
79+
public function uniqueId(string $prefix = ''): string
10280
{
10381
static $id = 0;
10482

0 commit comments

Comments
 (0)