Skip to content

Commit dcabd1a

Browse files
authored
Feat: Add PHPStan (#19)
1 parent 1162238 commit dcabd1a

File tree

8 files changed

+60
-13
lines changed

8 files changed

+60
-13
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: "Static Analysis"
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- "master"
8+
9+
jobs:
10+
static-analysis-phpstan:
11+
name: "Static Analysis with PHPStan"
12+
runs-on: "ubuntu-20.04"
13+
14+
strategy:
15+
matrix:
16+
php-version:
17+
- "7.3"
18+
19+
steps:
20+
- name: "Checkout code"
21+
uses: "actions/checkout@v2"
22+
23+
- name: "Install PHP"
24+
uses: "shivammathur/setup-php@v2"
25+
with:
26+
coverage: "none"
27+
php-version: "${{ matrix.php-version }}"
28+
tools: "cs2pr"
29+
30+
- name: "Install dependencies with Composer"
31+
uses: "ramsey/composer-install@v1"
32+
33+
- name: "Run a static analysis with phpstan/phpstan"
34+
run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/.phpcs-cache
2+
/.phpunit.result.cache
23
/composer.lock
34
/phpcs.xml
5+
/phpstan.neon
46
/phpunit.xml
57
/vendor/

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
"require-dev": {
2424
"doctrine/coding-standard": "^8.2",
2525
"pestphp/pest": "^1.0",
26+
"phpstan/extension-installer": "^1.1",
27+
"phpstan/phpstan": "^0.12.67",
28+
"phpstan/phpstan-strict-rules": "^0.12.9",
2629
"symfony/var-dumper": "^5.2"
2730
},
2831
"config": {

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- %currentWorkingDirectory%/src
5+
- %currentWorkingDirectory%/tests

src/IterableObject.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010

1111
/**
1212
* @internal
13+
*
14+
* @implements IteratorAggregate<mixed>
1315
*/
1416
final class IterableObject implements IteratorAggregate
1517
{
1618
/** @var iterable<mixed> */
1719
private $iterable;
1820

19-
/** @var callable */
21+
/** @var callable|null */
2022
private $filterFn;
2123

22-
/** @var callable */
24+
/** @var callable|null */
2325
private $mapFn;
2426

2527
/**

src/iterable-functions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function iterable_to_traversable(iterable $iterable): Traversable
7373
function iterable_filter(iterable $iterable, ?callable $filter = null)
7474
{
7575
if ($filter === null) {
76-
$filter = static function ($value) {
76+
$filter = static function ($value): bool {
7777
return (bool) $value;
7878
};
7979
}
@@ -89,14 +89,14 @@ function iterable_filter(iterable $iterable, ?callable $filter = null)
8989
* Reduces an iterable.
9090
*
9191
* @param iterable<mixed> $iterable
92-
* @param callable(mixed, mixed) $reduce
92+
* @param callable(mixed, mixed):mixed $reduce
9393
*
9494
* @return mixed
9595
*
9696
* @psalm-template TValue
97-
* @psalm-template TResult
97+
* @template TResult
9898
* @psalm-param iterable<TValue> $iterable
99-
* @psalm-param callable(TResult|null, TValue) $reduce
99+
* @psalm-param callable(TResult|null, TValue):TResult $reduce
100100
* @psalm-param TResult|null $initial
101101
* @psalm-return TResult|null
102102
*/

tests/IterableFilterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
1717
});
1818

19-
it('filters a Travsersable object', function (): void {
19+
it('filters a Traversable object', function (): void {
2020
$iterable = SplFixedArray::fromArray([false, true]);
2121
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
2222
});
2323

2424
it('filters an array with a callback', function (): void {
2525
$iterable = ['foo', 'bar'];
26-
$filter = static function ($input) {
26+
$filter = static function ($input): bool {
2727
return $input === 'bar';
2828
};
2929
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
3030
});
3131

3232
it('filters a Travsersable object with a callback', function (): void {
3333
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
34-
$filter = static function ($input) {
34+
$filter = static function ($input): bool {
3535
return $input === 'bar';
3636
};
3737
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));

tests/IterableObjectTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace BenTools\IterableFunctions\Tests;
66

77
use BenTools\IterableFunctions\IterableObject;
8+
use Generator;
89
use SplFixedArray;
910

1011
use function BenTools\IterableFunctions\iterable;
@@ -14,9 +15,9 @@
1415
use function PHPUnit\Framework\assertInstanceOf;
1516
use function test;
1617

17-
$dataProvider = function () {
18+
$dataProvider = static function (): Generator {
1819
$data = ['foo', 'bar'];
19-
$filter = static function ($value) {
20+
$filter = static function ($value): bool {
2021
return $value === 'bar';
2122
};
2223
$map = 'strtoupper';
@@ -72,7 +73,7 @@
7273
})->with($dataProvider());
7374

7475
it('filters the subject', function (): void {
75-
$filter = static function ($value) {
76+
$filter = static function ($value): bool {
7677
return $value === 'bar';
7778
};
7879
$iterableObject = iterable(['foo', 'bar'])->filter($filter);
@@ -87,7 +88,7 @@
8788
});
8889

8990
it('combines filter and map', function (): void {
90-
$filter = static function ($value) {
91+
$filter = static function ($value): bool {
9192
return $value === 'bar';
9293
};
9394
$map = 'strtoupper';

0 commit comments

Comments
 (0)