From 9ee71715a8dc027e608df6f9c5b7551dee077bd2 Mon Sep 17 00:00:00 2001 From: Can Vural Date: Wed, 12 Nov 2025 23:47:38 +0000 Subject: [PATCH 1/2] add default value for getAttribute call in ContinueBreakInLoopRule --- Makefile | 1 + src/Rules/Keywords/ContinueBreakInLoopRule.php | 2 +- .../Rules/Keywords/ContinueBreakInLoopRuleTest.php | 10 ++++++++++ tests/PHPStan/Rules/Keywords/data/bug-13790.php | 2 ++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Keywords/data/bug-13790.php diff --git a/Makefile b/Makefile index f5476d6067..16e076baa0 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ 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.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..2fb27195a5 100644 --- a/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php +++ b/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php @@ -74,4 +74,14 @@ public function testPropertyHooks(): void ]); } + public function testBug13790(): void + { + $this->analyse([__DIR__ . '/data/bug-13790.php'], [ + [ + 'Keyword break used outside of a loop or a switch statement.', + 2, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Keywords/data/bug-13790.php b/tests/PHPStan/Rules/Keywords/data/bug-13790.php new file mode 100644 index 0000000000..28189ad66f --- /dev/null +++ b/tests/PHPStan/Rules/Keywords/data/bug-13790.php @@ -0,0 +1,2 @@ + Date: Thu, 13 Nov 2025 09:54:36 +0000 Subject: [PATCH 2/2] add test for continue --- Makefile | 3 ++- .../Keywords/ContinueBreakInLoopRuleTest.php | 18 ++++++++++++++---- .../{bug-13790.php => bug-13790-break.php} | 0 .../Rules/Keywords/data/bug-13790-continue.php | 2 ++ 4 files changed, 18 insertions(+), 5 deletions(-) rename tests/PHPStan/Rules/Keywords/data/{bug-13790.php => bug-13790-break.php} (100%) create mode 100644 tests/PHPStan/Rules/Keywords/data/bug-13790-continue.php diff --git a/Makefile b/Makefile index 16e076baa0..4169ec41b2 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +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.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/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php b/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php index 2fb27195a5..8bfb42a0bf 100644 --- a/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php +++ b/tests/PHPStan/Rules/Keywords/ContinueBreakInLoopRuleTest.php @@ -76,12 +76,22 @@ public function testPropertyHooks(): void public function testBug13790(): void { - $this->analyse([__DIR__ . '/data/bug-13790.php'], [ + $this->analyse( [ - 'Keyword break used outside of a loop or a switch statement.', - 2, + __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.php b/tests/PHPStan/Rules/Keywords/data/bug-13790-break.php similarity index 100% rename from tests/PHPStan/Rules/Keywords/data/bug-13790.php rename to tests/PHPStan/Rules/Keywords/data/bug-13790-break.php diff --git a/tests/PHPStan/Rules/Keywords/data/bug-13790-continue.php b/tests/PHPStan/Rules/Keywords/data/bug-13790-continue.php new file mode 100644 index 0000000000..187772a0ba --- /dev/null +++ b/tests/PHPStan/Rules/Keywords/data/bug-13790-continue.php @@ -0,0 +1,2 @@ +