2021-09-19 09:13:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Arguments\NodeAnalyzer;
|
2021-09-19 09:13:32 +00:00
|
|
|
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2021-09-19 09:13:32 +00:00
|
|
|
final class ChangedArgumentsDetector
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2021-09-19 09:13:32 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-09-19 11:08:13 +00:00
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
2021-09-19 09:13:32 +00:00
|
|
|
*/
|
2021-09-19 11:08:13 +00:00
|
|
|
private $staticTypeMapper;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-09-19 11:08:13 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
|
|
|
*/
|
|
|
|
private $typeComparator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ValueResolver $valueResolver, StaticTypeMapper $staticTypeMapper, TypeComparator $typeComparator)
|
2021-09-19 09:13:32 +00:00
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
2021-09-19 11:08:13 +00:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
|
|
|
$this->typeComparator = $typeComparator;
|
2021-09-19 09:13:32 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function isDefaultValueChanged(Param $param, $value) : bool
|
2021-09-19 09:13:32 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$param->default instanceof Expr) {
|
2021-09-19 09:13:32 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return !$this->valueResolver->isValue($param->default, $value);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function isTypeChanged(Param $param, ?Type $newType) : bool
|
2021-09-19 09:13:32 +00:00
|
|
|
{
|
|
|
|
if ($param->type === null) {
|
|
|
|
return \false;
|
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$newType instanceof Type) {
|
2021-09-19 09:13:32 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-09-19 11:08:13 +00:00
|
|
|
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
|
|
|
|
return !$this->typeComparator->areTypesEqual($currentParamType, $newType);
|
2021-09-19 09:13:32 +00:00
|
|
|
}
|
|
|
|
}
|