Skip to content

Commit 3b7bbc2

Browse files
Majkl578kukulich
authored andcommitted
TypeHintDeclarationSniff: Fix UselessDocComment detection when function has parameters/returns, but doesn't have corresponding annotation
1 parent e3b5359 commit 3b7bbc2

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/TypeHintDeclarationSniff.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -475,43 +475,50 @@ private function checkUselessDocComment(\PHP_CodeSniffer_File $phpcsFile, int $f
475475
return TypeHintHelper::isSimpleTypeHint($typeHint) || TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHint) === TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHintInAnnotation);
476476
};
477477

478-
$returnTypeHint = FunctionHelper::findReturnTypeHint($phpcsFile, $functionPointer);
479478
if ($isAbstract || FunctionHelper::returnsValue($phpcsFile, $functionPointer)) {
480-
if ($returnTypeHint === null) {
481-
return;
482-
}
483-
484-
if ($this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $returnTypeHint->getTypeHint()))) {
485-
return;
486-
}
487-
479+
$returnTypeHint = FunctionHelper::findReturnTypeHint($phpcsFile, $functionPointer);
488480
$returnAnnotation = FunctionHelper::findReturnAnnotation($phpcsFile, $functionPointer);
481+
489482
if ($returnAnnotation !== null) {
490-
if (preg_match('~^\\S+\\s+\\S+~', $returnAnnotation->getContent())) {
483+
if ($returnTypeHint === null) {
491484
return;
492485
}
493486

494-
$returnTypeHintsDefinition = preg_split('~\\s+~', $returnAnnotation->getContent())[0];
495-
if ($this->definitionContainsStaticOrThisTypeHint($returnTypeHintsDefinition)) {
487+
if ($this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $returnTypeHint->getTypeHint()))) {
496488
return;
497-
} elseif ($this->enableNullableTypeHints && $this->definitionContainsJustTwoTypeHints($returnTypeHintsDefinition) && $this->definitionContainsNullTypeHint($returnTypeHintsDefinition)) {
498-
$returnTypeHintDefinitionParts = explode('|', $returnTypeHintsDefinition);
499-
$returnTypeHintInAnnotation = strtolower($returnTypeHintDefinitionParts[0]) === 'null' ? $returnTypeHintDefinitionParts[1] : $returnTypeHintDefinitionParts[0];
500-
if (!$typeHintEqualsAnnotation($returnTypeHint->getTypeHint(), $returnTypeHintInAnnotation)) {
489+
}
490+
491+
if ($returnAnnotation !== null) {
492+
if (preg_match('~^\\S+\\s+\\S+~', $returnAnnotation->getContent())) {
493+
return;
494+
}
495+
496+
$returnTypeHintsDefinition = preg_split('~\\s+~', $returnAnnotation->getContent())[0];
497+
if ($this->definitionContainsStaticOrThisTypeHint($returnTypeHintsDefinition)) {
498+
return;
499+
} elseif ($this->enableNullableTypeHints && $this->definitionContainsJustTwoTypeHints($returnTypeHintsDefinition) && $this->definitionContainsNullTypeHint($returnTypeHintsDefinition)) {
500+
$returnTypeHintDefinitionParts = explode('|', $returnTypeHintsDefinition);
501+
$returnTypeHintInAnnotation = strtolower($returnTypeHintDefinitionParts[0]) === 'null' ? $returnTypeHintDefinitionParts[1] : $returnTypeHintDefinitionParts[0];
502+
if (!$typeHintEqualsAnnotation($returnTypeHint->getTypeHint(), $returnTypeHintInAnnotation)) {
503+
return;
504+
}
505+
} elseif (!$this->definitionContainsOneTypeHint($returnTypeHintsDefinition)) {
506+
return;
507+
} elseif (!$typeHintEqualsAnnotation($returnTypeHint->getTypeHint(), $returnTypeHintsDefinition)) {
501508
return;
502509
}
503-
} elseif (!$this->definitionContainsOneTypeHint($returnTypeHintsDefinition)) {
504-
return;
505-
} elseif (!$typeHintEqualsAnnotation($returnTypeHint->getTypeHint(), $returnTypeHintsDefinition)) {
506-
return;
507510
}
508511
}
509512
}
510513

511514
$parametersTypeHintsDefinitions = $this->getFunctionParameterTypeHintsDefinitions($phpcsFile, $functionPointer);
512515
foreach (FunctionHelper::getParametersTypeHints($phpcsFile, $functionPointer) as $parameterName => $parameterTypeHint) {
513516
if ($parameterTypeHint === null) {
514-
return;
517+
if (array_key_exists($parameterName, $parametersTypeHintsDefinitions)) {
518+
return;
519+
} else {
520+
continue;
521+
}
515522
}
516523

517524
if ($this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $parameterTypeHint->getTypeHint()))) {

tests/Sniffs/TypeHints/TypeHintDeclarationSniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testErrors()
3434
],
3535
]);
3636

37-
$this->assertSame(51, $report->getErrorCount());
37+
$this->assertSame(53, $report->getErrorCount());
3838

3939
$this->assertSniffError($report, 8, TypeHintDeclarationSniff::CODE_MISSING_PARAMETER_TYPE_HINT);
4040
$this->assertSniffError($report, 15, TypeHintDeclarationSniff::CODE_MISSING_PARAMETER_TYPE_HINT);
@@ -56,6 +56,8 @@ public function testErrors()
5656
$this->assertSniffError($report, 87, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
5757
$this->assertSniffError($report, 111, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
5858

59+
$this->assertSniffError($report, 15, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);
60+
$this->assertSniffError($report, 55, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);
5961
$this->assertSniffError($report, 95, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);
6062
$this->assertSniffError($report, 102, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);
6163
$this->assertSniffError($report, 214, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);

0 commit comments

Comments
 (0)