Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 12 additions & 18 deletions src/Allowed/Allowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Reflector\Reflector;
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;
Expand All @@ -27,7 +27,7 @@ class Allowed

private Formatter $formatter;

private Reflector $reflector;
private ReflectionProvider $reflectionProvider;

private Identifier $identifier;

Expand All @@ -36,12 +36,12 @@ class Allowed

public function __construct(
Formatter $formatter,
Reflector $reflector,
ReflectionProvider $reflectionProvider,
Identifier $identifier,
AllowedPath $allowedPath
) {
$this->formatter = $formatter;
$this->reflector = $reflector;
$this->reflectionProvider = $reflectionProvider;
$this->identifier = $identifier;
$this->allowedPath = $allowedPath;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ private function getAttributes(Scope $scope): array
if (!$scope->isInClass()) {
return [];
}
return array_map(static fn($a) => $a->getName(), $scope->getClassReflection()->getNativeReflection()->getAttributes());
return array_map(static fn($a) => $a->getName(), $scope->getClassReflection()->getAttributes());
}


Expand All @@ -273,19 +273,13 @@ private function getAttributes(Scope $scope): array
private function getCallAttributes(?Node $node, Scope $scope): array
{
$function = $scope->getFunction();
if ($function instanceof MethodReflection) {
if (!$scope->isInClass()) {
return [];
}
return array_map(static fn($a) => $a->getName(), $scope->getClassReflection()->getNativeReflection()->getMethod($function->getName())->getAttributes());
} elseif ($function instanceof FunctionReflection) {
return array_map(static fn($a) => $a->getName(), $this->reflector->reflectFunction($function->getName())->getAttributes());
} elseif ($function === null) {
if ($node instanceof ClassMethod && $scope->isInClass()) {
return array_map(static fn($a) => $a->getName(), $scope->getClassReflection()->getNativeReflection()->getMethod($node->name->name)->getAttributes());
} elseif ($node instanceof Function_) {
return array_map(static fn($a) => $a->getName(), $this->reflector->reflectFunction(($node->namespacedName ?? $node->name)->toString())->getAttributes());
}
if ($function !== null) {
return array_map(static fn($a) => $a->getName(), $function->getAttributes());
} elseif ($node instanceof ClassMethod && $scope->isInClass()) {
return array_map(static fn($a) => $a->getName(), $scope->getClassReflection()->getNativeMethod($node->name->name)->getAttributes());
} elseif ($node instanceof Function_ && $node->namespacedName !== null) {
return array_map(static fn($a) => $a->getName(), $this->reflectionProvider->getFunction($node->namespacedName, $scope)->getAttributes());
} else {
// $scope->getFunction() is null in param/return type hints, use the enclosing function attributes stored by TypeHintContextVisitor
if ($node !== null) {
$names = $node->getAttribute(TypeHintContextVisitor::ATTRIBUTE_ENCLOSING_FUNCTION_ATTR_NAMES);
Expand Down
18 changes: 8 additions & 10 deletions src/Identifier/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Usages/NamespaceUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,7 +92,7 @@ public function processNode(Node $node, Scope $scope): array
}
}
$position = $this->getPosition($node);
} 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())];
Expand Down