Skip to content

Commit

Permalink
Skip DateTime vs DateTimeImmutable vs DateTimeInterface conflicts (#54)
Browse files Browse the repository at this point in the history
* add fixture with date time diff

* skip date time vs date time immutable vs date time interface conflicts without any value
  • Loading branch information
TomasVotruba authored Jan 4, 2025
1 parent 1e37da5 commit 9e6a241
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Printer/CollectorMetadataPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\TypePerfect\Tests\Rules\NarrowPublicClassMethodParamTypeRule\Fixture;

final class SkipDateTimeMix
{
public function markDate(
\DateTimeInterface $date
): bool
{
return true;
}

static public function run(): void
{
$skipDateTimeMix = new SkipDateTimeMix();
$skipDateTimeMix->markDate(
new \DateTimeImmutable('15:30')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e6a241

Please sign in to comment.