Skip to content

Commit 1006365

Browse files
committed
Use the new getAttributes() API to get attributes
The API is available starting with PHPStan 2.1.3 https://github.com/phpstan/phpstan/releases/tag/2.1.3 so I'll need to decide when to merge this and when or how to remove support for earlier versions. Also require nikic/php-parser 5.0+ because first PHPStan 2.x requires and uses it and second, Function_::namespacedName is not available in earlier versions.
1 parent d146959 commit 1006365

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
},
2323
"require": {
2424
"php": "^7.4 || ^8.0",
25-
"phpstan/phpstan": "^1.12.6 || ^2.0"
25+
"phpstan/phpstan": "^2.1.3"
2626
},
2727
"require-dev": {
2828
"nette/neon": "^3.3.1",
29-
"nikic/php-parser": "^4.13 || ^5.0",
29+
"nikic/php-parser": "^5.0",
3030
"phpunit/phpunit": "^8.5.14 || ^10.1 || ^11.0 || ^12.0",
3131
"php-parallel-lint/php-parallel-lint": "^1.2",
3232
"php-parallel-lint/php-console-highlighter": "^1.0",

src/Allowed/Allowed.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PhpParser\Node\Stmt\Function_;
1010
use PHPStan\Analyser\Scope;
11-
use PHPStan\BetterReflection\Reflection\Adapter\FakeReflectionAttribute;
12-
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionAttribute;
13-
use PHPStan\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute;
14-
use PHPStan\BetterReflection\Reflector\Reflector;
11+
use PHPStan\Reflection\AttributeReflection;
1512
use PHPStan\Reflection\FunctionReflection;
1613
use PHPStan\Reflection\MethodReflection;
14+
use PHPStan\Reflection\ReflectionProvider;
1715
use PHPStan\Type\Type;
1816
use PHPStan\Type\UnionType;
1917
use Spaze\PHPStan\Rules\Disallowed\Disallowed;
@@ -28,7 +26,7 @@ class Allowed
2826

2927
private Formatter $formatter;
3028

31-
private Reflector $reflector;
29+
private ReflectionProvider $reflectionProvider;
3230

3331
private Identifier $identifier;
3432

@@ -37,12 +35,12 @@ class Allowed
3735

3836
public function __construct(
3937
Formatter $formatter,
40-
Reflector $reflector,
38+
ReflectionProvider $reflectionProvider,
4139
Identifier $identifier,
4240
AllowedPath $allowedPath
4341
) {
4442
$this->formatter = $formatter;
45-
$this->reflector = $reflector;
43+
$this->reflectionProvider = $reflectionProvider;
4644
$this->identifier = $identifier;
4745
$this->allowedPath = $allowedPath;
4846
}
@@ -199,7 +197,7 @@ private function hasAllowedParamsInAllowed(Scope $scope, ?array $args, Disallowe
199197

200198

201199
/**
202-
* @param list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute> $attributes
200+
* @param list<AttributeReflection> $attributes
203201
* @param list<string> $allowConfig
204202
* @return bool
205203
*/
@@ -243,49 +241,46 @@ private function getArgType(array $args, Scope $scope, Param $param): ?Type
243241

244242
/**
245243
* @param Scope $scope
246-
* @return list<FakeReflectionAttribute>|list<ReflectionAttribute>
244+
* @return list<AttributeReflection>
247245
*/
248246
private function getAttributes(Scope $scope): array
249247
{
250-
return $scope->isInClass() ? $scope->getClassReflection()->getNativeReflection()->getAttributes() : [];
248+
return $scope->isInClass() ? $scope->getClassReflection()->getAttributes() : [];
251249
}
252250

253251

254252
/**
255253
* @param Node|null $node
256254
* @param Scope $scope
257-
* @return list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute>
255+
* @return list<AttributeReflection>
258256
*/
259257
private function getCallAttributes(?Node $node, Scope $scope): array
260258
{
261259
$function = $scope->getFunction();
262-
if ($function instanceof MethodReflection) {
263-
return $scope->isInClass() ? $scope->getClassReflection()->getNativeReflection()->getMethod($function->getName())->getAttributes() : [];
264-
} elseif ($function instanceof FunctionReflection) {
265-
return $this->reflector->reflectFunction($function->getName())->getAttributes();
266-
} elseif ($function === null) {
267-
if ($node instanceof ClassMethod && $scope->isInClass()) {
268-
return $scope->getClassReflection()->getNativeReflection()->getMethod($node->name->name)->getAttributes();
269-
} elseif ($node instanceof Function_) {
270-
return $this->reflector->reflectFunction($node->name->name)->getAttributes();
271-
}
260+
if ($function !== null) {
261+
return $function->getAttributes();
262+
} elseif ($node instanceof ClassMethod && $scope->isInClass()) {
263+
return $scope->getClassReflection()->getNativeMethod($node->name->name)->getAttributes();
264+
} elseif ($node instanceof Function_ && $node->namespacedName !== null) {
265+
return $this->reflectionProvider->getFunction($node->namespacedName, $scope)->getAttributes();
272266
}
273267
return [];
274268
}
275269

276270

277271
/**
278272
* @param Scope $scope
279-
* @return list<FakeReflectionAttribute>|list<ReflectionAttribute>
273+
* @return list<AttributeReflection>
280274
*/
281275
private function getAllMethodAttributes(Scope $scope): array
282276
{
283277
if (!$scope->isInClass()) {
284278
return [];
285279
}
286280
$attributes = [];
287-
foreach ($scope->getClassReflection()->getNativeReflection()->getMethods() as $method) {
288-
$methodAttributes = $method->getAttributes();
281+
$classReflection = $scope->getClassReflection();
282+
foreach ($classReflection->getNativeReflection()->getMethods() as $method) {
283+
$methodAttributes = $classReflection->getNativeMethod($method->getName())->getAttributes();
289284
if ($methodAttributes !== []) {
290285
$attributes = array_merge($attributes, $methodAttributes);
291286
}

0 commit comments

Comments
 (0)