/
InterfaceAnalyzer.php
182 lines (153 loc) · 6.37 KB
/
InterfaceAnalyzer.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Psalm\Internal\Analyzer;
use InvalidArgumentException;
use LogicException;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\FileManipulation;
use Psalm\Internal\Analyzer\Statements\Expression\ClassConstAnalyzer;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Issue\ParseError;
use Psalm\Issue\UndefinedInterface;
use Psalm\IssueBuffer;
use UnexpectedValueException;
use function strtolower;
/**
* @internal
*/
class InterfaceAnalyzer extends ClassLikeAnalyzer
{
public function __construct(
PhpParser\Node\Stmt\Interface_ $interface,
SourceAnalyzer $source,
string $fq_interface_name
) {
parent::__construct($interface, $source, $fq_interface_name);
}
public function analyze(): void
{
if (!$this->class instanceof PhpParser\Node\Stmt\Interface_) {
throw new LogicException('Something went badly wrong');
}
$interface_context = new Context($this->fq_class_name);
$project_analyzer = $this->file_analyzer->project_analyzer;
$codebase = $project_analyzer->getCodebase();
$config = $project_analyzer->getConfig();
if ($this->class->extends) {
foreach ($this->class->extends as $extended_interface) {
$extended_interface_name = self::getFQCLNFromNameObject(
$extended_interface,
$this->getAliases()
);
$parent_reference_location = new CodeLocation($this, $extended_interface);
if (!$codebase->classOrInterfaceExists(
$extended_interface_name,
$parent_reference_location
)) {
// we should not normally get here
return;
}
try {
$extended_interface_storage = $codebase->classlike_storage_provider->get($extended_interface_name);
} catch (InvalidArgumentException $e) {
continue;
}
if (!$extended_interface_storage->is_interface) {
$code_location = new CodeLocation(
$this,
$extended_interface
);
IssueBuffer::maybeAdd(
new UndefinedInterface(
$extended_interface_name . ' is not an interface',
$code_location,
$extended_interface_name
),
$this->getSuppressedIssues()
);
}
if ($codebase->store_node_types && $extended_interface_name) {
$bounds = $parent_reference_location->getSelectionBounds();
$codebase->analyzer->addOffsetReference(
$this->getFilePath(),
$bounds[0],
$bounds[1],
$extended_interface_name
);
}
}
}
$fq_interface_name = $this->getFQCLN();
if (!$fq_interface_name) {
throw new UnexpectedValueException('bad');
}
$class_storage = $codebase->classlike_storage_provider->get($fq_interface_name);
foreach ($class_storage->attributes as $attribute) {
AttributeAnalyzer::analyze(
$this,
$attribute,
$class_storage->suppressed_issues + $this->getSuppressedIssues(),
1,
$class_storage
);
}
$member_stmts = [];
foreach ($this->class->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod) {
$method_analyzer = new MethodAnalyzer($stmt, $this);
$type_provider = new NodeDataProvider();
$method_analyzer->analyze(new Context($this->getFQCLN()), $type_provider);
$actual_method_id = $method_analyzer->getMethodId();
if ($stmt->name->name !== '__construct'
&& $config->reportIssueInFile('InvalidReturnType', $this->getFilePath())
) {
ClassAnalyzer::analyzeClassMethodReturnType(
$stmt,
$method_analyzer,
$this,
$type_provider,
$codebase,
$class_storage,
$fq_interface_name,
$actual_method_id,
$actual_method_id,
false
);
}
} elseif ($stmt instanceof PhpParser\Node\Stmt\Property) {
IssueBuffer::add(
new ParseError(
'Interfaces cannot have properties',
new CodeLocation($this, $stmt)
)
);
return;
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
$member_stmts[] = $stmt;
foreach ($stmt->consts as $const) {
$const_id = strtolower($this->fq_class_name) . '::' . $const->name;
foreach ($codebase->class_constants_to_rename as $original_const_id => $new_const_name) {
if ($const_id === $original_const_id) {
$file_manipulations = [
new FileManipulation(
(int) $const->name->getAttribute('startFilePos'),
(int) $const->name->getAttribute('endFilePos') + 1,
$new_const_name
)
];
FileManipulationBuffer::add(
$this->getFilePath(),
$file_manipulations
);
}
}
}
}
}
$statements_analyzer = new StatementsAnalyzer($this, new NodeDataProvider());
$statements_analyzer->analyze($member_stmts, $interface_context, null, true);
ClassConstAnalyzer::analyze($this->storage, $this->getCodebase());
}
}