Skip to content

Commit b85805f

Browse files
committed
Test ContinueBreakInLoopRule for property hooks
1 parent 2688515 commit b85805f

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ lint:
4343
--exclude tests/PHPStan/Rules/Functions/data/arrow-function-nullsafe-by-ref.php \
4444
--exclude tests/PHPStan/Levels/data/namedArguments.php \
4545
--exclude tests/PHPStan/Rules/Keywords/data/continue-break.php \
46+
--exclude tests/PHPStan/Rules/Keywords/data/continue-break-property-hook.php \
4647
--exclude tests/PHPStan/Rules/Properties/data/invalid-callable-property-type.php \
4748
--exclude tests/PHPStan/Rules/Properties/data/properties-in-interface.php \
4849
--exclude tests/PHPStan/Rules/Properties/data/read-only-property.php \

src/Rules/Keywords/ContinueBreakInLoopRule.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public function processNode(Node $node, Scope $scope): array
3939
if ($parentStmtType === Stmt\Case_::class) {
4040
continue;
4141
}
42-
if (
43-
$parentStmtType === Stmt\Function_::class
44-
|| $parentStmtType === Stmt\ClassMethod::class
45-
|| $parentStmtType === Node\Expr\Closure::class
46-
) {
42+
if ($parentStmtType === Node\Expr\Closure::class) {
4743
return [
4844
RuleErrorBuilder::message(sprintf(
4945
'Keyword %s used outside of a loop or a switch statement.',

tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7+
use const PHP_VERSION_ID;
78

89
/**
910
* @extends RuleTestCase<ContinueBreakInLoopRule>
@@ -46,4 +47,34 @@ public function testRule(): void
4647
]);
4748
}
4849

50+
public function testPropertyHooks(): void
51+
{
52+
if (PHP_VERSION_ID < 80400) {
53+
$this->markTestSkipped('Test requires PHP 8.4.');
54+
}
55+
56+
$this->analyse([__DIR__ . '/data/continue-break-property-hook.php'], [
57+
[
58+
'Keyword break used outside of a loop or a switch statement.',
59+
13,
60+
],
61+
[
62+
'Keyword break used outside of a loop or a switch statement.',
63+
15,
64+
],
65+
[
66+
'Keyword break used outside of a loop or a switch statement.',
67+
24,
68+
],
69+
[
70+
'Keyword continue used outside of a loop or a switch statement.',
71+
26,
72+
],
73+
[
74+
'Keyword break used outside of a loop or a switch statement.',
75+
35,
76+
],
77+
]);
78+
}
79+
4980
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php // lint >= 8.4
2+
3+
namespace ContinueBreakPropertyHook;
4+
5+
class Foo
6+
{
7+
8+
public int $bar {
9+
set (int $foo) {
10+
foreach ([1, 2, 3] as $val) {
11+
switch ($foo) {
12+
case 1:
13+
break 3;
14+
default:
15+
break 3;
16+
}
17+
}
18+
}
19+
}
20+
21+
public int $baz {
22+
get {
23+
if (rand(0, 1)) {
24+
break;
25+
} else {
26+
continue;
27+
}
28+
}
29+
}
30+
31+
public int $ipsum {
32+
get {
33+
foreach ([1, 2, 3] as $val) {
34+
function (): void {
35+
break;
36+
};
37+
}
38+
}
39+
}
40+
41+
}

tests/PHPStan/Rules/Keywords/data/continue-break.php

+61
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,64 @@ function (): void {
9494
if (rand(0, 1)) {
9595
break;
9696
}
97+
98+
class ValidUsages
99+
{
100+
101+
public int $i {
102+
set (int $foo) {
103+
switch ($foo) {
104+
case 1:
105+
break;
106+
default:
107+
break;
108+
}
109+
110+
foreach ([1, 2, 3] as $val) {
111+
if (rand(0, 1)) {
112+
break;
113+
} else {
114+
continue;
115+
}
116+
}
117+
118+
for ($i = 0; $i < 5; $i++) {
119+
if (rand(0, 1)) {
120+
break;
121+
} else {
122+
continue;
123+
}
124+
}
125+
126+
while (true) {
127+
if (rand(0, 1)) {
128+
break;
129+
} else {
130+
continue;
131+
}
132+
}
133+
134+
do {
135+
if (rand(0, 1)) {
136+
break;
137+
} else {
138+
continue;
139+
}
140+
} while (true);
141+
}
142+
}
143+
144+
public int $j {
145+
set (int $foo) {
146+
foreach ([1, 2, 3] as $val) {
147+
switch ($foo) {
148+
case 1:
149+
break 2;
150+
default:
151+
break 2;
152+
}
153+
}
154+
}
155+
}
156+
157+
}

0 commit comments

Comments
 (0)