diff --git a/src/Printer/CollectorMetadataPrinter.php b/src/Printer/CollectorMetadataPrinter.php index afb8bc7d..b602a413 100644 --- a/src/Printer/CollectorMetadataPrinter.php +++ b/src/Printer/CollectorMetadataPrinter.php @@ -71,7 +71,10 @@ public function printArgTypesAsString(MethodCall $methodCall, ExtendedMethodRefl } } - $stringArgTypes[] = $this->printTypeToString($argType); + $printedArgType = $this->printTypeToString($argType); + $printedArgType = $this->normalizeDateTime($printedArgType); + + $stringArgTypes[] = $printedArgType; } return implode('|', $stringArgTypes); @@ -101,6 +104,9 @@ public function printParamTypesToString(ClassMethod $classMethod, ?string $class $printedParamType = ltrim($printedParamType, '\\'); $printedParamType = str_replace('|\\', '|', $printedParamType); + // to avoid DateTime vs DateTimeImmutable vs DateTimeInterface conflicts + $printedParamType = $this->normalizeDateTime($printedParamType); + $printedParamTypes[] = $printedParamType; } @@ -182,4 +188,17 @@ private function printTypeToString(Type $type): string return $type->describe(VerbosityLevel::typeOnly()); } + + private function normalizeDateTime(string $printedType): string + { + if ($printedType === 'DateTimeImmutable') { + return 'DateTimeInterface'; + } + + if ($printedType === 'DateTime') { + return 'DateTimeInterface'; + } + + return $printedType; + } } diff --git a/tests/Rules/NarrowPublicClassMethodParamTypeRule/Fixture/SkipDateTimeMix.php b/tests/Rules/NarrowPublicClassMethodParamTypeRule/Fixture/SkipDateTimeMix.php new file mode 100644 index 00000000..c64230a9 --- /dev/null +++ b/tests/Rules/NarrowPublicClassMethodParamTypeRule/Fixture/SkipDateTimeMix.php @@ -0,0 +1,23 @@ +markDate( + new \DateTimeImmutable('15:30') + ); + } +} diff --git a/tests/Rules/NarrowPublicClassMethodParamTypeRule/NarrowPublicClassMethodParamTypeRuleTest.php b/tests/Rules/NarrowPublicClassMethodParamTypeRule/NarrowPublicClassMethodParamTypeRuleTest.php index 88578e0c..fa6295bb 100644 --- a/tests/Rules/NarrowPublicClassMethodParamTypeRule/NarrowPublicClassMethodParamTypeRuleTest.php +++ b/tests/Rules/NarrowPublicClassMethodParamTypeRule/NarrowPublicClassMethodParamTypeRuleTest.php @@ -26,6 +26,7 @@ public function testRule(array $filePaths, array $expectedErrorsWithLines): void public static function provideData(): Iterator { + yield [[__DIR__ . '/Fixture/SkipDateTimeMix.php'], []]; yield [[__DIR__ . '/Fixture/SkipNonPublicClassMethod.php'], []]; // skip first class callables as anything can be passed there