Skip to content

Adding difference in detection between mixed[] and array #15

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

Merged
merged 1 commit into from
Sep 22, 2017
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
30 changes: 26 additions & 4 deletions src/Rules/TypeHints/AbstractMissingTypeHintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TheCodingMachine\PHPStan\Rules\TypeHints;


use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Boolean;
Expand All @@ -24,6 +25,8 @@
use Roave\BetterReflection\Reflection\ReflectionFunction;
use Roave\BetterReflection\Reflection\ReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionParameter;
use Roave\BetterReflection\TypesFinder\PhpDocumentor\NamespaceNodeToReflectionTypeContext;
use Roave\BetterReflection\TypesFinder\ResolveTypes;

abstract class AbstractMissingTypeHintRule implements Rule
{
Expand Down Expand Up @@ -181,10 +184,12 @@ private function analyzeWithTypehint($context, Type $phpTypeHint, array $docBloc
}

if ($docblockTypehint->getValueType() instanceof Mixed_) {
if ($context instanceof ReflectionParameter) {
return sprintf('%s, parameter $%s type is "array". Please provide a more specific @param annotation. For instance: @param int[] $%s', $this->getContext($context), $context->getName(), $context->getName());
} else {
return sprintf('%s, return type is "array". Please provide a more specific @return annotation. For instance: @return int[]', $this->getContext($context));
if (!$this->findExplicitMixedArray($context)) {
if ($context instanceof ReflectionParameter) {
return sprintf('%s, parameter $%s type is "array". Please provide a more specific @param annotation in the docblock. For instance: @param int[] $%s. Use @param mixed[] $%s if this is really an array of mixed values.', $this->getContext($context), $context->getName(), $context->getName(), $context->getName());
} else {
return sprintf('%s, return type is "array". Please provide a more specific @return annotation. For instance: @return int[]. Use @return mixed[] if this is really an array of mixed values.', $this->getContext($context));
}
}
}
}
Expand All @@ -193,6 +198,23 @@ private function analyzeWithTypehint($context, Type $phpTypeHint, array $docBloc
return null;
}

/**
* @param ReflectionParameter|ReflectionMethod|ReflectionFunction $context
* @return bool
*/
private function findExplicitMixedArray($context) : bool
{
if ($context instanceof ReflectionParameter) {
$context = $context->getDeclaringFunction();
}

$docComment = $context->getDocComment();

// Very approximate solution: let's find in the whole docblock whether there is a mixed[] value or not.
// TODO: improve this to target precisely the parameter or return type.
return strpos($docComment, 'mixed[]') !== false;
}

/**
* @param ReflectionParameter|ReflectionMethod|ReflectionFunction $context
* @param Type[] $docBlockTypeHints
Expand Down
8 changes: 8 additions & 0 deletions tests/Rules/TypeHints/MissingTypeHintRuleInFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function testCheckCatchedException()
[
'In function "mismatch", return type is type-hinted to "string" but the @return annotation says it is a "int". Please fix the @return annotation.',
52,
],
[
'In function "test8", parameter $any_array type is "array". Please provide a more specific @param annotation in the docblock. For instance: @param int[] $any_array. Use @param mixed[] $any_array if this is really an array of mixed values.',
70,
],
[
'In function "test8", return type is "array". Please provide a more specific @return annotation. For instance: @return int[]. Use @return mixed[] if this is really an array of mixed values.',
70,
],
]);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Rules/TypeHints/data/typehints.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,21 @@ function mismatch(?string $param): string
{

}

/**
* @param mixed[] $any_array
* @return mixed[]
*/
function test7(array $any_array): array
{

}

/**
* @param array $any_array
* @return array
*/
function test8(array $any_array): array
{

}