-
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix type for collection->whereNotNull (#1332)
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
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
51 changes: 51 additions & 0 deletions
51
src/ReturnTypes/CollectionWhereNotNullDynamicReturnTypeExtension.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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\ReturnTypes; | ||
|
||
use Illuminate\Support\Enumerable; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class CollectionWhereNotNullDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function getClass(): string | ||
{ | ||
return Enumerable::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'whereNotNull'; | ||
} | ||
|
||
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(); | ||
} | ||
|
||
$nonFalseyTypes = TypeCombinator::removeNull($valueType); | ||
|
||
return new GenericObjectType($calledOnType->getClassName(), [$keyType, $nonFalseyTypes]); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace CollectionWhereNotNull; | ||
|
||
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | ||
use function PHPStan\Testing\assertType; | ||
|
||
assertType('Illuminate\Support\Collection<int, int|string>', collect([1, 2, null, '', 'hello'])->whereNotNull()); | ||
assertType('Illuminate\Support\Collection<int, int|string>', collect([1, 2, null, '', 'hello'])->whereNotNull()); | ||
|
||
/** @param \Illuminate\Database\Eloquent\Collection<int, \App\User> $foo */ | ||
function objectAndParam(EloquentCollection $foo): void | ||
{ | ||
assertType("Illuminate\Database\Eloquent\Collection<int, App\User>", $foo->whereNotNull('blocked')); | ||
} | ||
|
||
/** @param \Illuminate\Database\Eloquent\Collection<int, ?\App\User> $foo */ | ||
function objectOrNullAndParam(EloquentCollection $foo): void | ||
{ | ||
assertType("Illuminate\Database\Eloquent\Collection<int, App\User>", $foo->whereNotNull('blocked')); | ||
} | ||
|
||
/** @param \Illuminate\Database\Eloquent\Collection<int, ?\App\User> $foo */ | ||
function objectOrNull(EloquentCollection $foo): void | ||
{ | ||
assertType("Illuminate\Database\Eloquent\Collection<int, App\User>", $foo->whereNotNull()); | ||
} |