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,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)
{
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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