Skip to content

Commit

Permalink
SlevomatCodingStandard.PHP.RequireExplicitAssertion: Ignores unsuppor…
Browse files Browse the repository at this point in the history
…ted unofficial type hints
  • Loading branch information
kukulich committed Jun 17, 2022
1 parent d0e9240 commit 84cc169
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
34 changes: 19 additions & 15 deletions SlevomatCodingStandard/Sniffs/PHP/RequireExplicitAssertionSniff.php
Expand Up @@ -105,6 +105,15 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
continue;
}

/** @var IdentifierTypeNode|ThisTypeNode|UnionTypeNode $variableAnnotationType */
$variableAnnotationType = $variableAnnotationType;

$assertion = $this->createAssert($variableAnnotation->getVariableName(), $variableAnnotationType);

if ($assertion === null) {
continue;
}

if ($tokens[$codePointer]['code'] === T_VARIABLE) {
$pointerAfterVariable = TokenHelper::findNextEffective($phpcsFile, $codePointer + 1);
if ($tokens[$pointerAfterVariable]['code'] !== T_EQUAL) {
Expand Down Expand Up @@ -245,11 +254,6 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
}
}

/** @var IdentifierTypeNode|ThisTypeNode|UnionTypeNode $variableAnnotationType */
$variableAnnotationType = $variableAnnotationType;

$assertion = $this->createAssert($variableAnnotation->getVariableName(), $variableAnnotationType);

if (
$pointerToAddAssertion < $docCommentClosePointer
&& array_key_exists($pointerAfterDocComment + 1, $tokens)
Expand All @@ -265,15 +269,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void

private function isValidTypeNode(TypeNode $typeNode): bool
{
if ($typeNode instanceof ThisTypeNode) {
return true;
}

if (!$typeNode instanceof IdentifierTypeNode) {
return false;
}

return !in_array($typeNode->name, ['mixed', 'static'], true);
return $typeNode instanceof ThisTypeNode || $typeNode instanceof IdentifierTypeNode;
}

private function getNextSemicolonInSameScope(File $phpcsFile, int $scopePointer, int $searchAt): int
Expand All @@ -297,7 +293,7 @@ private function getNextSemicolonInSameScope(File $phpcsFile, int $scopePointer,
/**
* @param IdentifierTypeNode|ThisTypeNode|UnionTypeNode|IntersectionTypeNode $typeNode
*/
private function createAssert(string $variableName, TypeNode $typeNode): string
private function createAssert(string $variableName, TypeNode $typeNode): ?string
{
$conditions = [];

Expand All @@ -310,6 +306,10 @@ private function createAssert(string $variableName, TypeNode $typeNode): string
}
}

if ($conditions === []) {
return null;
}

$operator = $typeNode instanceof IntersectionTypeNode ? '&&' : '||';

return sprintf('\assert(%s);', implode(sprintf(' %s ', $operator), array_unique($conditions)));
Expand Down Expand Up @@ -357,6 +357,10 @@ private function createConditions(string $variableName, TypeNode $typeNode): arr
];
}

if (TypeHintHelper::isSimpleUnofficialTypeHints($typeNode->name)) {
return [];
}

return [sprintf('%s instanceof %s', $variableName, $typeNode->name)];
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Sniffs/PHP/data/requireExplicitAssertionNoErrors.php
Expand Up @@ -51,3 +51,6 @@ function (int $a) {

/** @var $m invalid annotation */
$m = 0;

/** @var class-string $n */
$n = 'SomeClass';

0 comments on commit 84cc169

Please sign in to comment.