Skip to content

Commit e741174

Browse files
jrfnlkukulich
authored andcommitted
ControlStructures/AssignmentInCondition: prevent fatal error during live coding
``` Fatal error: Uncaught TypeError: SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff::processCondition(): Argument slevomat#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) slevomat#2 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Files\LocalFile.php(92): PHP_CodeSniffer\Files\File->process() slevomat#3 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Runner.php(632): PHP_CodeSniffer\Files\LocalFile->process() slevomat#4 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Runner.php(438): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile)) slevomat#5 D:\000_GitHub\PHPCS\PHP_CodeSniffer\src\Runner.php(116): PHP_CodeSniffer\Runner->run() slevomat#6 D:\000_GitHub\PHPCS\PHP_CodeSniffer\bin\phpcs(18): PHP_CodeSniffer\Runner->runPHPCS() slevomat#7 {main} thrown in path\to\SlevomatCodingStandard\Sniffs\ControlStructures\AssignmentInConditionSniff.php on line 59 ```
1 parent b01453d commit e741174

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function process(File $phpcsFile, $conditionStartPointer): void
4242
{
4343
$tokens = $phpcsFile->getTokens();
4444
$token = $tokens[$conditionStartPointer];
45+
4546
if ($token['code'] === T_DO) {
4647
$whilePointer = TokenHelper::findNext($phpcsFile, T_WHILE, $token['scope_closer'] + 1);
4748
$whileToken = $tokens[$whilePointer];
@@ -53,6 +54,14 @@ public function process(File $phpcsFile, $conditionStartPointer): void
5354
$parenthesisCloser = $token['parenthesis_closer'];
5455
$type = $token['code'] === T_IF ? 'if' : 'elseif';
5556
}
57+
58+
if (
59+
$parenthesisOpener === null
60+
|| $parenthesisCloser === null
61+
) {
62+
return;
63+
}
64+
5665
$this->processCondition($phpcsFile, $parenthesisOpener, $parenthesisCloser, $type);
5766
}
5867

tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public function testErrorsWithIgnoreAssignmentsInsideFunctionCalls(): void
4949
}
5050
}
5151

52+
public function testLiveCoding(): void
53+
{
54+
$resultFile = self::checkFile(__DIR__ . '/data/assignmentsInConditionsLiveCoding.php');
55+
self::assertNoSniffErrorInFile($resultFile);
56+
}
57+
5258
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php // lint >= 99.00
2+
3+
// Live coding/parse error.
4+
// This must be the last test in the file.
5+
if ($a = 1

0 commit comments

Comments
 (0)