Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

final class IterableParamFromAnotherMethod
{
/**
* @param string[] $items
*/
public function go(array $items)
{
$this->run($items);
}

private function run(array $items)
{
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

final class IterableParamFromAnotherMethod
{
/**
* @param string[] $items
*/
public function go(array $items)
{
$this->run($items);
}

/**
* @param string[] $items
*/
private function run(array $items)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;

final class SkipParamArrayOverride
{
public function go(array $items)
{
$this->run(['item1', 'item2']);
}

/**
* @param int[]|string[] $items
*/
private function run(array $items)
{
}
}
97 changes: 82 additions & 15 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Rector\TypeDeclaration\NodeAnalyzer;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -38,13 +40,12 @@ public function resolveStrictTypesFromCalls(array $calls): array

foreach ($calls as $call) {
foreach ($call->args as $position => $arg) {
// there is first class callable usage, or argument unpack, or named arg
// simply returns array marks as unknown as can be anything and in any position
if (! $arg instanceof Arg || $arg->unpack || $arg->name instanceof Identifier) {
if ($this->shouldSkipArg($arg)) {
return [];
}

if ($arg->value instanceof Array_ && $arg->value->items === []) {
/** @var Arg $arg */
if ($this->isEmptyArray($arg->value)) {
// skip empty array, as it doesn't add any value
continue;
}
Expand All @@ -57,23 +58,46 @@ public function resolveStrictTypesFromCalls(array $calls): array
return $this->unionToSingleType($staticTypesByArgumentPosition);
}

private function resolveStrictArgValueType(Arg $arg): Type
/**
* @param MethodCall[]|StaticCall[] $calls
* @return array<int, Type>
*/
public function resolveTypesFromCalls(array $calls): array
{
$argValueType = $this->nodeTypeResolver->getNativeType($arg->value);
$staticTypesByArgumentPosition = [];

// "self" in another object is not correct, this make it independent
$argValueType = $this->correctSelfType($argValueType);
foreach ($calls as $call) {
foreach ($call->args as $position => $arg) {
if ($this->shouldSkipArg($arg)) {
return [];
}

if (! $argValueType instanceof ObjectType) {
return $argValueType;
}
/** @var Arg $arg */
if ($this->isEmptyArray($arg->value)) {
// skip empty array, as it doesn't add any value
continue;
}

// fix false positive generic type on string
if (! $this->reflectionProvider->hasClass($argValueType->getClassName())) {
return new MixedType();
$staticTypesByArgumentPosition[$position][] = $this->resolveArgValueType($arg);
}
}

return $argValueType;
// unite to single type
return $this->unionToSingleType($staticTypesByArgumentPosition);
}

private function resolveStrictArgValueType(Arg $arg): Type
{
$argValueType = $this->nodeTypeResolver->getNativeType($arg->value);

return $this->normalizeType($argValueType);
}

private function resolveArgValueType(Arg $arg): Type
{
$argValueType = $this->nodeTypeResolver->getType($arg->value);

return $this->normalizeType($argValueType);
}

private function correctSelfType(Type $argValueType): Type
Expand Down Expand Up @@ -149,4 +173,47 @@ private function isTypeWithClassNameOnly(UnionType $unionType): bool

return true;
}

private function normalizeType(Type $argValueType): MixedType|ObjectType|Type
{
// "self" in another object is not correct, this make it independent
$argValueType = $this->correctSelfType($argValueType);

if (! $argValueType instanceof ObjectType) {
return $argValueType;
}

// fix false positive generic type on string
if (! $this->reflectionProvider->hasClass($argValueType->getClassName())) {
return new MixedType();
}

return $argValueType;
}

/**
* There is first class callable usage, or argument unpack, or named expr
* simply returns array marks as unknown as can be anything and in any position
*/
private function shouldSkipArg(Arg|VariadicPlaceholder $arg): bool
{
if ($arg instanceof VariadicPlaceholder) {
return true;
}

if ($arg->unpack) {
return true;
}

return $arg->name instanceof Identifier;
}

private function isEmptyArray(Expr $expr): bool
{
if (! $expr instanceof Array_) {
return false;
}

return $expr->items === [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function refactor(Node $node): ?Node
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);

$methodCalls = $this->localMethodCallFinder->match($node, $classMethod);
$classMethodParameterTypes = $this->callTypesResolver->resolveStrictTypesFromCalls($methodCalls);
$classMethodParameterTypes = $this->callTypesResolver->resolveTypesFromCalls($methodCalls);

foreach ($classMethod->getParams() as $parameterPosition => $param) {
if ($param->type === null) {
Expand Down
Loading