Skip to content
Closed
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,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddParamTypeFromStrictMethodCallPassRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\Fixture;

final class SomeClass
{
public function setValue($number)
{
$this->checkInt($number);
}

private function checkInt(int $integer)
{
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\Fixture;

final class SomeClass
{
public function setValue(int $number)
{
$this->checkInt($number);
}

private function checkInt(int $integer)
{
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\Fixture;

final class SomeClass
{
public function setValue($number)
{
if (is_string($number)) {
return;
}

$this->checkInt($number);
}

private function checkInt(int $integer)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector;

return RectorConfig::configure()
->withRules([AddParamTypeFromStrictMethodCallPassRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\NodeTypeAnalyzer;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use Rector\Reflection\ReflectionResolver;

final readonly class MethodCallParamTypeResolver
{
public function __construct(
private ReflectionResolver $reflectionResolver,
) {
}

/**
* @return array<int, Type>
*/
public function resolve(MethodCall $methodCall): array
{
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($methodCall);
if (! $methodReflection instanceof MethodReflection) {
return [];
}

$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());

$typeByPosition = [];
foreach ($extendedParametersAcceptor->getParameters() as $position => $parameterReflection) {
$typeByPosition[$position] = $parameterReflection->getNativeType();
}

return $typeByPosition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\ClassMethod;

use PhpParser\Node\Expr\Closure;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\Type;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\NodeTypeAnalyzer\MethodCallParamTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\AddParamTypeFromStrictMethodCallPassRectorTest
*/
final class AddParamTypeFromStrictMethodCallPassRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly MethodCallParamTypeResolver $methodCallParamTypeResolver,
private readonly StaticTypeMapper $staticTypeMapper,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change param type from strict type of passed-to method call',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($value)
{
$this->resolve($value);
}

private function resolve(int $value)
{
}
}
Comment on lines +41 to +51
Copy link
Member

@samsonasik samsonasik Oct 13, 2025

Choose a reason for hiding this comment

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

@TomasVotruba there is already rule ParamTypeByMethodCallTypeRector for it, see

https://getrector.com/demo/e925c962-1957-42dc-8464-c943e7c2850b

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks 👍 I knew we had some similar rule :D . It missed an edge case, I'll check why.

Copy link
Member Author

Choose a reason for hiding this comment

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

CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(int $value)
{
$this->resolve($value);
}

private function resolve(int $value)
{
}
}
CODE_SAMPLE
),

]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class, Closure::class];
}

/**
* @param ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
{
if ($node->getParams() === []) {
return null;
}

if ($node instanceof ClassMethod && $node->stmts === null) {
return null;
}

$hasChanged = false;

$usedVariables = $this->betterNodeFinder->findInstancesOfScoped((array) $node->stmts, Variable::class);

foreach ($node->getParams() as $param) {
// already known type
if ($param->type instanceof Node) {
continue;
}

/** @var string $paramName */
$paramName = $this->getName($param->var);

$paramVariables = array_filter(
$usedVariables,
fn (Variable $variable): bool => $this->isName($variable, $paramName)
);

if (count($paramVariables) >= 2) {
// skip for now, as we look for sole use
continue;
}

$methodCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $node->stmts, MethodCall::class);
foreach ($methodCalls as $methodCall) {
if ($methodCall->isFirstClassCallable()) {
continue;
}

$typesByPosition = $this->methodCallParamTypeResolver->resolve($methodCall);

$usedPosition = $this->matchParamMethodCallUsedPosition($methodCall, $paramName);
if (! is_int($usedPosition)) {
continue;
}

$paramType = $typesByPosition[$usedPosition] ?? null;
if (! $paramType instanceof Type) {
continue;
}

$paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM);
if (! $paramTypeNode instanceof Node) {
continue;
}

$param->type = $paramTypeNode;
$hasChanged = true;

// go to next param
continue 2;
}
}

return null;
}

private function matchParamMethodCallUsedPosition(MethodCall $methodCall, string $paramName): int|null
{
foreach ($methodCall->getArgs() as $position => $arg) {
if (! $arg->value instanceof Variable) {
continue;
}

if (! $this->isName($arg->value, $paramName)) {
continue;
}

return $position;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ public function refactor(Node $node): ?Node
}

$varType = $this->getType($node->var);

if (! $varType instanceof IntersectionType || ! $varType->isIterable()->yes()) {
return null;
}

$className = $varType->getObjectClassNames()[0] ?? null;

if ($className === null) {
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
Expand Down Expand Up @@ -149,6 +150,7 @@ final class TypeDeclarationLevel
// array parameter from dim fetch assign inside
StrictArrayParamDimFetchRector::class,
AddParamFromDimFetchKeyUseRector::class,
AddParamTypeFromStrictMethodCallPassRector::class,
AddParamStringTypeFromSprintfUseRector::class,

// possibly based on docblocks, but also helpful, intentionally last
Expand Down
2 changes: 0 additions & 2 deletions src/Reflection/ReflectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
Expand Down
Loading