Skip to content

Commit 7feb6c8

Browse files
[Naming] Re-use repetitive DateTimeInterface object type check on services into PropertyNaming service (#6926)
* [Naming] Re-use repetitive DateTimeInterface object type check on services * fix * clean up * [ci-review] Rector Rectify * Fix test --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent f00028f commit 7feb6c8

5 files changed

Lines changed: 9 additions & 33 deletions

File tree

rules-tests/Naming/Rector/Class_/RenamePropertyToMatchTypeRector/Fixture/keep_date_time.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixt
44

55
final class KeepDateTime
66
{
7-
private ?DateTime $timestamp = null;
7+
private ?\DateTime $timestamp = null;
88

99
public function setValue(
1010
\DateTime $timestamp,

rules/Naming/ExpectedNameResolver/MatchParamTypeExpectedNameResolver.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Param;
9-
use PHPStan\Type\ObjectType;
109
use Rector\Naming\Naming\PropertyNaming;
1110
use Rector\Naming\ValueObject\ExpectedName;
12-
use Rector\NodeTypeResolver\NodeTypeResolver;
1311
use Rector\StaticTypeMapper\StaticTypeMapper;
1412

1513
final readonly class MatchParamTypeExpectedNameResolver
1614
{
1715
public function __construct(
1816
private StaticTypeMapper $staticTypeMapper,
1917
private PropertyNaming $propertyNaming,
20-
private NodeTypeResolver $nodeTypeResolver,
2118
) {
2219
}
2320

@@ -28,12 +25,6 @@ public function resolve(Param $param): ?string
2825
return null;
2926
}
3027

31-
// include nullable too
32-
// skip date time + date time interface, as should be kept
33-
if ($this->nodeTypeResolver->isObjectType($param->type, new ObjectType('DateTimeInterface'))) {
34-
return null;
35-
}
36-
3728
$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
3829
$expectedName = $this->propertyNaming->getExpectedNameFromType($staticType);
3930

rules/Naming/Naming/ExpectedNameResolver.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ public function resolveForCall(MethodCall | StaticCall | FuncCall $expr): ?strin
118118
return null;
119119
}
120120

121-
if ($this->isDateTimeType($returnedType)) {
122-
return null;
123-
}
124-
125121
$expectedName = $this->propertyNaming->getExpectedNameFromType($returnedType);
122+
126123
if ($expectedName instanceof ExpectedName) {
127124
return $expectedName->getName();
128125
}
@@ -211,17 +208,4 @@ private function resolveReturnTypeFromArrayType(ArrayType $arrayType): ?Type
211208

212209
return $arrayType->getIterableValueType();
213210
}
214-
215-
/**
216-
* Skip date time, as custom naming
217-
*/
218-
private function isDateTimeType(ObjectType $objectType): bool
219-
{
220-
if ($objectType->isInstanceOf('DateTimeInterface')->yes()) {
221-
return true;
222-
}
223-
224-
return $objectType->isInstanceOf('DateTime')
225-
->yes();
226-
}
227211
}

rules/Naming/Naming/PropertyNaming.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\ThisType;
1212
use PHPStan\Type\Type;
1313
use PHPStan\Type\TypeCombinator;
14+
use Rector\Enum\ClassName;
1415
use Rector\Exception\ShouldNotHappenException;
1516
use Rector\Naming\RectorNamingInflector;
1617
use Rector\Naming\ValueObject\ExpectedName;
@@ -71,6 +72,8 @@ public function getExpectedNameFromMethodName(string $methodName): ?ExpectedName
7172

7273
public function getExpectedNameFromType(Type $type): ?ExpectedName
7374
{
75+
$type = TypeCombinator::removeNull($type);
76+
7477
// keep collections untouched
7578
if ($type instanceof ObjectType) {
7679
if ($type->isInstanceOf('Doctrine\Common\Collections\Collection')->yes()) {
@@ -80,6 +83,10 @@ public function getExpectedNameFromType(Type $type): ?ExpectedName
8083
if ($type->isInstanceOf('Illuminate\Support\Collection')->yes()) {
8184
return null;
8285
}
86+
87+
if ($type->isInstanceOf(ClassName::DATE_TIME_INTERFACE)->yes()) {
88+
return null;
89+
}
8390
}
8491

8592
$className = $this->resolveClassNameFromType($type);
@@ -278,7 +285,6 @@ private function normalizeShortClassName(string $shortClassName): string
278285

279286
private function resolveClassNameFromType(Type $type): ?string
280287
{
281-
$type = TypeCombinator::removeNull($type);
282288
$className = ClassNameFromObjectTypeResolver::resolve($type);
283289

284290
if ($className === null) {

rules/Naming/RenameGuard/PropertyRenameGuard.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ public function shouldSkip(PropertyRename $propertyRename): bool
3232
return true;
3333
}
3434

35-
// skip date times, as often custom named and using "dateTime" does not bring any value to code
36-
if ($propertyRename->getExpectedName() === 'dateTime') {
37-
return true;
38-
}
39-
4035
if ($this->dateTimeAtNamingConventionGuard->isConflicting($propertyRename)) {
4136
return true;
4237
}

0 commit comments

Comments
 (0)