From 20da5f65cb6a5938e3b978cc967902f157b49e7b Mon Sep 17 00:00:00 2001 From: Xaver Loppenstedt Date: Sat, 14 Dec 2024 21:09:49 +0100 Subject: [PATCH] Fix phpcbf crash in UnnecessaryNamespaceUsageSniff when class name is empty, fixes #212 --- .../Formatting/UnnecessaryNamespaceUsageSniff.php | 9 +++++++-- .../UnnecessaryNamespaceUsageUnitTest.pass.6.inc | 15 +++++++++++++++ .../UnnecessaryNamespaceUsageUnitTest.php | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.pass.6.inc diff --git a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php index 18b9d0f..28860e9 100644 --- a/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php +++ b/MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php @@ -224,13 +224,18 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra $end, true ); - $classNameEnd = (int) $phpcsFile->findNext( + $classNameEnd = $phpcsFile->findNext( $this->classNameTokens, ($classNameStart + 1), $end, true ); - $useEnd = $phpcsFile->findNext( + + if (false === $classNameEnd) { + break; + } + + $useEnd = $phpcsFile->findNext( [ T_SEMICOLON, T_COMMA, diff --git a/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.pass.6.inc b/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.pass.6.inc new file mode 100644 index 0000000..53448a0 --- /dev/null +++ b/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.pass.6.inc @@ -0,0 +1,15 @@ +b(); + } +} diff --git a/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php b/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php index ae6dab6..2cc6048 100644 --- a/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php +++ b/MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php @@ -40,6 +40,7 @@ class UnnecessaryNamespaceUsageUnitTest extends AbstractMo4SniffUnitTest 'UnnecessaryNamespaceUsageUnitTest.pass.3.inc' => [], 'UnnecessaryNamespaceUsageUnitTest.pass.4.inc' => [], 'UnnecessaryNamespaceUsageUnitTest.pass.5.inc' => [], + 'UnnecessaryNamespaceUsageUnitTest.pass.6.inc' => [], 'UnnecessaryNamespaceUsageUnitTest.fail.1.inc' => [ 17 => 1, 19 => 1,