Skip to content

Commit

Permalink
Test ContinueBreakInLoopRule for property hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 19, 2024
1 parent 2688515 commit b85805f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ lint:
--exclude tests/PHPStan/Rules/Functions/data/arrow-function-nullsafe-by-ref.php \
--exclude tests/PHPStan/Levels/data/namedArguments.php \
--exclude tests/PHPStan/Rules/Keywords/data/continue-break.php \
--exclude tests/PHPStan/Rules/Keywords/data/continue-break-property-hook.php \
--exclude tests/PHPStan/Rules/Properties/data/invalid-callable-property-type.php \
--exclude tests/PHPStan/Rules/Properties/data/properties-in-interface.php \
--exclude tests/PHPStan/Rules/Properties/data/read-only-property.php \
Expand Down
6 changes: 1 addition & 5 deletions src/Rules/Keywords/ContinueBreakInLoopRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public function processNode(Node $node, Scope $scope): array
if ($parentStmtType === Stmt\Case_::class) {
continue;
}
if (
$parentStmtType === Stmt\Function_::class
|| $parentStmtType === Stmt\ClassMethod::class
|| $parentStmtType === Node\Expr\Closure::class
) {
if ($parentStmtType === Node\Expr\Closure::class) {
return [
RuleErrorBuilder::message(sprintf(
'Keyword %s used outside of a loop or a switch statement.',
Expand Down
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<ContinueBreakInLoopRule>
Expand Down Expand Up @@ -46,4 +47,34 @@ public function testRule(): void
]);
}

public function testPropertyHooks(): void
{
if (PHP_VERSION_ID < 80400) {
$this->markTestSkipped('Test requires PHP 8.4.');
}

$this->analyse([__DIR__ . '/data/continue-break-property-hook.php'], [
[
'Keyword break used outside of a loop or a switch statement.',
13,
],
[
'Keyword break used outside of a loop or a switch statement.',
15,
],
[
'Keyword break used outside of a loop or a switch statement.',
24,
],
[
'Keyword continue used outside of a loop or a switch statement.',
26,
],
[
'Keyword break used outside of a loop or a switch statement.',
35,
],
]);
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/continue-break-property-hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php // lint >= 8.4

namespace ContinueBreakPropertyHook;

class Foo
{

public int $bar {
set (int $foo) {
foreach ([1, 2, 3] as $val) {
switch ($foo) {
case 1:
break 3;
default:
break 3;
}
}
}
}

public int $baz {
get {
if (rand(0, 1)) {
break;
} else {
continue;
}
}
}

public int $ipsum {
get {
foreach ([1, 2, 3] as $val) {
function (): void {
break;
};
}
}
}

}
61 changes: 61 additions & 0 deletions tests/PHPStan/Rules/Keywords/data/continue-break.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,64 @@ function (): void {
if (rand(0, 1)) {
break;
}

class ValidUsages
{

public int $i {
set (int $foo) {
switch ($foo) {
case 1:
break;
default:
break;
}

foreach ([1, 2, 3] as $val) {
if (rand(0, 1)) {
break;
} else {
continue;
}
}

for ($i = 0; $i < 5; $i++) {
if (rand(0, 1)) {
break;
} else {
continue;
}
}

while (true) {
if (rand(0, 1)) {
break;
} else {
continue;
}
}

do {
if (rand(0, 1)) {
break;
} else {
continue;
}
} while (true);
}
}

public int $j {
set (int $foo) {
foreach ([1, 2, 3] as $val) {
switch ($foo) {
case 1:
break 2;
default:
break 2;
}
}
}
}

}

0 comments on commit b85805f

Please sign in to comment.