Skip to content
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
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php: ['7.3', '7.4', '8.0']
php: ['8.1','8.2','8.3','8.4']

steps:
- name: "Checkout"
Expand All @@ -26,7 +26,7 @@ jobs:
extensions: "json, dom, mbstring"

- name: "Cache dependencies"
uses: "actions/cache@v1.1.2"
uses: "actions/cache@v4"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php }}-composer-cache-${{ hashFiles('**/composer.json') }}"
Expand All @@ -44,7 +44,7 @@ jobs:

strategy:
matrix:
php: ['7.3', '7.4', '8.0']
php: ['8.1','8.2','8.3','8.4']

steps:
- name: "Checkout"
Expand All @@ -57,7 +57,7 @@ jobs:
extensions: "json, dom, mbstring"

- name: "Cache dependencies"
uses: "actions/cache@v1.1.2"
uses: "actions/cache@v4"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php }}-composer-cache-${{ hashFiles('**/composer.json') }}"
Expand All @@ -75,7 +75,7 @@ jobs:

strategy:
matrix:
php: ['8.0']
php: ['8.1']

steps:
- name: "Checkout"
Expand All @@ -91,7 +91,7 @@ jobs:
run: "composer validate"

- name: "Cache dependencies"
uses: "actions/cache@v1.1.2"
uses: "actions/cache@v4"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php }}-composer-cache-${{ hashFiles('**/composer.json') }}"
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ test:
$(MAKE) fmt-check

phpstan:
docker run -it --rm -v ${PWD}:/app -w /app php:7.3-cli-alpine php -d error_reporting=-1 -d memory_limit=-1 bin/phpstan --ansi analyse
docker run -it --rm -v ${PWD}:/app -w /app php:8.4-cli-alpine php -d memory_limit=-1 bin/phpstan --ansi analyse

phpstan-clear-cache:
docker run -it --rm -v ${PWD}:/app -w /app php:7.3-cli-alpine php -d error_reporting=-1 -d memory_limit=-1 bin/phpstan --ansi clear-result-cache
docker run -it --rm -v ${PWD}:/app -w /app php:8.4-cli-alpine php -d error_reporting=-1 -d memory_limit=-1 bin/phpstan --ansi clear-result-cache

phpunit:
docker run -it --rm -v ${PWD}:/app -w /app php:7.3-cli-alpine php -d error_reporting=-1 bin/phpunit --colors=always -c phpunit.xml
docker run -it --rm -v ${PWD}:/app -w /app php:8.4-cli-alpine php -d error_reporting=-1 bin/phpunit --colors=always -c phpunit.xml

fmt-check:
docker run -it --rm -v ${PWD}:/app -w /app php:7.3-cli-alpine php bin/phpcs --standard=./ruleset.xml --extensions=php --tab-width=4 -sp ./src ./tests
docker run -it --rm -v ${PWD}:/app -w /app php:8.4-cli-alpine php bin/phpcs --standard=./ruleset.xml --extensions=php --tab-width=4 -sp ./src ./tests

