Skip to content

Commit fe255fe

Browse files
committed
Test ContinueBreakInLoopRule for property hooks
1 parent 0bff31c commit fe255fe

File tree

4 files changed

+135
-5
lines changed

4 files changed

+135
-5
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 31 additions & 0 deletions
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
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
42+
43+
class ValidUsages
44+
{
45+
46+
public int $i {
47+
set (int $foo) {
48+
switch ($foo) {
49+
case 1:
50+
break;
51+
default:
52+
break;
53+
}
54+
55+
foreach ([1, 2, 3] as $val) {
56+
if (rand(0, 1)) {
57+
break;
58+
} else {
59+
continue;
60+
}
61+
}
62+
63+
for ($i = 0; $i < 5; $i++) {
64+
if (rand(0, 1)) {
65+
break;
66+
} else {
67+
continue;
68+
}
69+
}
70+
71+
while (true) {
72+
if (rand(0, 1)) {
73+
break;
74+
} else {
75+
continue;
76+
}
77+
}
78+
79+
do {
80+
if (rand(0, 1)) {
81+
break;
82+
} else {
83+
continue;
84+
}
85+
} while (true);
86+
}
87+
}
88+
89+
public int $j {
90+
set (int $foo) {
91+
foreach ([1, 2, 3] as $val) {
92+
switch ($foo) {
93+
case 1:
94+
break 2;
95+
default:
96+
break 2;
97+
}
98+
}
99+
}
100+
}
101+
102+
}

0 commit comments

Comments
 (0)