rector/rules/Naming/Guard/DateTimeAtNamingConventionGuard.php
Tomas Votruba 54f8d6dc33 Updated Rector to commit da4d598bf0fc20b831e138407263b82645109bdf
da4d598bf0 [PHPStan 1.0] resolve FunctionReflection getFileName() from main interface (#1117)
2021-10-31 15:30:19 +00:00

54 lines
2.0 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Naming\Guard;
use DateTimeInterface;
use RectorPrefix20211031\Nette\Utils\Strings;
use PHPStan\Type\TypeWithClassName;
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;
final class DateTimeAtNamingConventionGuard implements \Rector\Naming\Contract\Guard\ConflictingNameGuardInterface
{
/**
* @var string
* @see https://regex101.com/r/1pKLgf/1/
*/
private const AT_NAMING_REGEX = '#[\\w+]At$#';
/**
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @var \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper
*/
private $typeUnwrapper;
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper $typeUnwrapper)
{
$this->nodeTypeResolver = $nodeTypeResolver;
$this->typeUnwrapper = $typeUnwrapper;
}
/**
* @param \Rector\Naming\Contract\RenameValueObjectInterface $renameValueObject
*/
public function isConflicting($renameValueObject) : bool
{
return $this->isDateTimeAtNamingConvention($renameValueObject);
}
private function isDateTimeAtNamingConvention(\Rector\Naming\ValueObject\PropertyRename $propertyRename) : bool
{
$type = $this->nodeTypeResolver->getType($propertyRename->getProperty());
$type = $this->typeUnwrapper->unwrapFirstObjectTypeFromUnionType($type);
if (!$type instanceof \PHPStan\Type\TypeWithClassName) {
return \false;
}
if (!\is_a($type->getClassName(), \DateTimeInterface::class, \true)) {
return \false;
}
return (bool) \RectorPrefix20211031\Nette\Utils\Strings::match($propertyRename->getCurrentName(), self::AT_NAMING_REGEX . '');
}
}