-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Type-specifying extension for ReflectionClass::isSubclassOf()
- Loading branch information
1 parent
f124a45
commit d3b5d60
Showing
5 changed files
with
86 additions
and
2 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
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
55 changes: 55 additions & 0 deletions
55
src/Type/Php/ReflectionClassIsSubclassOfTypeSpecifyingExtension.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,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
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\ConstantStringType; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\MethodTypeSpecifyingExtension; | ||
use PHPStan\Type\ObjectType; | ||
|
||
class ReflectionClassIsSubclassOfTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return \ReflectionClass::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool | ||
{ | ||
return $methodReflection->getName() === 'isSubclassOf' | ||
&& isset($node->args[0]) | ||
&& $context->true(); | ||
} | ||
|
||
public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes | ||
{ | ||
$valueType = $scope->getType($node->args[0]->value); | ||
if (!$valueType instanceof ConstantStringType) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
$node->var, | ||
new GenericObjectType(\ReflectionClass::class, [ | ||
new ObjectType($valueType->getValue()), | ||
]), | ||
$context | ||
); | ||
} | ||
|
||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Bug4577; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
class Test | ||
{ | ||
|
||
public function test(\ReflectionClass $refClass): void | ||
{ | ||
if ($refClass->isSubclassOf(Test::class)) { | ||
$instance = $refClass->newInstance(); | ||
assertType(Test::class, $instance); | ||
} | ||
} | ||
|
||
} |