Skip to content

Commit

Permalink
ControlStructures/RequireSingleLineCondition: prevent error for parse…
Browse files Browse the repository at this point in the history
… error

```
An error occurred during processing; checking has been aborted. The error message was: Undefined array key "" in path\to\SlevomatCodingStandard\Sniffs\ControlStructures\RequireSingleLineConditionSniff.php on line 41
```
  • Loading branch information
jrfnl authored and kukulich committed Oct 5, 2023
1 parent 2d45e24 commit b01453d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions build/PHPStan/GetTokenDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

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

0 comments on commit b01453d

Please sign in to comment.