Skip to content

Commit

Permalink
Fixed "TypeHintDeclarationSniff incorrectly reports about missing typ…
Browse files Browse the repository at this point in the history
…ehint when parameter docBlock contains union of two types, one is traversable and the second one does not specify the traversable type"
  • Loading branch information
kukulich committed Apr 18, 2017
1 parent eb43ba0 commit 2bcf401
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,20 @@ private function checkParametersTypeHints(\PHP_CodeSniffer_File $phpcsFile, int
}
} elseif ($this->definitionContainsTraversableTypeHint($phpcsFile, $functionPointer, $parameterTypeHintDefinition)) {
$parameterTypeHintDefinitionParts = explode('|', $parameterTypeHintDefinition);
$possibleParameterTypeHint = $this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $parameterTypeHintDefinitionParts[0])) ? $parameterTypeHintDefinitionParts[0] : $parameterTypeHintDefinitionParts[1];
$nullableParameterTypeHint = false;

if ($this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $parameterTypeHintDefinitionParts[0]))) {
$possibleParameterTypeHint = $parameterTypeHintDefinitionParts[0];
$itemsTypeHintDefinition = $parameterTypeHintDefinitionParts[1];
} else {
$possibleParameterTypeHint = $parameterTypeHintDefinitionParts[1];
$itemsTypeHintDefinition = $parameterTypeHintDefinitionParts[0];
}

if (!$this->definitionContainsTraversableTypeHintSpeficication($itemsTypeHintDefinition)) {
return;
}

} else {
return;
}
Expand Down Expand Up @@ -398,8 +410,20 @@ private function checkReturnTypeHints(\PHP_CodeSniffer_File $phpcsFile, int $fun
}
} elseif ($this->definitionContainsTraversableTypeHint($phpcsFile, $functionPointer, $returnTypeHintDefinition)) {
$returnTypeHintDefinitionParts = explode('|', $returnTypeHintDefinition);
$possibleReturnTypeHint = $this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $returnTypeHintDefinitionParts[0])) ? $returnTypeHintDefinitionParts[0] : $returnTypeHintDefinitionParts[1];
$nullableReturnTypeHint = false;

if ($this->isTraversableTypeHint(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $returnTypeHintDefinitionParts[0]))) {
$possibleReturnTypeHint = $returnTypeHintDefinitionParts[0];
$itemsTypeHintDefinition = $returnTypeHintDefinitionParts[1];
} else {
$possibleReturnTypeHint = $returnTypeHintDefinitionParts[1];
$itemsTypeHintDefinition = $returnTypeHintDefinitionParts[0];
}

if (!$this->definitionContainsTraversableTypeHintSpeficication($itemsTypeHintDefinition)) {
return;
}

} else {
return;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Sniffs/TypeHints/data/typeHintDeclarationNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,21 @@ private function nullableMultidimensionalArrayParameter($a)
*/
abstract public function invalidAnnotations();

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification
* @param string|array $a
*/
public function mixedContainingTraversable($a)
{
}

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification
* @return string|array
*/
public function returnsMixedContainingTraversable()
{
return [];
}

}

0 comments on commit 2bcf401

Please sign in to comment.