Skip to content

Commit 5db254c

Browse files
Slamdunksebastianbergmann
authored andcommitted
Fix new in initializers
1 parent 6dd9e46 commit 5db254c

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ public function enterNode(Node $node): void
128128
$node instanceof Node\Stmt\ClassMethod ||
129129
$node instanceof Node\Expr\Closure ||
130130
$node instanceof Node\Stmt\Trait_) {
131+
if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
132+
foreach ($node->getParams() as $param) {
133+
foreach (range($param->getStartLine(), $param->getEndLine()) as $line) {
134+
$this->unsets[$line] = true;
135+
}
136+
}
137+
unset($this->unsets[$node->getEndLine()]);
138+
}
139+
131140
$isConcreteClassLike = $node instanceof Node\Stmt\Enum_ || $node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_;
132141

133142
if (null !== $node->stmts) {

tests/_files/source_for_branched_exec_lines_php81.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,54 @@ interface MyIntersection
3232
public function check(MyIntOne&MyIntTwo $intersection);
3333
public function neverReturn(): never;
3434
}
35+
36+
// New in initializers
37+
class NewInInit_NoInit
38+
{
39+
public function __construct(private DateTimeInterface $dateTime) {
40+
} // +1
41+
public function noinit(DateTimeInterface $dateTime) {
42+
} // +1
43+
}
44+
class NewInInit_OneLineNewLine
45+
{
46+
public function __construct(private DateTimeInterface $dateTime = new DateTime()) {
47+
} // +1
48+
public function onelinenewline(DateTimeInterface $dateTime = new DateTime()) {
49+
} // +2
50+
}
51+
class NewInInit_OneLineSameLine
52+
{
53+
public function __construct(private DateTimeInterface $dateTime = new DateTime()) {} // +2
54+
public function onelinesameline(DateTimeInterface $dateTime = new DateTime()) {} // +1
55+
}
56+
class NewInInit_MultiLine
57+
{
58+
public function __construct(
59+
private
60+
DateTimeInterface
61+
$dateTime
62+
=
63+
new
64+
DateTime()
65+
,
66+
private
67+
bool
68+
$var
69+
=
70+
true
71+
)
72+
{
73+
} // +1
74+
public function multiline(
75+
DateTimeInterface $dateTime = new DateTime()
76+
) {
77+
} // +2
78+
}
79+
function newInInit_OneLineNewLine(DateTimeInterface $dateTime = new DateTime()) {
80+
} // +2
81+
function newInInit_OneLineSameLine(DateTimeInterface $dateTime = new DateTime()) {} // +2
82+
function newInInit_multiline(
83+
DateTimeInterface $dateTime = new DateTime()
84+
) {
85+
} // +1

tests/tests/StaticAnalysis/ExecutableLinesFindingVisitorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public function testExecutableLinesAreGroupedByBranch(): void
2828
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines.php');
2929
}
3030

31-
#[RequiresPhp('8.1.*')]
31+
#[RequiresPhp('>=8.1')]
3232
public function testExecutableLinesAreGroupedByBranchPhp81(): void
3333
{
3434
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php81.php');
3535
}
3636

37-
#[RequiresPhp('8.2.*')]
37+
#[RequiresPhp('>=8.2')]
3838
public function testExecutableLinesAreGroupedByBranchPhp82(): void
3939
{
4040
$this->doTestSelfDescribingAsset(TEST_FILES_PATH . 'source_for_branched_exec_lines_php82.php');

0 commit comments

Comments
 (0)