-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - RuntimeReflectionFunctionRule
- Loading branch information
1 parent
1d58f26
commit c4a662a
Showing
7 changed files
with
192 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
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
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,76 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function array_keys; | ||
use function in_array; | ||
use function sprintf; | ||
use function strpos; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\FuncCall> | ||
*/ | ||
class RuntimeReflectionFunctionRule implements Rule | ||
{ | ||
|
||
public function __construct(private ReflectionProvider $reflectionProvider) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->name instanceof Node\Name) { | ||
return []; | ||
} | ||
|
||
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { | ||
return []; | ||
} | ||
|
||
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); | ||
if (!in_array($functionReflection->getName(), [ | ||
'is_a', | ||
'is_subclass_of', | ||
'class_parents', | ||
'class_implements', | ||
'class_uses', | ||
], true)) { | ||
return []; | ||
} | ||
|
||
if (!$scope->isInClass()) { | ||
return []; | ||
} | ||
|
||
$classReflection = $scope->getClassReflection(); | ||
$hasPhpStanInterface = false; | ||
foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) { | ||
if (strpos($interfaceName, 'PHPStan\\') !== 0) { | ||
continue; | ||
} | ||
|
||
$hasPhpStanInterface = true; | ||
} | ||
|
||
if (!$hasPhpStanInterface) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('Function %s() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', $functionReflection->getName()), | ||
)->build(), | ||
]; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
tests/PHPStan/Rules/Api/RuntimeReflectionFunctionRuleTest.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,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Api; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<RuntimeReflectionFunctionRule> | ||
*/ | ||
class RuntimeReflectionFunctionRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new RuntimeReflectionFunctionRule($this->createReflectionProvider()); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/runtime-reflection-function.php'], [ | ||
[ | ||
'Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', | ||
43, | ||
], | ||
[ | ||
'Function is_subclass_of() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', | ||
46, | ||
], | ||
[ | ||
'Function class_parents() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', | ||
49, | ||
], | ||
[ | ||
'Function class_implements() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', | ||
50, | ||
], | ||
[ | ||
'Function class_uses() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.', | ||
51, | ||
], | ||
]); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
tests/PHPStan/Rules/Api/data/runtime-reflection-function.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,54 @@ | ||
<?php | ||
|
||
namespace RuntimeReflectionFunction; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(object $o): void | ||
{ | ||
if (is_a($o, \stdClass::class)) { | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
class Bar implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
// TODO: Implement getClass() method. | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
// TODO: Implement isMethodSupported() method. | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
{ | ||
// TODO: Implement getTypeFromMethodCall() method. | ||
} | ||
|
||
public function doFoo(object $o): void | ||
{ | ||
if (is_a($o, \stdClass::class)) { | ||
|
||
} | ||
if (is_subclass_of($o, \stdClass::class)) { | ||
|
||
} | ||
$p = class_parents($o); | ||
$i = class_implements($o); | ||
$t = class_uses($o); | ||
} | ||
|
||
} |
c4a662a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you are aware of it, but if not: we recently fixed a static vs runtime reflection issue which was triggered by extension code which instantiated a
ReflectionMethod
objectsee deprecated-packages/symplify#4166
these should also be reported as an error I guess…?
c4a662a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s literally the next item on my todolist 😊