Skip to content

Fix one-line method for anonymous classes #1021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function enterNode(Node $node): void
);

if ($hasEmptyBody) {
if ($node->getEndLine() === $node->getStartLine()) {
if ($node->getEndLine() === $node->getStartLine() && isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
return;
}

Expand Down
23 changes: 17 additions & 6 deletions tests/_files/source_for_branched_exec_lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,18 @@ public function myMethod()
{
return; // +3
}
} // -3

public function m1(): void {} // +1
Copy link
Contributor

@mvorisek mvorisek Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test for one-line method in non-anonymous class as well (at least in this test file it is not tested currently)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

public function m2(): void {
} // +1
public function m3(): void
{} // +1
} // -6
; // 0
}
public function withComments()
{
$var = 1; // +4
$var = 1; // +7
/** @var int $var */
$var = 2; // +1
// C3
Expand Down Expand Up @@ -582,7 +588,7 @@ public function multilineVoid(
trait MyTrait
{
public function myTrait()
{} // +5
{} // +6
}

abstract class MyAbstractClass implements MyInterface
Expand All @@ -594,7 +600,12 @@ final class MyFinalClass extends MyAbstractClass
'foo' => 'bar',
];
private string $var;
public function myMethod(): void
public function m1(): void {} // +4
public function m2(): void {
} // +1
public function m3(): void
{} // +1
public function m4(): void
{
} // +4
}
} // +1
}
2 changes: 1 addition & 1 deletion tests/_files/source_for_branched_exec_lines_php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function neverReturn(): never;
class NewInInit_NoInit
{
public function __construct(private DateTimeInterface $dateTime) {
} // +1
} // +3
public function noinit(DateTimeInterface $dateTime) {
} // +1
}
Expand Down