2020-09-28 20:29:37 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Naming\Guard;
|
2020-09-28 20:29:37 +02:00
|
|
|
|
|
|
|
use DateTimeInterface;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\TypeWithClassName;
|
|
|
|
use Rector\Core\Util\StringUtils;
|
|
|
|
use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface;
|
|
|
|
use Rector\Naming\Contract\RenameValueObjectInterface;
|
|
|
|
use Rector\Naming\ValueObject\PropertyRename;
|
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
|
2021-11-28 17:01:20 +00:00
|
|
|
/**
|
|
|
|
* @implements ConflictingNameGuardInterface<PropertyRename>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class DateTimeAtNamingConventionGuard implements ConflictingNameGuardInterface
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
2020-09-28 20:29:37 +02:00
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper
|
2020-09-28 20:29:37 +02:00
|
|
|
*/
|
|
|
|
private $typeUnwrapper;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(NodeTypeResolver $nodeTypeResolver, TypeUnwrapper $typeUnwrapper)
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
|
|
$this->typeUnwrapper = $typeUnwrapper;
|
|
|
|
}
|
2020-10-03 21:18:12 +02:00
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param PropertyRename $renameValueObject
|
2020-10-03 21:18:12 +02:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function isConflicting(RenameValueObjectInterface $renameValueObject) : bool
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
|
|
|
return $this->isDateTimeAtNamingConvention($renameValueObject);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isDateTimeAtNamingConvention(PropertyRename $propertyRename) : bool
|
2020-09-28 20:29:37 +02:00
|
|
|
{
|
2021-10-07 19:40:12 +00:00
|
|
|
$type = $this->nodeTypeResolver->getType($propertyRename->getProperty());
|
2020-09-28 20:29:37 +02:00
|
|
|
$type = $this->typeUnwrapper->unwrapFirstObjectTypeFromUnionType($type);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$type instanceof TypeWithClassName) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!\is_a($type->getClassName(), DateTimeInterface::class, \true)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
2022-07-20 05:00:44 +00:00
|
|
|
return StringUtils::isMatch($propertyRename->getCurrentName(), \Rector\Naming\Guard\BreakingVariableRenameGuard::AT_NAMING_REGEX . '');
|
2020-09-28 20:29:37 +02:00
|
|
|
}
|
|
|
|
}
|