Skip to content

Commit

Permalink
[TypeDeclaration] Skip default numeric string on param int on ParamTy…
Browse files Browse the repository at this point in the history
…peByMethodCallTypeRector (#5210)

* [TypeDeclaration] Skip default numeric string on param int on ParamTypeByMethodCallTypeRector

* udpate code

* Fixed 🎉

* Fixed 🎉

* more fixture

* fix
  • Loading branch information
samsonasik committed Oct 28, 2023
1 parent 6f33010 commit 97d095f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
@@ -0,0 +1,15 @@
<?php

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

final class SkipDefaultNumericStringParamInt
{
public function go($value = '1')
{
$this->execute($value);
}

private function execute(int $value)
{
}
}
@@ -0,0 +1,35 @@
<?php

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

final class WithDefaultConstantIntegerTypeParamInt
{
public function go($value = 1)
{
$this->execute($value);
}

private function execute(int $value)
{
}
}

?>
-----
<?php

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

final class WithDefaultConstantIntegerTypeParamInt
{
public function go(int $value = 1)
{
$this->execute($value);
}

private function execute(int $value)
{
}
}

?>
29 changes: 27 additions & 2 deletions rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php
Expand Up @@ -4,8 +4,10 @@

namespace Rector\TypeDeclaration\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
Expand All @@ -20,12 +22,16 @@
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;

final class CallerParamMatcher
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly AstResolver $astResolver
private readonly AstResolver $astResolver,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly TypeComparator $typeComparator
) {
}

Expand All @@ -39,7 +45,26 @@ public function matchCallParamType(
return null;
}

return $callParam->type;
if (! $param->default instanceof Expr) {
return $callParam->type;
}

if (! $callParam->type instanceof Node) {
return null;
}

$callParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($callParam->type);
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->default);

if ($this->typeComparator->areTypesEqual($callParamType, $defaultType)) {
return $callParam->type;
}

if ($this->typeComparator->isSubtype($defaultType, $callParamType)) {
return $callParam->type;
}

return null;
}

public function matchParentParam(StaticCall $parentStaticCall, Param $param, Scope $scope): ?Param
Expand Down

0 comments on commit 97d095f

Please sign in to comment.