|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gskema\TypeSniff\Inspection\Subject; |
|
4
|
|
|
|
|
5
|
|
|
use Gskema\TypeSniff\Core\DocBlock\DocBlock; |
|
6
|
|
|
use Gskema\TypeSniff\Core\DocBlock\UndefinedDocBlock; |
|
7
|
|
|
use Gskema\TypeSniff\Core\SniffHelper; |
|
8
|
|
|
use Gskema\TypeSniff\Core\Type\Common\UndefinedType; |
|
9
|
|
|
use Gskema\TypeSniff\Core\Type\TypeInterface; |
|
10
|
|
|
use PHP_CodeSniffer\Files\File; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @see AbstractTypeSubjectTest |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractTypeSubject |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TypeInterface|null */ |
|
18
|
|
|
protected $docType; // null = missing in PHPDoc |
|
19
|
|
|
|
|
20
|
|
|
/** @var TypeInterface */ |
|
21
|
|
|
protected $fnType; |
|
22
|
|
|
|
|
23
|
|
|
/** @var TypeInterface|null */ |
|
24
|
|
|
protected $valueType; // null = could not be detected |
|
25
|
|
|
|
|
26
|
|
|
/** @var int|null */ |
|
27
|
|
|
protected $docTypeLine; // null = missing in PHPDoc |
|
28
|
|
|
|
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
protected $fnTypeLine; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
protected $name; // "parameter $param1", "property $prop1", "constant CONST1" |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
protected $id; // TestClass::method1(), etc. |
|
37
|
|
|
|
|
38
|
|
|
/** @var DocBlock */ |
|
39
|
|
|
protected $docBlock; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string[] */ |
|
42
|
|
|
protected $docTypeWarnings = []; |
|
43
|
|
|
|
|
44
|
|
|
/** @var string[] */ |
|
45
|
|
|
protected $fnTypeWarnings = []; |
|
46
|
|
|
|
|
47
|
14 |
|
/** @var string[] */ |
|
48
|
|
|
protected $attributeNames = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param TypeInterface|null $docType |
|
52
|
|
|
* @param TypeInterface $fnType |
|
53
|
|
|
* @param TypeInterface|null $valueType |
|
54
|
|
|
* @param int|null $docTypeLine |
|
55
|
|
|
* @param int $fnTypeLine |
|
56
|
|
|
* @param string $name |
|
57
|
14 |
|
* @param DocBlock $docBlock |
|
58
|
14 |
|
* @param string[] $attributeNames |
|
59
|
14 |
|
* @param string $id |
|
60
|
14 |
|
*/ |
|
61
|
14 |
|
public function __construct( |
|
62
|
14 |
|
?TypeInterface $docType, |
|
63
|
14 |
|
TypeInterface $fnType, |
|
64
|
14 |
|
?TypeInterface $valueType, |
|
65
|
14 |
|
?int $docTypeLine, |
|
66
|
|
|
int $fnTypeLine, |
|
67
|
14 |
|
string $name, |
|
68
|
|
|
DocBlock $docBlock, |
|
69
|
14 |
|
array $attributeNames, |
|
70
|
|
|
string $id |
|
71
|
|
|
) { |
|
72
|
14 |
|
$this->docType = $docType; |
|
73
|
|
|
$this->fnType = $fnType; |
|
74
|
14 |
|
$this->valueType = $valueType; |
|
75
|
|
|
$this->docTypeLine = $docTypeLine; |
|
76
|
|
|
$this->fnTypeLine = $fnTypeLine; |
|
77
|
14 |
|
$this->name = $name; |
|
78
|
|
|
$this->docBlock = $docBlock; |
|
79
|
14 |
|
$this->attributeNames = $attributeNames; |
|
80
|
|
|
$this->id = $id; |
|
81
|
|
|
} |
|
82
|
1 |
|
|
|
83
|
|
|
public function getDocType(): ?TypeInterface |
|
84
|
1 |
|
{ |
|
85
|
|
|
return $this->docType; |
|
86
|
|
|
} |
|
87
|
1 |
|
|
|
88
|
|
|
public function getFnType(): TypeInterface |
|
89
|
1 |
|
{ |
|
90
|
|
|
return $this->fnType; |
|
91
|
|
|
} |
|
92
|
1 |
|
|
|
93
|
|
|
public function getValueType(): ?TypeInterface |
|
94
|
1 |
|
{ |
|
95
|
|
|
return $this->valueType; |
|
96
|
|
|
} |
|
97
|
1 |
|
|
|
98
|
|
|
public function getDocTypeLine(): ?int |
|
99
|
1 |
|
{ |
|
100
|
|
|
return $this->docTypeLine; |
|
101
|
|
|
} |
|
102
|
11 |
|
|
|
103
|
|
|
public function getFnTypeLine(): int |
|
104
|
11 |
|
{ |
|
105
|
|
|
return $this->fnTypeLine; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getName(): string |
|
109
|
|
|
{ |
|
110
|
1 |
|
return $this->name; |
|
111
|
|
|
} |
|
112
|
1 |
|
|
|
113
|
|
|
public function getId(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->id; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
public function getDocBlock(): DocBlock |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->docBlock; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
13 |
|
/** |
|
124
|
|
|
* @return string[] |
|
125
|
13 |
|
*/ |
|
126
|
|
|
public function getDocTypeWarnings(): array |
|
127
|
|
|
{ |
|
128
|
8 |
|
return $this->docTypeWarnings; |
|
129
|
|
|
} |
|
130
|
8 |
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string[] |
|
133
|
13 |
|
*/ |
|
134
|
|
|
public function getFnTypeWarnings(): array |
|
135
|
13 |
|
{ |
|
136
|
|
|
return $this->fnTypeWarnings; |
|
137
|
|
|
} |
|
138
|
12 |
|
|
|
139
|
|
|
public function hasDefinedDocType(): bool |
|
140
|
12 |
|
{ |
|
141
|
12 |
|
return $this->docType && !($this->docType instanceof UndefinedType); |
|
142
|
|
|
} |
|
143
|
8 |
|
|
|
144
|
|
|
public function hasDefinedFnType(): bool |
|
145
|
8 |
|
{ |
|
146
|
8 |
|
return $this->fnType && !($this->fnType instanceof UndefinedType); |
|
147
|
|
|
} |
|
148
|
13 |
|
|
|
149
|
|
|
public function hasDefinedDocBlock(): bool |
|
150
|
13 |
|
{ |
|
151
|
|
|
return !($this->docBlock instanceof UndefinedDocBlock); |
|
152
|
13 |
|
} |
|
153
|
13 |
|
|
|
154
|
11 |
|
public function addDocTypeWarning(string $warning): void |
|
155
|
11 |
|
{ |
|
156
|
|
|
$this->docTypeWarnings[] = $warning; |
|
157
|
|
|
} |
|
158
|
13 |
|
|
|
159
|
7 |
|
public function addFnTypeWarning(string $warning): void |
|
160
|
7 |
|
{ |
|
161
|
|
|
$this->fnTypeWarnings[] = $warning; |
|
162
|
13 |
|
} |
|
163
|
|
|
|
|
164
|
10 |
|
public function writeViolationsTo(File $file, string $sniffCode, string $reportType, bool $addViolationId): void |
|
165
|
|
|
{ |
|
166
|
10 |
|
$originId = $addViolationId ? $this->getId() : null; |
|
167
|
|
|
|
|
168
|
|
|
$ucName = ucfirst($this->name); |
|
169
|
|
|
foreach ($this->docTypeWarnings as $docTypeWarning) { |
|
170
|
|
|
$warning = str_replace([':subject:', ':Subject:'], [$this->name, $ucName], $docTypeWarning); |
|
171
|
|
|
SniffHelper::addViolation($file, $warning, $this->docTypeLine ?? $this->fnTypeLine, $sniffCode, $reportType, $originId); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
foreach ($this->fnTypeWarnings as $fnTypeWarning) { |
|
175
|
|
|
$warning = str_replace([':subject:', ':Subject:'], [$this->name, $ucName], $fnTypeWarning); |
|
176
|
|
|
SniffHelper::addViolation($file, $warning, $this->fnTypeLine, $sniffCode, $reportType, $originId); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function hasDocTypeTag(): bool |
|
181
|
|
|
{ |
|
182
|
|
|
return null !== $this->docType; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function hasAttribute(string $attributeName): bool |
|
186
|
|
|
{ |
|
187
|
|
|
return in_array($attributeName, $this->attributeNames); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|