@@ -64,7 +64,7 @@ final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
64
64
private $ propertyLines = [];
65
65
66
66
/**
67
- * @psalm-var array<int, Return_|Expression|Assign|Array_>
67
+ * @psalm-var array<int, Function_|ClassMethod| Return_|Expression|Assign|Array_>
68
68
*/
69
69
private $ returns = [];
70
70
@@ -115,7 +115,7 @@ private function savePropertyLines(Node $node): void
115
115
116
116
private function computeReturns (): void
117
117
{
118
- foreach ($ this ->returns as $ node ) {
118
+ foreach (array_reverse ( $ this ->returns ) as $ node ) {
119
119
foreach (range ($ node ->getStartLine (), $ node ->getEndLine ()) as $ index ) {
120
120
if (isset ($ this ->executableLines [$ index ])) {
121
121
continue ;
@@ -133,14 +133,34 @@ private function computeReturns(): void
133
133
*/
134
134
private function getLines (NodeAbstract $ node , bool $ fromReturns ): array
135
135
{
136
- if ($ node instanceof Return_ ||
136
+ if ($ node instanceof Function_ ||
137
+ $ node instanceof ClassMethod ||
138
+ $ node instanceof Return_ ||
137
139
$ node instanceof Expression ||
138
140
$ node instanceof Assign ||
139
141
$ node instanceof Array_
140
142
) {
141
143
if (!$ fromReturns ) {
142
144
$ this ->returns [] = $ node ;
143
145
146
+ if ($ node instanceof ClassMethod && $ node ->name ->name === '__construct ' ) {
147
+ $ existsAPromotedProperty = false ;
148
+
149
+ foreach ($ node ->getParams () as $ param ) {
150
+ if (0 !== ($ param ->flags & Class_::VISIBILITY_MODIFIER_MASK )) {
151
+ $ existsAPromotedProperty = true ;
152
+
153
+ break ;
154
+ }
155
+ }
156
+
157
+ if ($ existsAPromotedProperty ) {
158
+ // Only the line with `function` keyword should be listed here
159
+ // but `nikic/php-parser` doesn't provide a way to fetch it
160
+ return range ($ node ->getStartLine (), $ node ->name ->getEndLine ());
161
+ }
162
+ }
163
+
144
164
return [];
145
165
}
146
166
@@ -151,6 +171,20 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
151
171
return [];
152
172
}
153
173
}
174
+
175
+ // empty function
176
+ if ($ node instanceof Function_) {
177
+ return [$ node ->getEndLine ()];
178
+ }
179
+
180
+ // empty method
181
+ if ($ node instanceof ClassMethod) {
182
+ if (null === $ node ->stmts ) { // method without body (interface prototype)
183
+ return [];
184
+ }
185
+
186
+ return [$ node ->getEndLine ()];
187
+ }
154
188
}
155
189
156
190
if ($ node instanceof Return_) {
@@ -183,34 +217,6 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
183
217
return [$ this ->getNodeStartLine ($ node ->dim )];
184
218
}
185
219
186
- if ($ node instanceof ClassMethod) {
187
- if ($ node ->name ->name !== '__construct ' ) {
188
- if ($ node ->stmts === []) {
189
- return [$ node ->getEndLine ()];
190
- }
191
-
192
- return [];
193
- }
194
-
195
- $ existsAPromotedProperty = false ;
196
-
197
- foreach ($ node ->getParams () as $ param ) {
198
- if (0 !== ($ param ->flags & Class_::VISIBILITY_MODIFIER_MASK )) {
199
- $ existsAPromotedProperty = true ;
200
-
201
- break ;
202
- }
203
- }
204
-
205
- if ($ existsAPromotedProperty ) {
206
- // Only the line with `function` keyword should be listed here
207
- // but `nikic/php-parser` doesn't provide a way to fetch it
208
- return range ($ node ->getStartLine (), $ node ->name ->getEndLine ());
209
- }
210
-
211
- return [];
212
- }
213
-
214
220
if ($ node instanceof MethodCall) {
215
221
return [$ this ->getNodeStartLine ($ node ->name )];
216
222
}
@@ -255,14 +261,6 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
255
261
return [$ this ->getNodeStartLine ($ node ->types [0 ])];
256
262
}
257
263
258
- if ($ node instanceof Function_) {
259
- if ($ node ->stmts === []) {
260
- return [$ node ->getEndLine ()];
261
- }
262
-
263
- return [];
264
- }
265
-
266
264
return [$ this ->getNodeStartLine ($ node )];
267
265
}
268
266
0 commit comments