Skip to content

Commit

Permalink
Workaround for static::class in ReflectionExtension (#63)
Browse files Browse the repository at this point in the history
Fixes #62
  • Loading branch information
marcospassos authored and pepakriz committed Feb 13, 2019
1 parent c8dfd44 commit 53ebd08
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/Extension/ReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

use Pepakriz\PHPStanExceptionRules\DynamicConstructorThrowTypeExtension;
use Pepakriz\PHPStanExceptionRules\UnsupportedClassException;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\MethodReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -77,8 +86,7 @@ private function resolveReflectionClass(New_ $newNode, Scope $scope): Type
return $reflectionExceptionType;
}

$valueType = $scope->getType($newNode->args[0]->value);

$valueType = $this->resolveType($newNode->args[0]->value, $scope);
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
if (!$this->broker->hasClass($constantString->getValue())) {
return $reflectionExceptionType;
Expand All @@ -101,7 +109,7 @@ private function resolveReflectionFunction(New_ $newNode, Scope $scope): Type
return $reflectionExceptionType;
}

$valueType = $scope->getType($newNode->args[0]->value);
$valueType = $this->resolveType($newNode->args[0]->value, $scope);
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
if (!$this->broker->hasFunction(new Name($constantString->getValue()), $scope)) {
return $reflectionExceptionType;
Expand All @@ -124,8 +132,8 @@ private function resolveReflectionProperty(New_ $newNode, Scope $scope): Type
return $reflectionExceptionType;
}

$valueType = $scope->getType($newNode->args[0]->value);
$propertyType = $scope->getType($newNode->args[1]->value);
$valueType = $this->resolveType($newNode->args[0]->value, $scope);
$propertyType = $this->resolveType($newNode->args[1]->value, $scope);
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
if (!$this->broker->hasClass($constantString->getValue())) {
return $reflectionExceptionType;
Expand Down Expand Up @@ -166,8 +174,7 @@ private function resolveReflectionExtension(New_ $newNode, Scope $scope): Type
return $reflectionExceptionType;
}

$valueType = $scope->getType($newNode->args[0]->value);

$valueType = $this->resolveType($newNode->args[0]->value, $scope);
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
if (!extension_loaded($constantString->getValue())) {
return $reflectionExceptionType;
Expand All @@ -183,4 +190,48 @@ private function resolveReflectionExtension(New_ $newNode, Scope $scope): Type
return new VoidType();
}

private function resolveType(Expr $node, Scope $scope): Type
{
$classReflection = $scope->getClassReflection();

if (
$classReflection !== null
&& $node instanceof ClassConstFetch
&& $node->class instanceof Name
&& $node->name instanceof Identifier
&& $node->class->toString() === 'static'
&& $node->name->toString() === 'class'
) {
return new ConstantStringType($classReflection->getName());
}

$traverser = new NodeTraverser();
$traverser->addVisitor(new CloningVisitor()); // deep copy
$traverser->addVisitor(new class extends NodeVisitorAbstract {

public function enterNode(Node $node): Node
{
if (
$node instanceof ClassConstFetch
&& $node->class instanceof Name
&& $node->name instanceof Identifier
&& $node->class->toString() === 'static'
&& $node->name->toString() === 'class'
) {
$node->class->parts[0] = 'self';
}

return $node;
}

});

$node = $traverser->traverse([$node])[0];
if (!$node instanceof Expr) {
throw new ShouldNotHappenException();
}

return $scope->getType($node);
}

}
4 changes: 4 additions & 0 deletions tests/src/Rules/data/throws-php-internal-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public function testReflection(): void
new ReflectionObject(new stdClass());

new ReflectionClass(self::class);
new ReflectionClass(static::class);
new ReflectionClass('undefinedClass'); // error: Missing @throws ReflectionException annotation

new ReflectionProperty(self::class, 'property');
new ReflectionProperty(static::class, 'property');
new ReflectionProperty(self::class, 'undefinedProperty'); // error: Missing @throws ReflectionException annotation
new ReflectionProperty('undefinedClass', 'property'); // error: Missing @throws ReflectionException annotation
new ReflectionProperty('undefinedClass', 'undefinedProperty'); // error: Missing @throws ReflectionException annotation
Expand All @@ -48,10 +50,12 @@ public function testReflection(): void
new ReflectionZendExtension('unknownZendExtension'); // error: Missing @throws ReflectionException annotation

new ReflectionClass(rand(0, 1) === 0 ? self::class : Throwable::class);
new ReflectionClass(rand(0, 1) === 0 ? static::class : Throwable::class);
new ReflectionClass(rand(0, 1) === 0 ? self::class : null); // error: Missing @throws ReflectionException annotation
new ReflectionClass(rand(0, 1) === 0 ? self::class : 'undefinedClass'); // error: Missing @throws ReflectionException annotation

new ReflectionProperty(rand(0, 1) === 0 ? self::class : ValueObject::class, rand(0, 1) === 0 ? 'property' : 'secondProperty');
new ReflectionProperty(rand(0, 1) === 0 ? static::class : ValueObject::class, rand(0, 1) === 0 ? 'property' : 'secondProperty');
new ReflectionProperty(rand(0, 1) === 0 ? self::class : null, rand(0, 1) === 0 ? 'property' : 'secondProperty'); // error: Missing @throws ReflectionException annotation
new ReflectionProperty(rand(0, 1) === 0 ? self::class : Throwable::class, rand(0, 1) === 0 ? 'property' : 'undefinedProperty'); // error: Missing @throws ReflectionException annotation

Expand Down

0 comments on commit 53ebd08

Please sign in to comment.