diff --git a/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractLineCondition.php b/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractLineCondition.php index 29bf1429d..613a94da8 100644 --- a/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractLineCondition.php +++ b/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractLineCondition.php @@ -7,6 +7,7 @@ use SlevomatCodingStandard\Helpers\IndentationHelper; use SlevomatCodingStandard\Helpers\SniffSettingsHelper; use SlevomatCodingStandard\Helpers\TokenHelper; +use function array_key_exists; use function in_array; use function preg_replace; use function rtrim; @@ -60,6 +61,15 @@ protected function shouldBeSkipped(File $phpcsFile, int $controlStructurePointer { $tokens = $phpcsFile->getTokens(); + if ( + !array_key_exists('parenthesis_opener', $tokens[$controlStructurePointer]) + || $tokens[$controlStructurePointer]['parenthesis_opener'] === null + || !array_key_exists('parenthesis_closer', $tokens[$controlStructurePointer]) + || $tokens[$controlStructurePointer]['parenthesis_closer'] === null + ) { + return true; + } + if ($tokens[$controlStructurePointer]['code'] === T_WHILE) { $isPartOfDo = $this->isPartOfDo($phpcsFile, $controlStructurePointer); diff --git a/build/PHPStan/GetTokenDynamicReturnTypeExtension.php b/build/PHPStan/GetTokenDynamicReturnTypeExtension.php index 6af0371f1..c5b8726f0 100644 --- a/build/PHPStan/GetTokenDynamicReturnTypeExtension.php +++ b/build/PHPStan/GetTokenDynamicReturnTypeExtension.php @@ -11,6 +11,7 @@ use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\DynamicMethodReturnTypeExtension; use PHPStan\Type\IntegerType; +use PHPStan\Type\NullType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -46,6 +47,7 @@ private function getTokensArrayType(): ArrayType $stringType = new StringType(); $integerType = new IntegerType(); $stringIntegerUnion = new UnionType([$stringType, $integerType]); + $nullableInteger = new UnionType([new NullType(), $integerType]); $baseArrayBuilder = ConstantArrayTypeBuilder::createEmpty(); $baseArrayBuilder->setOffsetValueType(new ConstantStringType('content'), $stringType); @@ -65,8 +67,8 @@ private function getTokensArrayType(): ArrayType $arrayBuilder->setOffsetValueType(new ConstantStringType('length'), $integerType); $arrayBuilder->setOffsetValueType(new ConstantStringType('level'), $integerType); $arrayBuilder->setOffsetValueType(new ConstantStringType('conditions'), new ArrayType($integerType, $stringIntegerUnion)); - $arrayBuilder->setOffsetValueType(new ConstantStringType('parenthesis_opener'), $integerType); - $arrayBuilder->setOffsetValueType(new ConstantStringType('parenthesis_closer'), $integerType); + $arrayBuilder->setOffsetValueType(new ConstantStringType('parenthesis_opener'), $nullableInteger); + $arrayBuilder->setOffsetValueType(new ConstantStringType('parenthesis_closer'), $nullableInteger); $arrayBuilder->setOffsetValueType(new ConstantStringType('parenthesis_owner'), $integerType); $arrayBuilder->setOffsetValueType(new ConstantStringType('scope_condition'), $integerType); $arrayBuilder->setOffsetValueType(new ConstantStringType('scope_opener'), $integerType); diff --git a/tests/Sniffs/ControlStructures/RequireSingleLineConditionSniffTest.php b/tests/Sniffs/ControlStructures/RequireSingleLineConditionSniffTest.php index a843e1ded..9c3cd3740 100644 --- a/tests/Sniffs/ControlStructures/RequireSingleLineConditionSniffTest.php +++ b/tests/Sniffs/ControlStructures/RequireSingleLineConditionSniffTest.php @@ -98,4 +98,10 @@ public function testNoErrorsWhenDisabledWhileControlStructure(): void self::assertNoSniffErrorInFile($report); } + public function testNoErrorsLiveCoding(): void + { + $report = self::checkFile(__DIR__ . '/data/requireSingleLineConditionLiveCoding.php'); + self::assertNoSniffErrorInFile($report); + } + } diff --git a/tests/Sniffs/ControlStructures/data/requireSingleLineConditionLiveCoding.php b/tests/Sniffs/ControlStructures/data/requireSingleLineConditionLiveCoding.php new file mode 100644 index 000000000..b642f6bf0 --- /dev/null +++ b/tests/Sniffs/ControlStructures/data/requireSingleLineConditionLiveCoding.php @@ -0,0 +1,5 @@ += 99.0 + +// Live coding/parse error. +// This must be the last test in the file. +if ($a = 1