diff --git a/composer.json b/composer.json index 3451572..a2c59c1 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ }, "require": { "php": "^7.4 || ^8.0", - "phpstan/phpstan": "^1.12.6 || ^2.0" + "phpstan/phpstan": "^2.1.3" }, "require-dev": { "nette/neon": "^3.3.1", diff --git a/src/Allowed/Allowed.php b/src/Allowed/Allowed.php index 84db037..333c60c 100644 --- a/src/Allowed/Allowed.php +++ b/src/Allowed/Allowed.php @@ -8,12 +8,10 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PHPStan\Analyser\Scope; -use PHPStan\BetterReflection\Reflection\Adapter\FakeReflectionAttribute; -use PHPStan\BetterReflection\Reflection\Adapter\ReflectionAttribute; -use PHPStan\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute; -use PHPStan\BetterReflection\Reflector\Reflector; +use PHPStan\Reflection\AttributeReflection; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\Type; use PHPStan\Type\UnionType; use Spaze\PHPStan\Rules\Disallowed\Disallowed; @@ -28,7 +26,7 @@ class Allowed private Formatter $formatter; - private Reflector $reflector; + private ReflectionProvider $reflectionProvider; private Identifier $identifier; @@ -39,13 +37,13 @@ class Allowed public function __construct( Formatter $formatter, - Reflector $reflector, + ReflectionProvider $reflectionProvider, Identifier $identifier, GetAttributesWhenInSignature $attributesWhenInSignature, AllowedPath $allowedPath ) { $this->formatter = $formatter; - $this->reflector = $reflector; + $this->reflectionProvider = $reflectionProvider; $this->identifier = $identifier; $this->attributesWhenInSignature = $attributesWhenInSignature; $this->allowedPath = $allowedPath; @@ -203,7 +201,7 @@ private function hasAllowedParamsInAllowed(Scope $scope, ?array $args, Disallowe /** - * @param list $attributes + * @param list $attributes * @param list $allowConfig * @return bool */ @@ -247,32 +245,29 @@ private function getArgType(array $args, Scope $scope, Param $param): ?Type /** * @param Scope $scope - * @return list|list + * @return list */ private function getAttributes(Scope $scope): array { - return $scope->isInClass() ? $scope->getClassReflection()->getNativeReflection()->getAttributes() : []; + return $scope->isInClass() ? $scope->getClassReflection()->getAttributes() : []; } /** * @param Node|null $node * @param Scope $scope - * @return list + * @return list */ private function getCallAttributes(?Node $node, Scope $scope): array { $function = $scope->getFunction(); - if ($function instanceof MethodReflection) { - return $scope->isInClass() ? $scope->getClassReflection()->getNativeReflection()->getMethod($function->getName())->getAttributes() : []; - } elseif ($function instanceof FunctionReflection) { - return $this->reflector->reflectFunction($function->getName())->getAttributes(); - } elseif ($function === null) { - if ($node instanceof ClassMethod && $scope->isInClass()) { - return $scope->getClassReflection()->getNativeReflection()->getMethod($node->name->name)->getAttributes(); - } elseif ($node instanceof Function_) { - return $this->reflector->reflectFunction($node->name->name)->getAttributes(); - } + if ($function !== null) { + return $function->getAttributes(); + } elseif ($node instanceof ClassMethod && $scope->isInClass()) { + return $scope->getClassReflection()->getNativeMethod($node->name->name)->getAttributes(); + } elseif ($node instanceof Function_ && $node->namespacedName !== null) { + return $this->reflectionProvider->getFunction($node->namespacedName, $scope)->getAttributes(); + } else { $attributes = $this->attributesWhenInSignature->get($scope); if ($attributes !== null) { return $attributes; @@ -284,7 +279,7 @@ private function getCallAttributes(?Node $node, Scope $scope): array /** * @param Scope $scope - * @return list|list + * @return list */ private function getAllMethodAttributes(Scope $scope): array { @@ -292,8 +287,9 @@ private function getAllMethodAttributes(Scope $scope): array return []; } $attributes = []; - foreach ($scope->getClassReflection()->getNativeReflection()->getMethods() as $method) { - $methodAttributes = $method->getAttributes(); + $classReflection = $scope->getClassReflection(); + foreach ($classReflection->getNativeReflection()->getMethods() as $method) { + $methodAttributes = $classReflection->getNativeMethod($method->getName())->getAttributes(); if ($methodAttributes !== []) { $attributes = array_merge($attributes, $methodAttributes); } diff --git a/src/Allowed/GetAttributesWhenInSignature.php b/src/Allowed/GetAttributesWhenInSignature.php index 8dcbd0a..7228442 100644 --- a/src/Allowed/GetAttributesWhenInSignature.php +++ b/src/Allowed/GetAttributesWhenInSignature.php @@ -3,29 +3,28 @@ namespace Spaze\PHPStan\Rules\Disallowed\Allowed; +use PhpParser\Node\Name; use PHPStan\Analyser\Scope; -use PHPStan\BetterReflection\Reflection\Adapter\FakeReflectionAttribute; -use PHPStan\BetterReflection\Reflection\Adapter\ReflectionAttribute; -use PHPStan\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute; -use PHPStan\BetterReflection\Reflector\Reflector; +use PHPStan\Reflection\AttributeReflection; +use PHPStan\Reflection\ReflectionProvider; class GetAttributesWhenInSignature { - private Reflector $reflector; + private ReflectionProvider $reflectionProvider; /** @var class-string|null */ private ?string $currentClass = null; private ?string $currentMethod = null; - /** @var string|null */ - private ?string $currentFunction = null; + /** @var Name|null */ + private ?Name $currentFunction = null; - public function __construct(Reflector $reflector) + public function __construct(ReflectionProvider $reflectionProvider) { - $this->reflector = $reflector; + $this->reflectionProvider = $reflectionProvider; } @@ -38,7 +37,7 @@ public function __construct(Reflector $reflector) * or the function name in a Function_ and a InFunctionNode rules. * * @param Scope $scope - * @return list|null + * @return list|null */ public function get(Scope $scope): ?array { @@ -48,9 +47,9 @@ public function get(Scope $scope): ?array && $scope->isInClass() && $scope->getClassReflection()->getName() === $this->currentClass ) { - return $scope->getClassReflection()->getNativeReflection()->getMethod($this->currentMethod)->getAttributes(); + return $scope->getClassReflection()->getNativeMethod($this->currentMethod)->getAttributes(); } elseif ($this->currentFunction !== null) { - return $this->reflector->reflectFunction($this->currentFunction)->getAttributes(); + return $this->reflectionProvider->getFunction($this->currentFunction, $scope)->getAttributes(); } return null; } @@ -75,10 +74,10 @@ public function unsetCurrentClassMethodName(): void /** - * @param string $functionName + * @param Name $functionName * @return void */ - public function setCurrentFunctionName(string $functionName): void + public function setCurrentFunctionName(Name $functionName): void { $this->currentFunction = $functionName; } diff --git a/src/HelperRules/SetCurrentFunctionNameHelperRule.php b/src/HelperRules/SetCurrentFunctionNameHelperRule.php index 323fede..1d88e4d 100644 --- a/src/HelperRules/SetCurrentFunctionNameHelperRule.php +++ b/src/HelperRules/SetCurrentFunctionNameHelperRule.php @@ -38,7 +38,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { if ($node->namespacedName !== null) { - $this->attributesWhenInSignature->setCurrentFunctionName($node->namespacedName->toString()); + $this->attributesWhenInSignature->setCurrentFunctionName($node->namespacedName); } return []; } diff --git a/src/Identifier/Identifier.php b/src/Identifier/Identifier.php index 8f742a6..9430912 100644 --- a/src/Identifier/Identifier.php +++ b/src/Identifier/Identifier.php @@ -3,17 +3,17 @@ namespace Spaze\PHPStan\Rules\Disallowed\Identifier; -use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; -use PHPStan\BetterReflection\Reflector\Reflector; +use PHPStan\Reflection\ReflectionProvider; class Identifier { - private Reflector $reflector; + private ReflectionProvider $reflectionProvider; - public function __construct(Reflector $reflector) + + public function __construct(ReflectionProvider $reflectionProvider) { - $this->reflector = $reflector; + $this->reflectionProvider = $reflectionProvider; } @@ -40,12 +40,10 @@ public function matches(string $pattern, string $value, array $excludes = [], ar } } if ($matches && $excludeWithAttributes) { - try { - $attributes = array_map(fn($a) => $a->getName(), $this->reflector->reflectClass($value)->getAttributes()); - } catch (IdentifierNotFound $e) { - $attributes = []; + if (!$this->reflectionProvider->hasClass($value)) { + return true; } - + $attributes = array_map(fn($a) => $a->getName(), $this->reflectionProvider->getClass($value)->getAttributes()); foreach ($attributes as $attribute) { foreach ($excludeWithAttributes as $excludeWithAttribute) { if (fnmatch($excludeWithAttribute, $attribute, FNM_NOESCAPE | FNM_CASEFOLD)) { diff --git a/src/Usages/NamespaceUsages.php b/src/Usages/NamespaceUsages.php index 20d4443..258dc2b 100644 --- a/src/Usages/NamespaceUsages.php +++ b/src/Usages/NamespaceUsages.php @@ -13,8 +13,8 @@ use PhpParser\Node\NullableType; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\TraitUse; -use PhpParser\Node\Stmt\UseUse; use PhpParser\Node\UnionType; +use PhpParser\Node\UseItem; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleError; @@ -85,7 +85,7 @@ public function processNode(Node $node, Scope $scope): array $namespaces[] = $this->namespaceUsageFactory->create($type->toString()); } } - } elseif ($node instanceof UseUse) { + } elseif ($node instanceof UseItem) { $namespaces = [$this->namespaceUsageFactory->create($node->name->toString(), true)]; } elseif ($node instanceof StaticCall && $node->class instanceof Name) { $namespaces = [$this->namespaceUsageFactory->create($node->class->toString())]; diff --git a/tests/Usages/NamespaceUsagesAllowInClassWithAttributesTest.php b/tests/Usages/NamespaceUsagesAllowInClassWithAttributesTest.php index 859eabf..bcb10d1 100644 --- a/tests/Usages/NamespaceUsagesAllowInClassWithAttributesTest.php +++ b/tests/Usages/NamespaceUsagesAllowInClassWithAttributesTest.php @@ -112,7 +112,7 @@ private function emulateHelperRules(Node $node): void } elseif ($node instanceof InClassMethodNode) { /** @phpstan-ignore phpstanApi.instanceofAssumption (🤞) */ $this->container->getByType(GetAttributesWhenInSignature::class)->unsetCurrentClassMethodName(); } elseif ($node instanceof Function_ && $node->namespacedName !== null) { - $this->container->getByType(GetAttributesWhenInSignature::class)->setCurrentFunctionName($node->namespacedName->toString()); + $this->container->getByType(GetAttributesWhenInSignature::class)->setCurrentFunctionName($node->namespacedName); } elseif ($node instanceof InFunctionNode) { /** @phpstan-ignore phpstanApi.instanceofAssumption (🤞) */ $this->container->getByType(GetAttributesWhenInSignature::class)->unsetCurrentFunctionName(); }