From e7411742e0e580d48c8444ed9a36cdf8e5fadb81 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 21 Sep 2023 00:53:11 +0200 Subject: [PATCH] ControlStructures/AssignmentInCondition: prevent fatal error during live coding ``` Fatal error: Uncaught TypeError: SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff::processCondition(): Argument #3 ($parenthesisCloser) must be of type int, null given, called in path\to\SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff.php on line 56 and defined in path\to\SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff.php:59 Stack trace: #0 path\to\SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff.php(56): SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff->processCondition(Object(PHP_CodeSniffer\Files\LocalFile), 100, NULL, 'if') #1 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Files\File.php(509): SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 99) #2 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Files\LocalFile.php(92): PHP_CodeSniffer\Files\File->process() #3 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Runner.php(632): PHP_CodeSniffer\Files\LocalFile->process() #4 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Runner.php(438): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile)) #5 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Runner.php(116): PHP_CodeSniffer\Runner->run() #6 D:\000_GitHub\PHPCS\PHP_CodeSniffer\bin\phpcs(18): PHP_CodeSniffer\Runner->runPHPCS() #7 {main} thrown in path\to\SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff.php on line 59 ``` --- .../ControlStructures/AssignmentInConditionSniff.php | 9 +++++++++ .../ControlStructures/AssignmentInConditionSniffTest.php | 6 ++++++ .../data/assignmentsInConditionsLiveCoding.php | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php diff --git a/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php b/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php index 08877d347..56165449e 100644 --- a/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php +++ b/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php @@ -42,6 +42,7 @@ public function process(File $phpcsFile, $conditionStartPointer): void { $tokens = $phpcsFile->getTokens(); $token = $tokens[$conditionStartPointer]; + if ($token['code'] === T_DO) { $whilePointer = TokenHelper::findNext($phpcsFile, T_WHILE, $token['scope_closer'] + 1); $whileToken = $tokens[$whilePointer]; @@ -53,6 +54,14 @@ public function process(File $phpcsFile, $conditionStartPointer): void $parenthesisCloser = $token['parenthesis_closer']; $type = $token['code'] === T_IF ? 'if' : 'elseif'; } + + if ( + $parenthesisOpener === null + || $parenthesisCloser === null + ) { + return; + } + $this->processCondition($phpcsFile, $parenthesisOpener, $parenthesisCloser, $type); } diff --git a/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php b/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php index 1a37cb4a2..5aea3b7d6 100644 --- a/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php +++ b/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php @@ -49,4 +49,10 @@ public function testErrorsWithIgnoreAssignmentsInsideFunctionCalls(): void } } + public function testLiveCoding(): void + { + $resultFile = self::checkFile(__DIR__ . '/data/assignmentsInConditionsLiveCoding.php'); + self::assertNoSniffErrorInFile($resultFile); + } + } diff --git a/tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php b/tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php new file mode 100644 index 000000000..003dc8e5b --- /dev/null +++ b/tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php @@ -0,0 +1,5 @@ += 99.00 + +// Live coding/parse error. +// This must be the last test in the file. +if ($a = 1