Skip to content

Fix new in initializers #1015

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
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
9 changes: 9 additions & 0 deletions src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ public function enterNode(Node $node): void
$node instanceof Node\Stmt\ClassMethod ||
$node instanceof Node\Expr\Closure ||
$node instanceof Node\Stmt\Trait_) {
if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
foreach ($node->getParams() as $param) {
foreach (range($param->getStartLine(), $param->getEndLine()) as $line) {
$this->unsets[$line] = true;
}
}
unset($this->unsets[$node->getEndLine()]);
}

$isConcreteClassLike = $node instanceof Node\Stmt\Enum_ || $node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_;

if (null !== $node->stmts) {
Expand Down
51 changes: 51 additions & 0 deletions tests/_files/source_for_branched_exec_lines_php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,54 @@ interface MyIntersection
public function check(MyIntOne&MyIntTwo $intersection);
public function neverReturn(): never;
}

// New in initializers
class NewInInit_NoInit
{
public function __construct(private DateTimeInterface $dateTime) {
} // +1
public function noinit(DateTimeInterface $dateTime) {
} // +1
}
class NewInInit_OneLineNewLine
{
public function __construct(private DateTimeInterface $dateTime = new DateTime()) {
} // +1
public function onelinenewline(DateTimeInterface $dateTime = new DateTime()) {
} // +2
}
class NewInInit_OneLineSameLine
{
public function __construct(private DateTimeInterface $dateTime = new DateTime()) {} // +2
public function onelinesameline(DateTimeInterface $dateTime = new DateTime()) {} // +1
}
class NewInInit_MultiLine
{
public function __construct(
private
DateTimeInterface
$dateTime
=
new
DateTime()
,
private
bool
$var
=
true
)
{
} // +1
public function multiline(
DateTimeInterface $dateTime = new DateTime()
) {
} // +2
}
function newInInit_OneLineNewLine(DateTimeInterface $dateTime = new DateTime()) {
} // +2
function newInInit_OneLineSameLine(DateTimeInterface $dateTime = new DateTime()) {} // +2
function newInInit_multiline(
DateTimeInterface $dateTime = new DateTime()
) {
} // +1
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function testExecutableLinesAreGroupedByBranch(): void
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines.php');
}

#[RequiresPhp('8.1.*')]
#[RequiresPhp('>=8.1')]
public function testExecutableLinesAreGroupedByBranchPhp81(): void
{
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php81.php');
}

#[RequiresPhp('8.2.*')]
#[RequiresPhp('>=8.2')]
public function testExecutableLinesAreGroupedByBranchPhp82(): void
{
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php82.php');
Expand Down