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,39 @@
<?php

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

final class FlippedArgs
{
public function go()
{
$this->run(names: ['John', 'Doe'], items: [123, 456]);
}

private function run(array $items, array $names)
{
}
}

?>
-----
<?php

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

final class FlippedArgs
{
public function go()
{
$this->run(names: ['John', 'Doe'], items: [123, 456]);
}

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

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

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

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

private function run(array $items)
{
}
}

?>
-----
<?php

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

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

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

?>
18 changes: 10 additions & 8 deletions rules/TypeDeclaration/NodeAnalyzer/CallTypesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function resolveStrictTypesFromCalls(array $calls): array

/**
* @param MethodCall[]|StaticCall[] $calls
* @return array<int, Type>
* @return array<int|string, Type>
*/
public function resolveTypesFromCalls(array $calls): array
{
Expand All @@ -67,7 +67,7 @@ public function resolveTypesFromCalls(array $calls): array
foreach ($calls as $call) {
foreach ($call->args as $position => $arg) {
if ($this->shouldSkipArg($arg)) {
return [];
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return [] was right, when there is a first class callable or unpack in list of calls, should be immediatelly stop all together since we don't know what the arg passed allowed.

I will create new PR for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/** @var Arg $arg */
Expand All @@ -76,7 +76,13 @@ public function resolveTypesFromCalls(array $calls): array
continue;
}

$staticTypesByArgumentPosition[$position][] = $this->resolveArgValueType($arg);
if ($arg->name instanceof Identifier) {
$positionOrName = $arg->name->toString();
} else {
$positionOrName = $position;
}

$staticTypesByArgumentPosition[$positionOrName][] = $this->resolveArgValueType($arg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is ArgsAnalyzer->resolveArgPosition()

public function resolveArgPosition(array $args, string $name, int $defaultPosition): int

I will create a PR for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, seems ok on this case 👍

}
}

Expand Down Expand Up @@ -206,11 +212,7 @@ private function shouldSkipArg(Arg|VariadicPlaceholder $arg): bool
return true;
}

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

return $arg->name instanceof Identifier;
return $arg->unpack;
}

private function isEmptyArray(Expr $expr): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node
continue;
}

$resolvedParameterType = $classMethodParameterTypes[$parameterPosition] ?? null;
$resolvedParameterType = $classMethodParameterTypes[$parameterPosition] ?? $classMethodParameterTypes[$parameterName] ?? null;
if (! $resolvedParameterType instanceof Type) {
continue;
}
Expand Down
Loading