-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
ValueNormalizer.php
95 lines (93 loc) · 4.15 KB
/
ValueNormalizer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare (strict_types=1);
namespace Rector\PhpAttribute\Value;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PhpAttribute\Exception\InvalidNestedAttributeException;
final class ValueNormalizer
{
/**
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(\Rector\Core\Php\PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @param mixed $value
* @return mixed[]|bool|float|int|\PhpParser\Node\Expr|string
*/
public function normalize($value)
{
if ($value instanceof \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode) {
return $this->normalizeDoctrineAnnotationTagValueNode($value);
}
if ($value instanceof \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode) {
return $this->normalizeConstrExprNode($value);
}
if ($value instanceof \Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode) {
return \array_map(function ($node) {
return $this->normalize($node);
}, $value->getValuesWithExplicitSilentAndWithoutQuotes());
}
if (\is_string($value) && \strpos($value, '::') !== \false) {
// class const fetch
[$class, $constant] = \explode('::', $value);
return new \PhpParser\Node\Expr\ClassConstFetch(new \PhpParser\Node\Name($class), $constant);
}
if ($value instanceof \PHPStan\PhpDocParser\Ast\Node) {
return (string) $value;
}
if (\is_array($value)) {
return \array_map(function ($item) {
return $this->normalize($item);
}, $value);
}
return $value;
}
private function normalizeDoctrineAnnotationTagValueNode(\Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode) : \PhpParser\Node\Expr\New_
{
// if PHP 8.0- throw exception
if (!$this->phpVersionProvider->isAtLeastPhpVersion(\Rector\Core\ValueObject\PhpVersionFeature::NEW_INITIALIZERS)) {
throw new \Rector\PhpAttribute\Exception\InvalidNestedAttributeException();
}
$resolveClass = $doctrineAnnotationTagValueNode->identifierTypeNode->getAttribute(\Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey::RESOLVED_CLASS);
return new \PhpParser\Node\Expr\New_(new \PhpParser\Node\Name\FullyQualified($resolveClass));
}
/**
* @return bool|float|int
*/
private function normalizeConstrExprNode(\PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode $constExprNode)
{
if ($constExprNode instanceof \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode) {
return (int) $constExprNode->value;
}
if ($constExprNode instanceof \PHPStan\Type\Constant\ConstantFloatType || $constExprNode instanceof \PHPStan\Type\Constant\ConstantBooleanType) {
return $constExprNode->getValue();
}
if ($constExprNode instanceof \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode) {
return \true;
}
if ($constExprNode instanceof \PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode) {
return \false;
}
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
}