1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povils\PHPMND\Visitor; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node; |
6
|
|
|
use PhpParser\Node\Const_; |
7
|
|
|
use PhpParser\Node\Scalar; |
8
|
|
|
use PhpParser\Node\Scalar\DNumber; |
9
|
|
|
use PhpParser\Node\Scalar\LNumber; |
10
|
|
|
use PhpParser\NodeTraverser; |
11
|
|
|
use PhpParser\NodeVisitorAbstract; |
12
|
|
|
use Povils\PHPMND\Console\Option; |
13
|
|
|
use Povils\PHPMND\Extension\Extension; |
14
|
|
|
use Povils\PHPMND\Extension\FunctionAwareExtension; |
15
|
|
|
use Povils\PHPMND\FileReport; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class DetectorVisitor |
19
|
|
|
* |
20
|
|
|
* @package Povils\PHPMND |
21
|
|
|
*/ |
22
|
|
|
class DetectorVisitor extends NodeVisitorAbstract |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var FileReport |
26
|
|
|
*/ |
27
|
|
|
private $fileReport; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Option |
31
|
|
|
*/ |
32
|
|
|
private $option; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param FileReport $fileReport |
36
|
|
|
* @param Option $option |
37
|
|
|
*/ |
38
|
|
|
public function __construct(FileReport $fileReport, Option $option) |
39
|
|
|
{ |
40
|
|
|
$this->fileReport = $fileReport; |
41
|
|
|
$this->option = $option; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function enterNode(Node $node) |
48
|
|
|
{ |
49
|
|
|
if ($node instanceof Const_) { |
50
|
|
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var LNumber $node */ |
54
|
|
|
if ($this->isNumber($node) && false === $this->ignoreNumber($node)) { |
55
|
|
|
foreach ($this->option->getExtensions() as $extension) { |
56
|
|
|
if ($extension->extend($node) && false == $this->ignoreFunc($node, $extension)) { |
|
|
|
|
57
|
|
|
$this->fileReport->addEntry($node->getLine(), $node->value); |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Node $node |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
protected function isNumber(Node $node) |
73
|
|
|
{ |
74
|
|
|
return $node instanceof LNumber || $node instanceof DNumber; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param LNumber|DNumber|Scalar $node |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
private function ignoreNumber(Scalar $node) |
83
|
|
|
{ |
84
|
|
|
return in_array($node->value, $this->option->getIgnoreNumbers(), true); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Node $node |
89
|
|
|
* @param Extension $extension |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
private function ignoreFunc(Node $node, Extension $extension) |
94
|
|
|
{ |
95
|
|
|
if ($extension instanceof FunctionAwareExtension) { |
96
|
|
|
return $extension->ignoreFunc($node, $this->option->getIgnoreFuncs()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.