diff --git a/Makefile b/Makefile index f5476d6067..4169ec41b2 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,8 @@ lint: --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/Keywords/data/bug-13790-break.php \ + --exclude tests/PHPStan/Rules/Keywords/data/bug-13790-continue.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 \ diff --git a/src/Rules/Keywords/ContinueBreakInLoopRule.php b/src/Rules/Keywords/ContinueBreakInLoopRule.php index e07d63a5b7..7a167ee6be 100644 --- a/src/Rules/Keywords/ContinueBreakInLoopRule.php +++ b/src/Rules/Keywords/ContinueBreakInLoopRule.php @@ -37,7 +37,7 @@ public function processNode(Node $node, Scope $scope): array $value = $node->num->value; } - $parentStmtTypes = array_reverse($node->getAttribute(ParentStmtTypesVisitor::ATTRIBUTE_NAME)); + $parentStmtTypes = array_reverse($node->getAttribute(ParentStmtTypesVisitor::ATTRIBUTE_NAME, [])); foreach ($parentStmtTypes as $parentStmtType) { if ($parentStmtType === Stmt\Case_::class) { continue; diff --git a/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php b/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php index 7c955e4c50..8bfb42a0bf 100644 --- a/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php +++ b/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php @@ -74,4 +74,24 @@ public function testPropertyHooks(): void ]); } + public function testBug13790(): void + { + $this->analyse( + [ + __DIR__ . '/data/bug-13790-break.php', + __DIR__ . '/data/bug-13790-continue.php', + ], + [ + [ + 'Keyword break used outside of a loop or a switch statement.', + 2, + ], + [ + 'Keyword continue used outside of a loop or a switch statement.', + 2, + ], + ], + ); + } + } diff --git a/tests/PHPStan/Rules/Keywords/data/bug-13790-break.php b/tests/PHPStan/Rules/Keywords/data/bug-13790-break.php new file mode 100644 index 0000000000..28189ad66f --- /dev/null +++ b/tests/PHPStan/Rules/Keywords/data/bug-13790-break.php @@ -0,0 +1,2 @@ +