-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Narrow type of Collection::first() when using Collection::isEmpty()
Co-authored-by: Semyon <[email protected]>
- Loading branch information
1 parent
f2a650c
commit 9ce54d8
Showing
5 changed files
with
183 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
62 changes: 62 additions & 0 deletions
62
src/Type/Doctrine/Collection/FirstTypeSpecifyingExtension.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,62 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Collection; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
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\Constant\ConstantBooleanType; | ||
use PHPStan\Type\MethodTypeSpecifyingExtension; | ||
|
||
final class FirstTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private const COLLECTION_CLASS = 'Doctrine\Common\Collections\Collection'; | ||
private const IS_EMPTY_METHOD_NAME = 'isEmpty'; | ||
private const FIRST_METHOD_NAME = 'first'; | ||
|
||
/** @var TypeSpecifier */ | ||
private $typeSpecifier; | ||
|
||
public function getClass(): string | ||
{ | ||
return self::COLLECTION_CLASS; | ||
} | ||
|
||
public function isMethodSupported( | ||
MethodReflection $methodReflection, | ||
MethodCall $node, | ||
TypeSpecifierContext $context | ||
): bool | ||
{ | ||
return ( | ||
$methodReflection->getDeclaringClass()->getName() === self::COLLECTION_CLASS | ||
|| $methodReflection->getDeclaringClass()->isSubclassOf(self::COLLECTION_CLASS) | ||
) | ||
&& $methodReflection->getName() === self::IS_EMPTY_METHOD_NAME; | ||
} | ||
|
||
public function specifyTypes( | ||
MethodReflection $methodReflection, | ||
MethodCall $node, | ||
Scope $scope, | ||
TypeSpecifierContext $context | ||
): SpecifiedTypes | ||
{ | ||
return $this->typeSpecifier->create( | ||
new MethodCall($node->var, self::FIRST_METHOD_NAME), | ||
new ConstantBooleanType(false), | ||
$context | ||
); | ||
} | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
tests/Type/Doctrine/Collection/FirstTypeSpecifyingExtensionTest.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,46 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Collection; | ||
|
||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<VariableTypeReportingRule> | ||
*/ | ||
class FirstTypeSpecifyingExtensionTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new VariableTypeReportingRule(); | ||
} | ||
|
||
/** | ||
* @return \PHPStan\Type\MethodTypeSpecifyingExtension[] | ||
*/ | ||
protected function getMethodTypeSpecifyingExtensions(): array | ||
{ | ||
return [ | ||
new FirstTypeSpecifyingExtension(), | ||
]; | ||
} | ||
|
||
public function testExtension(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/collection.php'], [ | ||
[ | ||
'Variable $entityOrFalse is: MyEntity|false', | ||
18, | ||
], | ||
[ | ||
'Variable $false is: false', | ||
22, | ||
], | ||
[ | ||
'Variable $entity is: MyEntity', | ||
27, | ||
], | ||
]); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
tests/Type/Doctrine/Collection/VariableTypeReportingRule.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,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Collection; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<Node\Expr\Variable> | ||
*/ | ||
class VariableTypeReportingRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Variable::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!is_string($node->name)) { | ||
return []; | ||
} | ||
if (!$scope->isInFirstLevelStatement()) { | ||
return []; | ||
}; | ||
|
||
if ($scope->isInExpressionAssign($node)) { | ||
return []; | ||
} | ||
|
||
return [ | ||
sprintf( | ||
'Variable $%s is: %s', | ||
$node->name, | ||
$scope->getType($node)->describe(\PHPStan\Type\VerbosityLevel::value()) | ||
), | ||
]; | ||
} | ||
|
||
} |
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,28 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
|
||
class MyEntity | ||
{ | ||
|
||
} | ||
|
||
$new = new MyEntity(); | ||
|
||
/** | ||
* @var ArrayCollection<int, MyEntity> $collection | ||
*/ | ||
$collection = new ArrayCollection(); | ||
|
||
$entityOrFalse = $collection->first(); | ||
$entityOrFalse; | ||
|
||
if ($collection->isEmpty()) { | ||
$false = $collection->first(); | ||
$false; | ||
} | ||
|
||
if (!$collection->isEmpty()) { | ||
$entity = $collection->first(); | ||
$entity; | ||
} |