-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
EnumParamAnalyzer.php
70 lines (57 loc) · 2.03 KB
/
EnumParamAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace Rector\Php80\NodeAnalyzer;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
/**
* Detects enum-like params, e.g.
* Direction::*
*/
final class EnumParamAnalyzer
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
) {
}
public function matchParameterClassName(ParameterReflection $parameterReflection, PhpDocInfo $phpDocInfo): ?string
{
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($parameterReflection->getName());
if (! $paramTagValueNode instanceof ParamTagValueNode) {
return null;
}
$className = $this->resolveClassFromConstType($paramTagValueNode->type);
if ($className === null) {
return null;
}
if (! $this->reflectionProvider->hasClass($className)) {
return null;
}
return $className;
}
public function matchReturnClassName(PhpDocInfo $phpDocInfo): ?string
{
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
return null;
}
return $this->resolveClassFromConstType($returnTagValueNode->type);
}
private function resolveClassFromConstType(TypeNode $typeNode): ?string
{
if (! $typeNode instanceof ConstTypeNode) {
return null;
}
if (! $typeNode->constExpr instanceof ConstFetchNode) {
return null;
}
$constExpr = $typeNode->constExpr;
return $constExpr->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
}
}