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\Type\Common\UndefinedType; |
8
|
|
|
use Gskema\TypeSniff\Core\Type\TypeInterface; |
9
|
|
|
use PHP_CodeSniffer\Files\File; |
10
|
|
|
|
11
|
|
|
abstract class AbstractTypeSubject |
12
|
|
|
{ |
13
|
|
|
/** @var TypeInterface|null */ |
14
|
|
|
protected $docType; // null = missing in PHPDoc |
15
|
|
|
|
16
|
|
|
/** @var TypeInterface */ |
17
|
|
|
protected $fnType; |
18
|
|
|
|
19
|
|
|
/** @var TypeInterface|null */ |
20
|
|
|
protected $valueType; // null = could not be detected |
21
|
|
|
|
22
|
|
|
/** @var int|null */ |
23
|
|
|
protected $docTypeLine; // null = missing in PHPDoc |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
protected $fnTypeLine; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $name; // "parameter $param1", "property $prop1", "constant CONST1" |
30
|
|
|
|
31
|
|
|
/** @var DocBlock */ |
32
|
|
|
protected $docBlock; |
33
|
|
|
|
34
|
|
|
/** @var string[] */ |
35
|
|
|
protected $docTypeWarnings = []; |
36
|
|
|
|
37
|
|
|
/** @var string[] */ |
38
|
|
|
protected $fnTypeWarnings = []; |
39
|
|
|
|
40
|
6 |
|
public function __construct( |
41
|
|
|
?TypeInterface $docType, |
42
|
|
|
TypeInterface $fnType, |
43
|
|
|
?TypeInterface $valueType, |
44
|
|
|
?int $docTypeLine, |
45
|
|
|
int $fnTypeLine, |
46
|
|
|
string $name, |
47
|
|
|
DocBlock $docBlock |
48
|
|
|
) { |
49
|
6 |
|
$this->docType = $docType; |
50
|
6 |
|
$this->fnType = $fnType; |
51
|
6 |
|
$this->valueType = $valueType; |
52
|
6 |
|
$this->docTypeLine = $docTypeLine; |
53
|
6 |
|
$this->fnTypeLine = $fnTypeLine; |
54
|
6 |
|
$this->name = $name; |
55
|
6 |
|
$this->docBlock = $docBlock; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
6 |
|
public function getDocType(): ?TypeInterface |
59
|
|
|
{ |
60
|
6 |
|
return $this->docType; |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
public function getFnType(): TypeInterface |
64
|
|
|
{ |
65
|
6 |
|
return $this->fnType; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
public function getValueType(): ?TypeInterface |
69
|
|
|
{ |
70
|
6 |
|
return $this->valueType; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getDocTypeLine(): ?int |
74
|
|
|
{ |
75
|
|
|
return $this->docTypeLine; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getFnTypeLine(): int |
79
|
|
|
{ |
80
|
|
|
return $this->fnTypeLine; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getName(): string |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
public function getDocBlock(): DocBlock |
89
|
|
|
{ |
90
|
4 |
|
return $this->docBlock; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
|
|
public function getDocTypeWarnings(): array |
97
|
|
|
{ |
98
|
|
|
return $this->docTypeWarnings; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string[] |
103
|
|
|
*/ |
104
|
|
|
public function getFnTypeWarnings(): array |
105
|
|
|
{ |
106
|
|
|
return $this->fnTypeWarnings; |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
public function hasDefinedDocType(): bool |
110
|
|
|
{ |
111
|
6 |
|
return $this->docType && !($this->docType instanceof UndefinedType); |
112
|
|
|
} |
113
|
|
|
|
114
|
4 |
|
public function hasDefinedFnType(): bool |
115
|
|
|
{ |
116
|
4 |
|
return $this->fnType && !($this->fnType instanceof UndefinedType); |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
public function hasDefinedDocBlock(): bool |
120
|
|
|
{ |
121
|
6 |
|
return !($this->docBlock instanceof UndefinedDocBlock); |
122
|
|
|
} |
123
|
|
|
|
124
|
6 |
|
public function addDocTypeWarning(string $warning): void |
125
|
|
|
{ |
126
|
6 |
|
$this->docTypeWarnings[] = $warning; |
127
|
6 |
|
} |
128
|
|
|
|
129
|
4 |
|
public function addFnTypeWarning(string $warning): void |
130
|
|
|
{ |
131
|
4 |
|
$this->fnTypeWarnings[] = $warning; |
132
|
4 |
|
} |
133
|
|
|
|
134
|
6 |
|
public function writeWarningsTo(File $file, string $sniffCode): void |
135
|
|
|
{ |
136
|
6 |
|
foreach ($this->docTypeWarnings as $docTypeWarning) { |
137
|
6 |
|
$warning = str_replace(':subject:', $this->name, $docTypeWarning); |
138
|
6 |
|
$file->addWarningOnLine($warning, $this->docTypeLine ?? $this->fnTypeLine, $sniffCode); |
139
|
|
|
} |
140
|
|
|
|
141
|
6 |
|
foreach ($this->fnTypeWarnings as $fnTypeWarning) { |
142
|
4 |
|
$warning = str_replace(':subject:', $this->name, $fnTypeWarning); |
143
|
4 |
|
$file->addWarningOnLine($warning, $this->fnTypeLine, $sniffCode); |
144
|
|
|
} |
145
|
6 |
|
} |
146
|
|
|
} |
147
|
|
|
|