Skip to content

Add PHPStan #19

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
34 changes: 34 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Static Analysis"

on:
pull_request:
push:
branches:
- "master"

jobs:
static-analysis-phpstan:
name: "Static Analysis with PHPStan"
runs-on: "ubuntu-20.04"

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

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

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: "cs2pr"

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"

- name: "Run a static analysis with phpstan/phpstan"
run: "vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/.phpcs-cache
/.phpunit.result.cache
/composer.lock
/phpcs.xml
/phpstan.neon
/phpunit.xml
/vendor/
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"require-dev": {
"doctrine/coding-standard": "^8.2",
"pestphp/pest": "^1.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^0.12.67",
"phpstan/phpstan-strict-rules": "^0.12.9",
"symfony/var-dumper": "^5.2"
},
"config": {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: max
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
6 changes: 4 additions & 2 deletions src/IterableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@

/**
* @internal
*
* @implements IteratorAggregate<mixed>
*/
final class IterableObject implements IteratorAggregate
{
/** @var iterable<mixed> */
private $iterable;

/** @var callable */
/** @var callable|null */
private $filterFn;

/** @var callable */
/** @var callable|null */
private $mapFn;

/**
Expand Down
8 changes: 4 additions & 4 deletions src/iterable-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function iterable_to_traversable(iterable $iterable): Traversable
function iterable_filter(iterable $iterable, ?callable $filter = null)
{
if ($filter === null) {
$filter = static function ($value) {
$filter = static function ($value): bool {
return (bool) $value;
};
}
Expand All @@ -89,14 +89,14 @@ function iterable_filter(iterable $iterable, ?callable $filter = null)
* Reduces an iterable.
*
* @param iterable<mixed> $iterable
* @param callable(mixed, mixed) $reduce
* @param callable(mixed, mixed):mixed $reduce
*
* @return mixed
*
* @psalm-template TValue
* @psalm-template TResult
* @template TResult
* @psalm-param iterable<TValue> $iterable
* @psalm-param callable(TResult|null, TValue) $reduce
* @psalm-param callable(TResult|null, TValue):TResult $reduce
* @psalm-param TResult|null $initial
* @psalm-return TResult|null
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/IterableFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
assertEquals([1 => true], iterable_to_array(iterable_filter($iterable)));
});

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

it('filters an array with a callback', function (): void {
$iterable = ['foo', 'bar'];
$filter = static function ($input) {
$filter = 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) {
$filter = static function ($input): bool {
return $input === 'bar';
};
assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
Expand Down
9 changes: 5 additions & 4 deletions tests/IterableObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace BenTools\IterableFunctions\Tests;

use BenTools\IterableFunctions\IterableObject;
use Generator;
use SplFixedArray;

use function BenTools\IterableFunctions\iterable;
Expand All @@ -14,9 +15,9 @@
use function PHPUnit\Framework\assertInstanceOf;
use function test;

$dataProvider = function () {
$dataProvider = static function (): Generator {
$data = ['foo', 'bar'];
$filter = static function ($value) {
$filter = static function ($value): bool {
return $value === 'bar';
};
$map = 'strtoupper';
Expand Down Expand Up @@ -72,7 +73,7 @@
})->with($dataProvider());

it('filters the subject', function (): void {
$filter = static function ($value) {
$filter = static function ($value): bool {
return $value === 'bar';
};
$iterableObject = iterable(['foo', 'bar'])->filter($filter);
Expand All @@ -87,7 +88,7 @@
});

it('combines filter and map', function (): void {
$filter = static function ($value) {
$filter = static function ($value): bool {
return $value === 'bar';
};
$map = 'strtoupper';
Expand Down