Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds dynmaic method return type extension for
Enumerable::filter
- Loading branch information
Showing
5 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/ReturnTypes/CollectionDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\ReturnTypes; | ||
|
||
use Illuminate\Support\Enumerable; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PHPStan\Analyser\MutatingScope; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantFloatType; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\UnionType; | ||
|
||
class CollectionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function getClass(): string | ||
{ | ||
return Enumerable::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'filter'; | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type { | ||
$calledOnType = $scope->getType($methodCall->var); | ||
|
||
if (! $calledOnType instanceof \PHPStan\Type\Generic\GenericObjectType) { | ||
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$keyType = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap()->getType('TKey'); | ||
$valueType = $methodReflection->getDeclaringClass()->getActiveTemplateTypeMap()->getType('TValue'); | ||
|
||
if ($keyType === null || $valueType === null) { | ||
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
if (count($methodCall->getArgs()) < 1) { | ||
$falseyTypes = $this->getFalseyTypes(); | ||
|
||
return new GenericObjectType($calledOnType->getClassName(), [$keyType, TypeCombinator::remove($valueType, $falseyTypes)]); | ||
} | ||
|
||
$callbackArg = $methodCall->getArgs()[0]->value; | ||
|
||
$var = null; | ||
$expr = null; | ||
|
||
if ($callbackArg instanceof Closure && count($callbackArg->stmts) === 1 && count($callbackArg->params) > 0) { | ||
$statement = $callbackArg->stmts[0]; | ||
if ($statement instanceof Return_ && $statement->expr !== null) { | ||
$var = $callbackArg->params[0]->var; | ||
$expr = $statement->expr; | ||
} | ||
} elseif ($callbackArg instanceof ArrowFunction && count($callbackArg->params) > 0) { | ||
$var = $callbackArg->params[0]->var; | ||
$expr = $callbackArg->expr; | ||
} | ||
|
||
if ($var !== null && $expr !== null) { | ||
if (!$var instanceof Variable || !is_string($var->name)) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
$itemVariableName = $var->name; | ||
|
||
// @phpstan-ignore-next-line | ||
$scope = $scope->assignVariable($itemVariableName, $valueType); | ||
$scope = $scope->filterByTruthyValue($expr); | ||
$valueType = $scope->getVariableType($itemVariableName); | ||
} | ||
|
||
return new GenericObjectType($calledOnType->getClassName(), [$keyType, $valueType]); | ||
} | ||
|
||
private function getFalseyTypes(): UnionType | ||
{ | ||
return new UnionType([new NullType(), new ConstantBooleanType(false), new ConstantIntegerType(0), new ConstantFloatType(0.0), new ConstantStringType(''), new ConstantStringType('0'), new ConstantArrayType([], [])]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Type; | ||
|
||
class CollectionDynamicReturnTypeExtensionTest extends \PHPStan\Testing\TypeInferenceTestCase | ||
{ | ||
/** | ||
* @return iterable<mixed> | ||
*/ | ||
public function dataFileAsserts(): iterable | ||
{ | ||
yield from $this->gatherAssertTypes(__DIR__ . '/data/collection-filter.php'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataFileAsserts | ||
*/ | ||
public function testFileAsserts( | ||
string $assertType, | ||
string $file, | ||
...$args | ||
): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [__DIR__ . '/../../extension.neon']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace CollectionFilter; | ||
|
||
use App\Account; | ||
use App\User; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | ||
use function PHPStan\Testing\assertType; | ||
|
||
assertType('Illuminate\Support\Collection<int, non-empty-string>', collect(['foo', null, '', 'bar', null])->filter()); | ||
|
||
/** @param Collection<int, mixed> $foo */ | ||
function foo(Collection $foo): void { | ||
assertType("Illuminate\Support\Collection<int, mixed~0|0.0|''|'0'|array{}|false|null>", $foo->filter()); | ||
} | ||
|
||
/** | ||
* @param array<int, User> $attachments | ||
*/ | ||
function storeAttachments(array $attachments) | ||
{ | ||
assertType( | ||
'Illuminate\Support\Collection<int, App\Account>', | ||
collect($attachments) | ||
->map(function (User $attachment): ?Account { | ||
return convertToAccount($attachment); | ||
}) | ||
->filter() | ||
); | ||
} | ||
|
||
function convertToAccount(User $user): ?Account | ||
{ | ||
// | ||
} | ||
|
||
assertType('Illuminate\Support\Collection<int, int<3, max>>', collect([1, 2, 3, 4, 5, 6])->filter(function (int $value) { return $value > 2; })); | ||
|
||
/** @param EloquentCollection<User> $foo */ | ||
function bar(Collection $foo): void { | ||
assertType("Illuminate\Database\Eloquent\Collection<int, App\User>", $foo->filter(function (User $user) { return ! $user->blocked; })); | ||
} |