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
44 changes: 20 additions & 24 deletions src/Allowed/Allowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +26,7 @@ class Allowed

private Formatter $formatter;

private Reflector $reflector;
private ReflectionProvider $reflectionProvider;

private Identifier $identifier;

Expand All @@ -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;
Expand Down Expand Up @@ -203,7 +201,7 @@ private function hasAllowedParamsInAllowed(Scope $scope, ?array $args, Disallowe


/**
* @param list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute> $attributes
* @param list<AttributeReflection> $attributes
* @param list<string> $allowConfig
* @return bool
*/
Expand Down Expand Up @@ -247,32 +245,29 @@ private function getArgType(array $args, Scope $scope, Param $param): ?Type

/**
* @param Scope $scope
* @return list<FakeReflectionAttribute>|list<ReflectionAttribute>
* @return list<AttributeReflection>
*/
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<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute>
* @return list<AttributeReflection>
*/
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;
Expand All @@ -284,16 +279,17 @@ private function getCallAttributes(?Node $node, Scope $scope): array

/**
* @param Scope $scope
* @return list<FakeReflectionAttribute>|list<ReflectionAttribute>
* @return list<AttributeReflection>
*/
private function getAllMethodAttributes(Scope $scope): array
{
if (!$scope->isInClass()) {
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);
}
Expand Down
27 changes: 13 additions & 14 deletions src/Allowed/GetAttributesWhenInSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute>|null
* @return list<AttributeReflection>|null
*/
public function get(Scope $scope): ?array
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/HelperRules/SetCurrentFunctionNameHelperRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
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 @@ -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())];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Loading