Skip to content

Commit 84cc169

Browse files
committed
SlevomatCodingStandard.PHP.RequireExplicitAssertion: Ignores unsupported unofficial type hints
1 parent d0e9240 commit 84cc169

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

SlevomatCodingStandard/Sniffs/PHP/RequireExplicitAssertionSniff.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
105105
continue;
106106
}
107107

108+
/** @var IdentifierTypeNode|ThisTypeNode|UnionTypeNode $variableAnnotationType */
109+
$variableAnnotationType = $variableAnnotationType;
110+
111+
$assertion = $this->createAssert($variableAnnotation->getVariableName(), $variableAnnotationType);
112+
113+
if ($assertion === null) {
114+
continue;
115+
}
116+
108117
if ($tokens[$codePointer]['code'] === T_VARIABLE) {
109118
$pointerAfterVariable = TokenHelper::findNextEffective($phpcsFile, $codePointer + 1);
110119
if ($tokens[$pointerAfterVariable]['code'] !== T_EQUAL) {
@@ -245,11 +254,6 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
245254
}
246255
}
247256

248-
/** @var IdentifierTypeNode|ThisTypeNode|UnionTypeNode $variableAnnotationType */
249-
$variableAnnotationType = $variableAnnotationType;
250-
251-
$assertion = $this->createAssert($variableAnnotation->getVariableName(), $variableAnnotationType);
252-
253257
if (
254258
$pointerToAddAssertion < $docCommentClosePointer
255259
&& array_key_exists($pointerAfterDocComment + 1, $tokens)
@@ -265,15 +269,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
265269

266270
private function isValidTypeNode(TypeNode $typeNode): bool
267271
{
268-
if ($typeNode instanceof ThisTypeNode) {
269-
return true;
270-
}
271-
272-
if (!$typeNode instanceof IdentifierTypeNode) {
273-
return false;
274-
}
275-
276-
return !in_array($typeNode->name, ['mixed', 'static'], true);
272+
return $typeNode instanceof ThisTypeNode || $typeNode instanceof IdentifierTypeNode;
277273
}
278274

279275
private function getNextSemicolonInSameScope(File $phpcsFile, int $scopePointer, int $searchAt): int
@@ -297,7 +293,7 @@ private function getNextSemicolonInSameScope(File $phpcsFile, int $scopePointer,
297293
/**
298294
* @param IdentifierTypeNode|ThisTypeNode|UnionTypeNode|IntersectionTypeNode $typeNode
299295
*/
300-
private function createAssert(string $variableName, TypeNode $typeNode): string
296+
private function createAssert(string $variableName, TypeNode $typeNode): ?string
301297
{
302298
$conditions = [];
303299

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

309+
if ($conditions === []) {
310+
return null;
311+
}
312+
313313
$operator = $typeNode instanceof IntersectionTypeNode ? '&&' : '||';
314314

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

360+
if (TypeHintHelper::isSimpleUnofficialTypeHints($typeNode->name)) {
361+
return [];
362+
}
363+
360364
return [sprintf('%s instanceof %s', $variableName, $typeNode->name)];
361365
}
362366

tests/Sniffs/PHP/data/requireExplicitAssertionNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ function (int $a) {
5151

5252
/** @var $m invalid annotation */
5353
$m = 0;
54+
55+
/** @var class-string $n */
56+
$n = 'SomeClass';

0 commit comments

Comments
 (0)