Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ControlStructures/RequireSingleLineCondition: prevent error for parse error #1625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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