Skip to content

Commit 9e6a241

Browse files
authored
Skip DateTime vs DateTimeImmutable vs DateTimeInterface conflicts (#54)
* add fixture with date time diff * skip date time vs date time immutable vs date time interface conflicts without any value
1 parent 1e37da5 commit 9e6a241

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/Printer/CollectorMetadataPrinter.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public function printArgTypesAsString(MethodCall $methodCall, ExtendedMethodRefl
7171
}
7272
}
7373

74-
$stringArgTypes[] = $this->printTypeToString($argType);
74+
$printedArgType = $this->printTypeToString($argType);
75+
$printedArgType = $this->normalizeDateTime($printedArgType);
76+
77+
$stringArgTypes[] = $printedArgType;
7578
}
7679

7780
return implode('|', $stringArgTypes);
@@ -101,6 +104,9 @@ public function printParamTypesToString(ClassMethod $classMethod, ?string $class
101104
$printedParamType = ltrim($printedParamType, '\\');
102105
$printedParamType = str_replace('|\\', '|', $printedParamType);
103106

107+
// to avoid DateTime vs DateTimeImmutable vs DateTimeInterface conflicts
108+
$printedParamType = $this->normalizeDateTime($printedParamType);
109+
104110
$printedParamTypes[] = $printedParamType;
105111
}
106112

@@ -182,4 +188,17 @@ private function printTypeToString(Type $type): string
182188

183189
return $type->describe(VerbosityLevel::typeOnly());
184190
}
191+
192+
private function normalizeDateTime(string $printedType): string
193+
{
194+
if ($printedType === 'DateTimeImmutable') {
195+
return 'DateTimeInterface';
196+
}
197+
198+
if ($printedType === 'DateTime') {
199+
return 'DateTimeInterface';
200+
}
201+
202+
return $printedType;
203+
}
185204
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Fixture;
6+
7+
final class SkipDateTimeMix
8+
{
9+
public function markDate(
10+
\DateTimeInterface $date
11+
): bool
12+
{
13+
return true;
14+
}
15+
16+
static public function run(): void
17+
{
18+
$skipDateTimeMix = new SkipDateTimeMix();
19+
$skipDateTimeMix->markDate(
20+
new \DateTimeImmutable('15:30')
21+
);
22+
}
23+
}

tests/Rules/NarrowPublicClassMethodParamTypeRule/NarrowPublicClassMethodParamTypeRuleTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function testRule(array $filePaths, array $expectedErrorsWithLines): void
2626

2727
public static function provideData(): Iterator
2828
{
29+
yield [[__DIR__ . '/Fixture/SkipDateTimeMix.php'], []];
2930
yield [[__DIR__ . '/Fixture/SkipNonPublicClassMethod.php'], []];
3031

3132
// skip first class callables as anything can be passed there

0 commit comments

Comments
 (0)