fmt:
docker run -it --rm -v ${PWD}:/app -w /app php:7.3-cli-alpine php bin/phpcbf --standard=./ruleset.xml --extensions=php --tab-width=4 -sp ./src ./tests
docker run -it --rm -v ${PWD}:/app -w /app php:8.4-cli-alpine php bin/phpcbf --standard=./ruleset.xml --extensions=php --tab-width=4 -sp ./src ./tests
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": "0.5.0",
"name": "bonami/phpstan-collections",
"type": "phpstan-extension",
"description": "Phpstan extension for bonami/collections library",
Expand All @@ -10,16 +11,16 @@
}
],
"require": {
"php": ">=7.3|^8.0",
"phpstan/phpstan": "^1.0"
"php": "^8.1",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"ergebnis/composer-normalize": "^2.0.2",
"phpunit/phpunit": "^9.4.2",
"slevomat/coding-standard": "^6.4.1",
"squizlabs/php_codesniffer": "^3.5.0",
"bonami/collections": "^0.4.5"
"bonami/collections": "0.6.0"
},
"config": {
"bin-dir": "bin",
Expand All @@ -30,7 +31,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.4.x-dev"
"dev-master": "0.5.x-dev"
},
"phpstan": {
"includes": [
Expand Down
16 changes: 14 additions & 2 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
services:
-
class: Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension
arguments:
- 'Bonami\Collection\ArrayList'
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension
arguments:
- 'Bonami\Collection\LazyList'
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Bonami\Collection\Phpstan\ArrayListWithoutNullsReturnTypeExtension
tags:
Expand All @@ -10,7 +22,7 @@ services:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: Bonami\Collection\Phpstan\LateStaticBindingMethodReturnTypeExtension
factory: Bonami\Collection\Phpstan\LateStaticBindingMethodReturnTypeExtension::forMethods('Bonami\Collection\ArrayList', ['uniqueBy', 'unique', 'union', 'filter', 'sort', 'take', 'slice', 'minus', 'minusOne', 'concat', 'intersect', 'reverse'])
factory: Bonami\Collection\Phpstan\LateStaticBindingMethodReturnTypeExtension::forMethods('Bonami\Collection\ArrayList', ['uniqueBy', 'unique', 'union', 'sort', 'take', 'slice', 'minus', 'minusOne', 'concat', 'intersect', 'reverse'])
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
Expand All @@ -30,7 +42,7 @@ services:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: Bonami\Collection\Phpstan\LateStaticBindingMethodReturnTypeExtension
factory: Bonami\Collection\Phpstan\LateStaticBindingMethodReturnTypeExtension::forMethods('Bonami\Collection\LazyList', ['take', 'filter', 'dropWhile', 'drop', 'concat', 'add', 'insertOnPosition'])
factory: Bonami\Collection\Phpstan\LateStaticBindingMethodReturnTypeExtension::forMethods('Bonami\Collection\LazyList', ['take', 'dropWhile', 'drop', 'concat', 'add', 'insertOnPosition'])
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ includes:
- extension.neon
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
parameters:
ignoreErrors:
- '~Doing instanceof PHPStan\\Type\\Generic\\GenericObjectType is error-prone and deprecated~'
reportUnmatchedIgnoredErrors: true
level: 9
paths:
Expand Down
1 change: 0 additions & 1 deletion ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<rule ref="SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.RequireTernaryOperator"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>
<rule ref="SlevomatCodingStandard.Functions.DisallowArrowFunction"/>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="SlevomatCodingStandard.Namespaces.RequireOneNamespaceInFile"/>
<rule ref="SlevomatCodingStandard.Namespaces.NamespaceDeclaration"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
Expand All @@ -18,9 +17,10 @@

class GroupByMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
/** @var string */
private $class;
/** @var class-string */
private string $class;

/** @param class-string $class */
public function __construct(string $class)
{
$this->class = $class;
Expand All @@ -43,8 +43,9 @@ public function getTypeFromMethodCall(
): Type {
$arg = $methodCall->args[0];
assert($arg instanceof Arg);

$closure = $scope->getType($arg->value);
assert($closure instanceof ClosureType || $closure instanceof CallableType);
assert($closure instanceof ClosureType);

$listType = $scope->getType($methodCall->var);

Expand Down
33 changes: 33 additions & 0 deletions src/Bonami/Collection/Phpstan/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Bonami\Collection\Phpstan;

use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Return_;

class Helpers
{
/** @param Expr\ArrowFunction|Expr\Closure $closure */
public static function getSimpleClosureExpression($closure): ?Expr
{
if ($closure instanceof Expr\ArrowFunction) {
return $closure->expr;
}

if (!($closure instanceof Expr\Closure)) {

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.

Check failure on line 19 in src/Bonami/Collection/Phpstan/Helpers.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Instanceof between PhpParser\Node\Expr\Closure and PhpParser\Node\Expr\Closure will always evaluate to true.
return null;
}

if (!isset($closure->stmts[0])) {
return null;
}

if (!($closure->stmts[0] instanceof Return_)) {
return null;
}

return $closure->stmts[0]->expr;
}
}
110 changes: 110 additions & 0 deletions src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Bonami\Collection\Phpstan;

use Bonami\Collection\ArrayList;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Return_;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\CallableType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;

class IterableFilterTypeNarrowingExtension implements DynamicMethodReturnTypeExtension, TypeSpecifierAwareExtension
{
/** @var TypeSpecifier */
private $typeSpecifier;

/** @var string */
private $class;

public function __construct(string $class)
{
$this->class = $class;
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

public function getClass(): string
{
return $this->class;

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.

Check failure on line 48 in src/Bonami/Collection/Phpstan/IterableFilterTypeNarrowingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Bonami\Collection\Phpstan\IterableFilterTypeNarrowingExtension::getClass() should return class-string but returns string.
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'filter';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): ?Type {
$type = $scope->getType($methodCall->var);

if (!$type instanceof GenericObjectType) {
return $type;
}

$args = $methodCall->getArgs();
if (!array_key_exists(0, $args)) {
return $type;
}

$arg = $methodCall->getArgs()[0]->value;
if (!($arg instanceof Closure || $arg instanceof ArrowFunction)) {
return $type;
}

$expr = Helpers::getSimpleClosureExpression($arg);
if ($expr !== null) {
$specifiedTypes = $this->typeSpecifier
->specifyTypesInCondition($scope, $expr, TypeSpecifierContext::createTruthy());

if (count($specifiedTypes->getSureTypes()) !== 0) {
return new GenericObjectType(
$methodReflection->getDeclaringClass()->getName(),
array_values(array_map(
static function (array $pair) use ($type) {
return TypeCombinator::intersect($type->getIterableValueType(), $pair[1]);
},
$specifiedTypes->getSureTypes(),
))
);
}

if (count($specifiedTypes->getSureNotTypes()) !== 0) {
return new GenericObjectType(
$methodReflection->getDeclaringClass()->getName(),
array_values(array_map(
static function (array $pair) use ($type) {
return TypeCombinator::remove($type->getIterableValueType(), $pair[1]);
},
$specifiedTypes->getSureNotTypes(),
))
);
}
return $type;
}

return $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

class LateStaticBindingMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
/** @var string */
private $class;
/** @var class-string */
private string $class;

/** @var array<string, int> */
private $methods;
private array $methods;

/**
* @param string $class
* @param class-string $class
* @param array<string> $methods
*/
private function __construct(string $class, array $methods)
Expand All @@ -29,7 +29,7 @@ private function __construct(string $class, array $methods)
}

/**
* @param string $class
* @param class-string $class
* @param array<string> $methods
*/
public static function forMethods(string $class, array $methods): self
Expand Down
Loading
Loading