Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip DateTime vs DateTimeImmutable vs DateTimeInterface conflicts #54

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading