Skip to content

Commit db61a96

Browse files
committed
fix empty method with comment
1 parent 9b718c5 commit db61a96

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
6464
private $propertyLines = [];
6565

6666
/**
67-
* @psalm-var array<int, Return_|Expression|Assign|Array_>
67+
* @psalm-var array<int, Function_|ClassMethod|Return_|Expression|Assign|Array_>
6868
*/
6969
private $returns = [];
7070

@@ -115,7 +115,7 @@ private function savePropertyLines(Node $node): void
115115

116116
private function computeReturns(): void
117117
{
118-
foreach ($this->returns as $node) {
118+
foreach (array_reverse($this->returns) as $node) {
119119
foreach (range($node->getStartLine(), $node->getEndLine()) as $index) {
120120
if (isset($this->executableLines[$index])) {
121121
continue;
@@ -133,14 +133,34 @@ private function computeReturns(): void
133133
*/
134134
private function getLines(NodeAbstract $node, bool $fromReturns): array
135135
{
136-
if ($node instanceof Return_ ||
136+
if ($node instanceof Function_ ||
137+
$node instanceof ClassMethod ||
138+
$node instanceof Return_ ||
137139
$node instanceof Expression ||
138140
$node instanceof Assign ||
139141
$node instanceof Array_
140142
) {
141143
if (!$fromReturns) {
142144
$this->returns[] = $node;
143145

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+
144164
return [];
145165
}
146166

@@ -151,6 +171,20 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
151171
return [];
152172
}
153173
}
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+
}
154188
}
155189

156190
if ($node instanceof Return_) {
@@ -183,34 +217,6 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
183217
return [$this->getNodeStartLine($node->dim)];
184218
}
185219

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-
214220
if ($node instanceof MethodCall) {
215221
return [$this->getNodeStartLine($node->name)];
216222
}
@@ -255,14 +261,6 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
255261
return [$this->getNodeStartLine($node->types[0])];
256262
}
257263

258-
if ($node instanceof Function_) {
259-
if ($node->stmts === []) {
260-
return [$node->getEndLine()];
261-
}
262-
263-
return [];
264-
}
265-
266264
return [$this->getNodeStartLine($node)];
267265
}
268266

tests/_files/source_with_multiline_constant_return.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,14 @@ public function unaryMinusNowdoc(): float
499499
function
500500
emptyMethod
501501
(
502-
// empty method with comment
503502
)
504503
:
505504
void
506505
{
507506
}
507+
508+
public function emptyMethodWithComment(): void
509+
{
510+
// empty method with comment
511+
}
508512
}

tests/tests/Data/RawCodeCoverageDataTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): vo
531531
469,
532532
481,
533533
492,
534-
507,
534+
506,
535+
511,
535536
],
536537
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
537538
);

0 commit comments

Comments
 (0)