Skip to content

Commit 5d1b992

Browse files
authored
Feat: Add Psalm (#20)
1 parent 93b2647 commit 5d1b992

File tree

9 files changed

+128
-39
lines changed

9 files changed

+128
-39
lines changed

.github/workflows/static-analysis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,27 @@ jobs:
3232

3333
- name: "Run a static analysis with phpstan/phpstan"
3434
run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"
35+
36+
static-analysis-psalm:
37+
name: "Static Analysis with Psalm"
38+
runs-on: "ubuntu-20.04"
39+
40+
strategy:
41+
matrix:
42+
php-version:
43+
- "7.3"
44+
45+
steps:
46+
- name: Checkout code
47+
uses: actions/checkout@v2
48+
49+
- name: Psalm
50+
uses: docker://vimeo/psalm-github-actions:4.4.1
51+
with:
52+
composer_require_dev: true
53+
security_analysis: true
54+
report_file: results.sarif
55+
- name: Upload Security Analysis results to GitHub
56+
uses: github/codeql-action/upload-sarif@v1
57+
with:
58+
sarif_file: results.sarif

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/composer.lock
44
/phpcs.xml
55
/phpstan.neon
6+
/psalm.xml
67
/phpunit.xml
78
/vendor/

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[![Latest Stable Version](https://poser.pugx.org/bentools/iterable-functions/v/stable)](https://packagist.org/packages/bentools/iterable-functions)
22
[![GitHub Actions][GA master image]][GA master]
33
[![Code Coverage][Coverage image]][CodeCov Master]
4+
[![Shepherd Type][Shepherd Image]][Shepherd Link]
45
[![Total Downloads](https://poser.pugx.org/bentools/iterable-functions/downloads)](https://packagist.org/packages/bentools/iterable-functions)
56

67
Iterable functions
@@ -182,7 +183,14 @@ Unit tests
182183
php vendor/bin/pest
183184
```
184185

185-
[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev
186-
[Coverage image]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev/graph/badge.svg
187186
[GA master]: https://github.com/bpolaszek/php-iterable-functions/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A2.0.x-dev
187+
188188
[GA master image]: https://github.com/bpolaszek/php-iterable-functions/workflows/Continuous%20Integration/badge.svg
189+
190+
[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev
191+
192+
[Coverage image]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev/graph/badge.svg
193+
194+
[Shepherd Image]: https://shepherd.dev/github/bpolaszek/php-iterable-functions/coverage.svg
195+
196+
[Shepherd Link]: https://shepherd.dev/github/bpolaszek/php-iterable-functions

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"phpstan/extension-installer": "^1.1",
2727
"phpstan/phpstan": "^0.12.67",
2828
"phpstan/phpstan-strict-rules": "^0.12.9",
29-
"symfony/var-dumper": "^5.2"
29+
"symfony/var-dumper": "^5.2",
30+
"vimeo/psalm": "^4.4"
3031
},
3132
"config": {
3233
"sort-packages": true

psalm.xml.dist

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns="https://getpsalm.org/schema/config"
5+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
6+
>
7+
<projectFiles>
8+
<directory name="src" />
9+
<directory name="tests" />
10+
<ignoreFiles>
11+
<directory name="vendor" />
12+
</ignoreFiles>
13+
</projectFiles>
14+
15+
<issueHandlers>
16+
<InternalMethod>
17+
<errorLevel type="suppress">
18+
<directory name="tests" />
19+
</errorLevel>
20+
</InternalMethod>
21+
<PossiblyUndefinedMethod>
22+
<errorLevel type="suppress">
23+
<directory name="tests" />
24+
</errorLevel>
25+
</PossiblyUndefinedMethod>
26+
</issueHandlers>
27+
</psalm>

src/iterable-functions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ 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): bool {
77-
return (bool) $value;
78-
};
76+
$filter =
77+
/** @param mixed $value */
78+
static function ($value): bool {
79+
return (bool) $value;
80+
};
7981
}
8082

8183
if ($iterable instanceof Traversable) {

tests/IterableFilterTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@
2323

2424
it('filters an array with a callback', function (): void {
2525
$iterable = ['foo', 'bar'];
26-
$filter = static function ($input): bool {
26+
$filter =
27+
/** @param mixed $input */
28+
static function ($input): bool {
2729
return $input === 'bar';
2830
};
2931
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
3032
});
3133

3234
it('filters a Travsersable object with a callback', function (): void {
3335
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
34-
$filter = static function ($input): bool {
36+
$filter =
37+
/** @param mixed $input */
38+
static function ($input): bool {
3539
return $input === 'bar';
3640
};
3741
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));

tests/IterableObjectTest.php

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
$dataProvider = static function (): Generator {
1919
$data = ['foo', 'bar'];
20-
$filter = static function ($value): bool {
21-
return $value === 'bar';
22-
};
20+
$filter =
21+
/** @param mixed $value */
22+
static function ($value): bool {
23+
return $value === 'bar';
24+
};
2325
$map = 'strtoupper';
2426

2527
yield from [
@@ -50,32 +52,50 @@
5052
];
5153
};
5254

53-
test('input: array | output: traversable', function ($data, $filter, $map, $expectedResult): void {
54-
$iterableObject = iterable($data, $filter, $map);
55-
assertEquals($expectedResult, iterator_to_array($iterableObject));
56-
})->with($dataProvider());
55+
test(
56+
'input: array | output: traversable',
57+
/** @param array<int, mixed> $data */
58+
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
59+
$iterableObject = iterable($data, $filter, $map);
60+
assertEquals($expectedResult, iterator_to_array($iterableObject));
61+
}
62+
)->with($dataProvider());
5763

58-
test('input: array | output: array', function ($data, $filter, $map, $expectedResult): void {
59-
$iterableObject = iterable($data, $filter, $map);
60-
assertEquals($expectedResult, $iterableObject->asArray());
61-
})->with($dataProvider());
64+
test(
65+
'input: array | output: array',
66+
/** @param array<int, mixed> $data */
67+
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
68+
$iterableObject = iterable($data, $filter, $map);
69+
assertEquals($expectedResult, $iterableObject->asArray());
70+
}
71+
)->with($dataProvider());
6272

63-
test('input: traversable | output: traversable', function ($data, $filter, $map, $expectedResult): void {
64-
$data = SplFixedArray::fromArray($data);
65-
$iterableObject = iterable($data, $filter, $map);
66-
assertEquals($expectedResult, iterator_to_array($iterableObject));
67-
})->with($dataProvider());
73+
test(
74+
'input: traversable | output: traversable',
75+
/** @param array<int, mixed> $data */
76+
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
77+
$data = SplFixedArray::fromArray($data);
78+
$iterableObject = iterable($data, $filter, $map);
79+
assertEquals($expectedResult, iterator_to_array($iterableObject));
80+
}
81+
)->with($dataProvider());
6882

69-
test('input: traversable | output: array', function ($data, $filter, $map, $expectedResult): void {
70-
$data = SplFixedArray::fromArray($data);
71-
$iterableObject = iterable($data, $filter, $map);
72-
assertEquals($expectedResult, $iterableObject->asArray());
73-
})->with($dataProvider());
83+
test(
84+
'input: traversable | output: array',
85+
/** @param array<int, mixed> $data */
86+
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
87+
$data = SplFixedArray::fromArray($data);
88+
$iterableObject = iterable($data, $filter, $map);
89+
assertEquals($expectedResult, $iterableObject->asArray());
90+
}
91+
)->with($dataProvider());
7492

7593
it('filters the subject', function (): void {
76-
$filter = static function ($value): bool {
77-
return $value === 'bar';
78-
};
94+
$filter =
95+
/** @param mixed $value */
96+
static function ($value): bool {
97+
return $value === 'bar';
98+
};
7999
$iterableObject = iterable(['foo', 'bar'])->filter($filter);
80100
assertEquals([1 => 'bar'], iterator_to_array($iterableObject));
81101
});
@@ -88,9 +108,11 @@
88108
});
89109

90110
it('combines filter and map', function (): void {
91-
$filter = static function ($value): bool {
92-
return $value === 'bar';
93-
};
111+
$filter =
112+
/** @param mixed $value */
113+
static function ($value): bool {
114+
return $value === 'bar';
115+
};
94116
$map = 'strtoupper';
95117
$iterableObject = iterable(['foo', 'bar'])->map($map)->filter($filter);
96118
assertInstanceOf(IterableObject::class, $iterableObject);

tests/IterableReduceTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212

1313
it('reduces an array', function (): void {
1414
$iterable = [1, 2];
15-
$reduce = static function ($carry, $item) {
16-
return $carry + $item;
15+
$reduce = static function (?int $carry, int $item): int {
16+
return (int) $carry + $item;
1717
};
1818
assertSame(3, iterable_reduce($iterable, $reduce, 0));
1919
});
2020

2121
it('reduces an traversable', function (): void {
2222
$iterable = SplFixedArray::fromArray([1, 2]);
23-
$reduce = static function ($carry, $item) {
24-
return $carry + $item;
23+
$reduce = static function (?int $carry, int $item): int {
24+
return (int) $carry + $item;
2525
};
2626
assertSame(3, iterable_reduce($iterable, $reduce, 0));
2727
});

0 commit comments

Comments
 (0)