-
-
Notifications
You must be signed in to change notification settings - Fork 683
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
[CodeQuality] Add DateTimeToDateTimeInterfaceRector #4791
Merged
TomasVotruba
merged 6 commits into
rectorphp:master
from
simivar:fature/datetime-to-datetimeinterface-rector
Dec 5, 2020
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f310bee
Add DateTimeToDateTimeInterfaceRector
simivar 4f7af2a
Fix test files
simivar f0beeb8
Fix test files
simivar b6466d7
Change from in_array to isNames
simivar 6b0db57
Add DateTimeInterface go PhpVersionFeature VO
simivar 1d9fdcd
Update src/ValueObject/PhpVersionFeature.php
TomasVotruba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
rules/code-quality/src/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\ClassMethod; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\NullableType; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\Type\NullType as PHPStanNullType; | ||
use PHPStan\Type\ObjectType as PHPStanObjectType; | ||
use PHPStan\Type\UnionType as PHPStanUnionType; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\NodeTypeResolver\NodeTypeResolver; | ||
|
||
/** | ||
* @see \Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\DateTimeToDateTimeInterfaceRectorTest | ||
*/ | ||
final class DateTimeToDateTimeInterfaceRector extends AbstractRector | ||
{ | ||
private const METHODS_MAP = [ | ||
'add', 'modify', '__set_state', 'setDate', 'setISODate', 'setTime', 'setTimestamp', 'setTimezone', 'sub', | ||
]; | ||
|
||
/** | ||
* @var NodeTypeResolver | ||
*/ | ||
private $nodeTypeResolver; | ||
|
||
public function __construct(NodeTypeResolver $nodeTypeResolver) | ||
{ | ||
$this->nodeTypeResolver = $nodeTypeResolver; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Changes DateTime type-hint to DateTimeInterface', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass { | ||
public function methodWithDateTime(\DateTime $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass { | ||
/** | ||
* @param \DateTime|\DateTimeImmutable $dateTime | ||
*/ | ||
public function methodWithDateTime(\DateTimeInterface $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$isModifiedNode = false; | ||
simivar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foreach ($node->getParams() as $param) { | ||
if (! $this->isDateTimeParam($param)) { | ||
continue; | ||
} | ||
|
||
$this->refactorParamTypeHint($param); | ||
$this->refactorParamDocBlock($param, $node); | ||
$this->refactorMethodCalls($param, $node); | ||
$isModifiedNode = true; | ||
} | ||
|
||
if (! $isModifiedNode) { | ||
return null; | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function refactorParamTypeHint(Param $param): void | ||
{ | ||
$dateTimeInterfaceType = new FullyQualified(DateTimeInterface::class); | ||
if ($param->type instanceof NullableType) { | ||
$param->type = new NullableType($dateTimeInterfaceType); | ||
return; | ||
} | ||
|
||
$param->type = $dateTimeInterfaceType; | ||
} | ||
|
||
private function refactorParamDocBlock(Param $param, ClassMethod $classMethod): void | ||
{ | ||
/** @var PhpDocInfo|null $phpDocInfo */ | ||
$phpDocInfo = $classMethod->getAttribute(AttributeKey::PHP_DOC_INFO); | ||
if ($phpDocInfo === null) { | ||
$phpDocInfo = $this->phpDocInfoFactory->createEmpty($classMethod); | ||
} | ||
|
||
$types = [new PHPStanObjectType(DateTime::class), new PHPStanObjectType(DateTimeImmutable::class)]; | ||
if ($param->type instanceof NullableType) { | ||
$types[] = new PHPStanNullType(); | ||
} | ||
|
||
$paramName = $this->getName($param->var); | ||
if ($paramName === null) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
$phpDocInfo->changeParamType(new PHPStanUnionType($types), $param, $paramName); | ||
} | ||
|
||
private function refactorMethodCalls(Param $param, ClassMethod $classMethod): void | ||
{ | ||
if ($classMethod->stmts === null) { | ||
return; | ||
} | ||
|
||
$this->traverseNodesWithCallable($classMethod->stmts, function (Node $node) use ($param) { | ||
if (!($node instanceof MethodCall)){ | ||
return; | ||
} | ||
|
||
$this->refactorMethodCall($param, $node); | ||
}); | ||
} | ||
|
||
private function refactorMethodCall(Param $param, MethodCall $methodCall): void | ||
{ | ||
$paramName = $this->getName($param->var); | ||
if ($paramName === null || $this->shouldSkipMethodCallRefactor($paramName, $methodCall)) { | ||
return; | ||
} | ||
|
||
$newAssignNode = new Assign(new Variable($paramName), $methodCall); | ||
|
||
/** @var Node $parentNode */ | ||
$parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE); | ||
if ($parentNode instanceof Arg) { | ||
$parentNode->value = $newAssignNode; | ||
return; | ||
} | ||
|
||
$parentNode->expr = $newAssignNode; | ||
} | ||
|
||
private function shouldSkipMethodCallRefactor(string $paramName, MethodCall $methodCall): bool | ||
{ | ||
if (! $this->isName($methodCall->var, $paramName)) { | ||
return true; | ||
} | ||
|
||
if (! in_array($this->getName($methodCall->name), self::METHODS_MAP)) { | ||
simivar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
|
||
$parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE) ; | ||
if ($parentNode === null) { | ||
return true; | ||
} | ||
|
||
return $parentNode instanceof Assign; | ||
} | ||
|
||
private function isDateTimeParam(Param $param): bool | ||
{ | ||
return $this->nodeTypeResolver->isObjectTypeOrNullableObjectType($param, DateTime::class); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...r/ClassMethod/DateTimeToDateTimeInterfaceRector/DateTimeToDateTimeInterfaceRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector; | ||
|
||
use Iterator; | ||
use Rector\CodeQuality\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class DateTimeToDateTimeInterfaceRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return DateTimeToDateTimeInterfaceRector::class; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ty/tests/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector/Fixture/fixture_fqn.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
class FixtureFqn { | ||
public function methodWithDateTime(\DateTime $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
class FixtureFqn { | ||
/** | ||
* @param \DateTime|\DateTimeImmutable $dateTime | ||
*/ | ||
public function methodWithDateTime(\DateTimeInterface $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...sts/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector/Fixture/fixture_nullable.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
class FixtureNullable { | ||
public function methodWithDateTime(?\DateTime $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
class FixtureNullable { | ||
/** | ||
* @param \DateTime|\DateTimeImmutable|null $dateTime | ||
*/ | ||
public function methodWithDateTime(?\DateTimeInterface $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...ty/tests/Rector/ClassMethod/DateTimeToDateTimeInterfaceRector/Fixture/fixture_use.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
use \DateTime; | ||
|
||
class FixtureUse { | ||
public function methodWithDateTime(DateTime $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
use \DateTime; | ||
|
||
class FixtureUse { | ||
/** | ||
* @param \DateTime|\DateTimeImmutable $dateTime | ||
*/ | ||
public function methodWithDateTime(\DateTimeInterface $dateTime) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
?> |
36 changes: 36 additions & 0 deletions
36
...ctor/ClassMethod/DateTimeToDateTimeInterfaceRector/Fixture/fixture_variable_usage.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
class FixtureVariableUsage { | ||
/** | ||
* @param mixed $mixed | ||
*/ | ||
public function methodWithDateTime($mixed, \DateTime $dateTime) | ||
{ | ||
$dateTime->modify('+1 day'); | ||
|
||
return $dateTime; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\ClassMethod\DateTimeToDateTimeInterfaceRector\Fixture; | ||
|
||
class FixtureVariableUsage { | ||
/** | ||
* @param mixed $mixed | ||
* @param \DateTime|\DateTimeImmutable $dateTime | ||
*/ | ||
public function methodWithDateTime($mixed, \DateTimeInterface $dateTime) | ||
{ | ||
$dateTime = $dateTime->modify('+1 day'); | ||
|
||
return $dateTime; | ||
} | ||
} | ||
|
||
?> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of map is this? Excluded, included or ...?
More specific naming could help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed it to
METHODS_RETURNING_CLASS_INSTANCE_MAP
.