Skip to content

Commit

Permalink
ControlStructures/AssignmentInCondition: prevent fatal error during l…
Browse files Browse the repository at this point in the history
…ive 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
```
  • Loading branch information
jrfnl authored and kukulich committed Oct 5, 2023
1 parent b01453d commit e741174
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ public function testErrorsWithIgnoreAssignmentsInsideFunctionCalls(): void
}
}

public function testLiveCoding(): void
{
$resultFile = self::checkFile(__DIR__ . '/data/assignmentsInConditionsLiveCoding.php');
self::assertNoSniffErrorInFile($resultFile);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php // lint >= 99.00

// Live coding/parse error.
// This must be the last test in the file.
if ($a = 1

0 comments on commit e741174

Please sign in to comment.