1 | <?php |
||
26 | class DetectorVisitor extends NodeVisitorAbstract |
||
27 | { |
||
28 | /** |
||
29 | * @var FileReport |
||
30 | */ |
||
31 | private $fileReport; |
||
32 | |||
33 | /** |
||
34 | * @var Option |
||
35 | */ |
||
36 | private $option; |
||
37 | |||
38 | public function __construct(FileReport $fileReport, Option $option) |
||
39 | { |
||
40 | $this->fileReport = $fileReport; |
||
41 | $this->option = $option; |
||
42 | } |
||
43 | |||
44 | public function enterNode(Node $node): ?int |
||
45 | { |
||
46 | if ($this->isIgnoreableConst($node)) { |
||
47 | if ($this->checkNameContainsLanguage( |
||
48 | $node->name->name, |
||
|
|||
49 | $node->value->value ?? 0 |
||
50 | )) { |
||
51 | $this->fileReport->addEntry($node->getLine(), $node->value->value); |
||
52 | } |
||
53 | |||
54 | return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
||
55 | } |
||
56 | |||
57 | /** @var LNumber|DNumber|String_ $scalar */ |
||
58 | $scalar = $node; |
||
59 | if ($this->hasSign($scalar)) { |
||
60 | $node = $scalar->getAttribute('parent'); |
||
61 | if ($this->isMinus($node)) { |
||
62 | $scalar->value = -$scalar->value; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if ($this->isNumber($scalar) || $this->isString($scalar)) { |
||
67 | |||
68 | if ($this->checkNameContainsLanguage( |
||
69 | $scalar->getAttribute('parent')->var->name ?? '', |
||
70 | $scalar->value |
||
71 | )) { |
||
72 | $this->fileReport->addEntry($node->getLine(), $scalar->value); |
||
73 | } |
||
74 | |||
75 | foreach ($this->option->getExtensions() as $extension) { |
||
76 | $extension->setOption($this->option); |
||
77 | if ($extension->extend($node)) { |
||
78 | $this->fileReport->addEntry($scalar->getLine(), $scalar->value); |
||
79 | |||
80 | return null; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return null; |
||
86 | } |
||
87 | |||
88 | private function isIgnoreableConst(Node $node): bool |
||
89 | { |
||
90 | return $node instanceof Const_ && |
||
91 | ($this->isNumber($node->value) || $this->isString($node->value)); |
||
92 | } |
||
93 | |||
94 | private function isNumber(Node $node): bool |
||
95 | { |
||
96 | $isNumber = ( |
||
97 | $node instanceof LNumber || |
||
98 | $node instanceof DNumber || |
||
99 | $this->isValidNumeric($node) |
||
100 | ); |
||
101 | |||
102 | return $isNumber && false === $this->ignoreNumber($node); |
||
103 | } |
||
104 | |||
105 | private function isString(Node $node): bool |
||
106 | { |
||
107 | return $this->option->includeStrings() && $node instanceof String_ && false === $this->ignoreString($node); |
||
108 | } |
||
109 | |||
110 | private function ignoreNumber(Node $node): bool |
||
114 | |||
115 | private function ignoreString(Node $node): bool |
||
116 | { |
||
117 | return in_array($node->value, $this->option->getIgnoreStrings(), true); |
||
118 | } |
||
119 | |||
120 | private function hasSign(Node $node): bool |
||
125 | |||
126 | private function isMinus(Node $node): bool |
||
130 | |||
131 | private function isValidNumeric(Node $node): bool |
||
138 | |||
139 | /** |
||
140 | * @param string $name |
||
141 | * @param string|int $value |
||
142 | * @return bool |
||
143 | */ |
||
144 | private function checkNameContainsLanguage(string $name, $value): bool |
||
164 | } |
||
165 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: