-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
186 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
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
58 changes: 58 additions & 0 deletions
58
src/Type/Php/ReflectionClassGetAttributesMethodReturnTypeExtension.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,58 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Generic\GenericClassStringType; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
class ReflectionClassGetAttributesMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return \ReflectionClass::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'getAttributes'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
if (count($methodCall->args) === 0) { | ||
return $this->getDefaultReturnType($scope, $methodCall, $methodReflection); | ||
} | ||
$argType = $scope->getType($methodCall->args[0]->value); | ||
|
||
if ($argType instanceof ConstantStringType) { | ||
$classType = new ObjectType($argType->getValue()); | ||
} elseif ($argType instanceof GenericClassStringType) { | ||
$classType = $argType->getGenericType(); | ||
} else { | ||
return $this->getDefaultReturnType($scope, $methodCall, $methodReflection); | ||
} | ||
|
||
return new ArrayType(new MixedType(), new GenericObjectType(\ReflectionAttribute::class, [$classType])); | ||
} | ||
|
||
private function getDefaultReturnType(Scope $scope, MethodCall $methodCall, MethodReflection $methodReflection): Type | ||
{ | ||
return ParametersAcceptorSelector::selectFromArgs( | ||
$scope, | ||
$methodCall->args, | ||
$methodReflection->getVariants() | ||
)->getReturnType(); | ||
} | ||
|
||
} |
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,21 @@ | ||
<?php | ||
|
||
/** | ||
* @template-covariant T of object | ||
*/ | ||
class ReflectionAttribute | ||
{ | ||
/** | ||
* @return class-string<T> | ||
*/ | ||
public function getName() : string | ||
{ | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function newInstance() : object | ||
{ | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
if (\PHP_VERSION_ID < 80000) { | ||
if (class_exists('ReflectionAttribute', false)) { | ||
return; | ||
} | ||
|
||
final class ReflectionAttribute | ||
{ | ||
public function getName(): string | ||
{ | ||
} | ||
|
||
public function getTarget(): int | ||
{ | ||
} | ||
|
||
public function isRepeated(): bool | ||
{ | ||
} | ||
|
||
public function getArguments(): array | ||
{ | ||
} | ||
|
||
public function newInstance(): object | ||
{ | ||
} | ||
|
||
private function __clone() | ||
{ | ||
} | ||
|
||
private function __construct() | ||
{ | ||
} | ||
} | ||
} |
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
tests/PHPStan/Analyser/data/reflectionclass-issue-5511-php8.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 Issue5511; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
#[\Attribute] | ||
class Abc | ||
{ | ||
} | ||
|
||
#[Abc] | ||
class X | ||
{ | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @param class-string $className | ||
* @param class-string<Abc> $genericClassName | ||
*/ | ||
function testGetAttributes(string $str, string $className, string $genericClassName): void | ||
{ | ||
$class = new \ReflectionClass(X::class); | ||
|
||
$attrsAll = $class->getAttributes(); | ||
$attrsAbc1 = $class->getAttributes(Abc::class); | ||
$attrsAbc2 = $class->getAttributes(Abc::class, \ReflectionAttribute::IS_INSTANCEOF); | ||
$attrsGCN = $class->getAttributes($genericClassName); | ||
$attrsCN = $class->getAttributes($className); | ||
$attrsStr = $class->getAttributes($str); | ||
$attrsNonsense = $class->getAttributes("some random string"); | ||
|
||
assertType('array<ReflectionAttribute<object>>', $attrsAll); | ||
assertType('array<ReflectionAttribute<Issue5511\Abc>>', $attrsAbc1); | ||
assertType('array<ReflectionAttribute<Issue5511\Abc>>', $attrsAbc2); | ||
assertType('array<ReflectionAttribute<Issue5511\Abc>>', $attrsGCN); | ||
assertType('array<ReflectionAttribute<object>>', $attrsCN); | ||
assertType('array<ReflectionAttribute<object>>', $attrsStr); | ||
assertType('array<ReflectionAttribute<some random string>>', $attrsNonsense); | ||
} | ||
|
||
/** | ||
* @param \ReflectionAttribute<Abc> $ra | ||
*/ | ||
function testNewInstance(\ReflectionAttribute $ra): void | ||
{ | ||
assertType('ReflectionAttribute<Issue5511\\Abc>', $ra); | ||
$abc = $ra->newInstance(); | ||
assertType(Abc::class, $abc); | ||
} |
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