-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathFileReportGenerator.php
More file actions
140 lines (115 loc) · 3.73 KB
/
FileReportGenerator.php
File metadata and controls
140 lines (115 loc) · 3.73 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace Povils\PHPMND;
use PhpParser\Node;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use Povils\PHPMND\Console\Option;
use Povils\PHPMND\PhpParser\Visitor\ParentConnector;
use Symfony\Component\Finder\SplFileInfo;
class FileReportGenerator
{
/**
* @var SplFileInfo
*/
private $file;
/**
* @var Option
*/
private $option;
private $baseLookup = [
2 => 'b',
8 => 'o',
16 => 'x',
];
public function __construct(SplFileInfo $file, Option $option)
{
$this->file = $file;
$this->option = $option;
}
public function detect(Node $node): iterable
{
if ($this->isIgnorableConst($node)) {
return;
}
/** @var LNumber|DNumber|String_ $scalar */
$scalar = $node;
if ($this->hasSign($node)) {
$node = ParentConnector::findParent($node);
if ($this->isMinus($node)) {
if (!isset($scalar->value)) {
return;
}
$scalar->value = -$scalar->value;
}
}
if ($this->isNumber($scalar) || $this->isString($scalar)) {
foreach ($this->option->getExtensions() as $extension) {
$extension->setOption($this->option);
if ($extension->extend($node)) {
if ((
$scalar instanceof LNumber &&
$scalar->getAttribute('kind') !== LNumber::KIND_DEC
)) {
$scalar->value = sprintf(
'0%s%d',
$this->baseLookup[$scalar->getAttribute('kind')],
base_convert(
(string) $scalar->value,
LNumber::KIND_DEC,
$scalar->getAttribute('kind')
)
);
}
yield new DetectionResult($this->file, $scalar->getLine(), $scalar->value);
}
}
}
}
private function isIgnorableConst(Node $node): bool
{
return $node instanceof Const_ &&
($this->isNumber($node->value) || $this->isString($node->value));
}
private function isNumber(Node $node): bool
{
$isNumber = (
$node instanceof LNumber ||
$node instanceof DNumber ||
$this->isValidNumeric($node)
);
return $isNumber && $this->ignoreNumber($node) === false;
}
private function isString(Node $node): bool
{
return $this->option->includeStrings() && $node instanceof String_ && $this->ignoreString($node) === false;
}
private function ignoreNumber(Node $node): bool
{
return in_array($node->value, $this->option->getIgnoreNumbers(), true);
}
private function ignoreString(Node $node): bool
{
return in_array($node->value, $this->option->getIgnoreStrings(), true);
}
private function hasSign(Node $node): bool
{
$parentNode = ParentConnector::findParent($node);
return $parentNode instanceof UnaryMinus || $parentNode instanceof UnaryPlus;
}
private function isMinus(Node $node): bool
{
return $node instanceof UnaryMinus;
}
private function isValidNumeric(Node $node): bool
{
return $this->option->includeNumericStrings()
&& isset($node->value)
&& is_numeric($node->value)
&& $this->ignoreString($node) === false;
}
}