Skip to content

Add Psalm #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,27 @@ jobs:

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"

static-analysis-psalm:
name: "Static Analysis with Psalm"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.3"

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Psalm
uses: docker://vimeo/psalm-github-actions:4.4.1
with:
composer_require_dev: true
security_analysis: true
report_file: results.sarif
- name: Upload Security Analysis results to GitHub
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: results.sarif
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/composer.lock
/phpcs.xml
/phpstan.neon
/psalm.xml
/phpunit.xml
/vendor/
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Latest Stable Version](https://poser.pugx.org/bentools/iterable-functions/v/stable)](https://packagist.org/packages/bentools/iterable-functions)
[![GitHub Actions][GA master image]][GA master]
[![Code Coverage][Coverage image]][CodeCov Master]
[![Shepherd Type][Shepherd Image]][Shepherd Link]
[![Total Downloads](https://poser.pugx.org/bentools/iterable-functions/downloads)](https://packagist.org/packages/bentools/iterable-functions)

Iterable functions
Expand Down Expand Up @@ -177,7 +178,14 @@ Unit tests
php vendor/bin/pest
```

[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev
[Coverage image]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev/graph/badge.svg
[GA master]: https://github.com/bpolaszek/php-iterable-functions/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A2.0.x-dev

[GA master image]: https://github.com/bpolaszek/php-iterable-functions/workflows/Continuous%20Integration/badge.svg

[CodeCov Master]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev

[Coverage image]: https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev/graph/badge.svg

[Shepherd Image]: https://shepherd.dev/github/bpolaszek/php-iterable-functions/coverage.svg

[Shepherd Link]: https://shepherd.dev/github/bpolaszek/php-iterable-functions
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^0.12.67",
"phpstan/phpstan-strict-rules": "^0.12.9",
"symfony/var-dumper": "^5.2"
"symfony/var-dumper": "^5.2",
"vimeo/psalm": "^4.4"
},
"config": {
"sort-packages": true
Expand Down
27 changes: 27 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<psalm
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<InternalMethod>
<errorLevel type="suppress">
<directory name="tests" />
</errorLevel>
</InternalMethod>
<PossiblyUndefinedMethod>
<errorLevel type="suppress">
<directory name="tests" />
</errorLevel>
</PossiblyUndefinedMethod>
</issueHandlers>
</psalm>
8 changes: 5 additions & 3 deletions src/iterable-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ function iterable_to_traversable(iterable $iterable): Traversable
function iterable_filter(iterable $iterable, ?callable $filter = null)
{
if ($filter === null) {
$filter = static function ($value): bool {
return (bool) $value;
};
$filter =
/** @param mixed $value */
static function ($value): bool {
return (bool) $value;
};
}

if ($iterable instanceof Traversable) {
Expand Down
8 changes: 6 additions & 2 deletions tests/IterableFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@

it('filters an array with a callback', function (): void {
$iterable = ['foo', 'bar'];
$filter = static function ($input): bool {
$filter =
/** @param mixed $input */
static function ($input): bool {
return $input === 'bar';
};
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
});

it('filters a Travsersable object with a callback', function (): void {
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
$filter = static function ($input): bool {
$filter =
/** @param mixed $input */
static function ($input): bool {
return $input === 'bar';
};
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
Expand Down
76 changes: 49 additions & 27 deletions tests/IterableObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

$dataProvider = static function (): Generator {
$data = ['foo', 'bar'];
$filter = static function ($value): bool {
return $value === 'bar';
};
$filter =
/** @param mixed $value */
static function ($value): bool {
return $value === 'bar';
};
$map = 'strtoupper';

yield from [
Expand Down Expand Up @@ -50,32 +52,50 @@
];
};

test('input: array | output: traversable', function ($data, $filter, $map, $expectedResult): void {
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, iterator_to_array($iterableObject));
})->with($dataProvider());
test(
'input: array | output: traversable',
/** @param array<int, mixed> $data */
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, iterator_to_array($iterableObject));
}
)->with($dataProvider());

test('input: array | output: array', function ($data, $filter, $map, $expectedResult): void {
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, $iterableObject->asArray());
})->with($dataProvider());
test(
'input: array | output: array',
/** @param array<int, mixed> $data */
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, $iterableObject->asArray());
}
)->with($dataProvider());

test('input: traversable | output: traversable', function ($data, $filter, $map, $expectedResult): void {
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, iterator_to_array($iterableObject));
})->with($dataProvider());
test(
'input: traversable | output: traversable',
/** @param array<int, mixed> $data */
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, iterator_to_array($iterableObject));
}
)->with($dataProvider());

test('input: traversable | output: array', function ($data, $filter, $map, $expectedResult): void {
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, $iterableObject->asArray());
})->with($dataProvider());
test(
'input: traversable | output: array',
/** @param array<int, mixed> $data */
function (array $data, ?callable $filter, ?callable $map, array $expectedResult): void {
$data = SplFixedArray::fromArray($data);
$iterableObject = iterable($data, $filter, $map);
assertEquals($expectedResult, $iterableObject->asArray());
}
)->with($dataProvider());

it('filters the subject', function (): void {
$filter = static function ($value): bool {
return $value === 'bar';
};
$filter =
/** @param mixed $value */
static function ($value): bool {
return $value === 'bar';
};
$iterableObject = iterable(['foo', 'bar'])->filter($filter);
assertEquals([1 => 'bar'], iterator_to_array($iterableObject));
});
Expand All @@ -88,9 +108,11 @@
});

it('combines filter and map', function (): void {
$filter = static function ($value): bool {
return $value === 'bar';
};
$filter =
/** @param mixed $value */
static function ($value): bool {
return $value === 'bar';
};
$map = 'strtoupper';
$iterableObject = iterable(['foo', 'bar'])->map($map)->filter($filter);
assertInstanceOf(IterableObject::class, $iterableObject);
Expand Down
8 changes: 4 additions & 4 deletions tests/IterableReduceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

it('reduces an array', function (): void {
$iterable = [1, 2];
$reduce = static function ($carry, $item) {
return $carry + $item;
$reduce = static function (?int $carry, int $item): int {
return (int) $carry + $item;
};
assertSame(3, iterable_reduce($iterable, $reduce, 0));
});

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