diff --git a/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php b/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php index ab8f8723759..f847f9dd06f 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php +++ b/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php @@ -3,8 +3,10 @@ declare (strict_types=1); 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; @@ -19,6 +21,8 @@ use PHPStan\Analyser\Scope; 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 { /** @@ -31,10 +35,22 @@ final class CallerParamMatcher * @var \Rector\Core\PhpParser\AstResolver */ private $astResolver; - public function __construct(NodeNameResolver $nodeNameResolver, AstResolver $astResolver) + /** + * @readonly + * @var \Rector\StaticTypeMapper\StaticTypeMapper + */ + private $staticTypeMapper; + /** + * @readonly + * @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator + */ + private $typeComparator; + public function __construct(NodeNameResolver $nodeNameResolver, AstResolver $astResolver, StaticTypeMapper $staticTypeMapper, TypeComparator $typeComparator) { $this->nodeNameResolver = $nodeNameResolver; $this->astResolver = $astResolver; + $this->staticTypeMapper = $staticTypeMapper; + $this->typeComparator = $typeComparator; } /** * @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\FuncCall $call @@ -46,7 +62,21 @@ final class CallerParamMatcher if (!$callParam instanceof Param) { 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 { diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 459ab76f645..3d7ff711621 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '6f330105bef70bec222f1cc6f86c09b137dff44f'; + public const PACKAGE_VERSION = '97d095f5bd3fe561dafd06f56f898f0aec535ba0'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-10-28 19:40:43'; + public const RELEASE_DATE = '2023-10-28 23:32:49'; /** * @var int */