Skip to content

Commit

Permalink
[Naming] Skip DateTimeImmutable on RenamePropertyToMatchTypeRector (#…
Browse files Browse the repository at this point in the history
…5340)

* [Naming] Skip DateTimeImmutable on RenamePropertyToMatchTypeRector

* fix

* use isObjectType()
  • Loading branch information
samsonasik committed Dec 8, 2023
1 parent 0a2dc3d commit 14f1128
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixture;

final class SkipDateTimeImmutable
{
public function __construct(
public \DateTimeImmutable $changed,
) {}
}
Expand Up @@ -9,13 +9,15 @@
use PHPStan\Type\Type;
use Rector\Naming\Naming\PropertyNaming;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;

final class MatchParamTypeExpectedNameResolver
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly PropertyNaming $propertyNaming,
private readonly NodeTypeResolver $nodeTypeResolver,
) {
}

Expand All @@ -28,9 +30,9 @@ public function resolve(Param $param): ?string

$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);

// include nullable too
// include nullabgitle too
// skip date time + date time interface, as should be kept
if ($this->isDateTimeType($staticType)) {
if ($this->nodeTypeResolver->isObjectType($param->type, new ObjectType('DateTimeInterface'))) {
return null;
}

Expand All @@ -41,14 +43,4 @@ public function resolve(Param $param): ?string

return $expectedName->getName();
}

private function isDateTimeType(Type $type): bool
{
if ($type->isSuperTypeOf(new ObjectType('DateTimeInterface'))->yes()) {
return true;
}

return $type->isSuperTypeOf(new ObjectType('DateTime'))
->yes();
}
}

0 comments on commit 14f1128

Please sign in to comment.