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

[Doctrine] Step #3 - getUuid/setUuid method calls to id values #2011

Merged
merged 4 commits into from
Sep 21, 2019
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"phpstan/phpdoc-parser": "^0.3.5",
"phpstan/phpstan": "^0.11.13",
"phpstan/phpstan-phpunit": "^0.11.2",
"ramsey/uuid": "^3.8",
"sebastian/diff": "^3.0",
"symfony/console": "^3.4|^4.2",
"symfony/dependency-injection": "^3.4|^4.2",
Expand Down
3 changes: 3 additions & 0 deletions config/set/doctrine/doctrine-id-to-uuid-step-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
Rector\Doctrine\Rector\MethodCall\ChangeSetIdToUuidValueRector: ~
Rector\Doctrine\Rector\MethodCall\ChangeGetUuidMethodCallToGetIdRector: ~
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private function isSmallerOrGreater(array $condExprs, string $keyValueName, stri
private function useForeachVariableInStmts(Expr $expr, array $stmts): void
{
if ($this->keyValueName === null) {
throw new ShouldNotHappenException(__METHOD__ . '() on line ' . __LINE__);
throw new ShouldNotHappenException();
}

$this->traverseNodesWithCallable($stmts, function (Node $node) use ($expr): ?Expr {
Expand Down
2 changes: 1 addition & 1 deletion packages/CodingStyle/src/Naming/ClassNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getShortName($name): string
if ($name instanceof Name) {
$name = $this->nameResolver->getName($name);
if ($name === null) {
throw new ShouldNotHappenException(__METHOD__ . '() on line ' . __LINE__);
throw new ShouldNotHappenException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function transformArrayToYieldsOnMethodNode(ClassMethod $classMethod, Ar
// remove whole return node
$parentNode = $arrayNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode === null) {
throw new ShouldNotHappenException(__METHOD__ . '() on line ' . __LINE__);
throw new ShouldNotHappenException();
}

$this->removeNode($parentNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private function resolveUsedNames(Node $searchNode): void

$parentNode = $nameNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode === null) {
throw new ShouldNotHappenException(__METHOD__ . '() on line ' . __LINE__);
throw new ShouldNotHappenException();
}

$this->resolvedNodeNames[$originalName->toString()][] = [$nameNode, $parentNode];
Expand Down
2 changes: 1 addition & 1 deletion packages/DeadCode/src/Data/VariableNodeUseInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getParentNode(): Node
{
$parentNode = $this->variable->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode === null) {
throw new ShouldNotHappenException(__METHOD__ . '() on line ' . __LINE__);
throw new ShouldNotHappenException();
}

return $parentNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private function isParentClassMethodVisibilityOverride(ClassMethod $classMethod,

$parentClassName = get_parent_class($className);
if ($parentClassName === false) {
throw new ShouldNotHappenException(__METHOD__);
throw new ShouldNotHappenException();
}

/** @var string $methodName */
Expand Down
5 changes: 4 additions & 1 deletion packages/Doctrine/src/AbstractRector/DoctrineTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ protected function isDoctrineProperty(Property $property): bool
return $this->doctrineDocBlockResolver->isDoctrineProperty($property);
}

protected function isDoctrineEntityClass(Class_ $class): bool
/**
* @param Class_|string $class
*/
protected function isDoctrineEntityClass($class): bool
{
return $this->doctrineDocBlockResolver->isDoctrineEntityClass($class);
}
Expand Down
44 changes: 39 additions & 5 deletions packages/Doctrine/src/PhpDocParser/DoctrineDocBlockResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rector\Doctrine\PhpDocParser;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
Expand All @@ -11,8 +12,12 @@
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\ColumnTagValueNode;
use Rector\DoctrinePhpDocParser\Ast\PhpDoc\Property_\IdTagValueNode;
use Rector\DoctrinePhpDocParser\Contract\Ast\PhpDoc\DoctrineRelationTagValueNodeInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use ReflectionClass;

final class DoctrineDocBlockResolver
{
Expand All @@ -21,19 +26,48 @@ final class DoctrineDocBlockResolver
*/
private $docBlockManipulator;

public function __construct(DocBlockManipulator $docBlockManipulator)
/**
* @var ParsedNodesByType
*/
private $parsedNodesByType;

public function __construct(DocBlockManipulator $docBlockManipulator, ParsedNodesByType $parsedNodesByType)
{
$this->docBlockManipulator = $docBlockManipulator;
$this->parsedNodesByType = $parsedNodesByType;
}

public function isDoctrineEntityClass(Class_ $class): bool
/**
* @param Class_|string $class
*/
public function isDoctrineEntityClass($class): bool
{
$classPhpDocInfo = $this->getPhpDocInfo($class);
if ($classPhpDocInfo === null) {
if ($class instanceof Class_) {
$classPhpDocInfo = $this->getPhpDocInfo($class);
if ($classPhpDocInfo === null) {
return false;
}

return (bool) $classPhpDocInfo->getByType(EntityTagValueNode::class);
}

if (is_string($class)) {
if (ClassExistenceStaticHelper::doesClassLikeExist($class)) {
$classNode = $this->parsedNodesByType->findClass($class);
if ($classNode) {
return $this->isDoctrineEntityClass($classNode);
}

$reflectionClass = new ReflectionClass($class);

// dummy check of 3rd party code without running it
return Strings::contains((string) $reflectionClass->getDocComment(), '@ORM\Entity');
}

return false;
}

return (bool) $classPhpDocInfo->getByType(EntityTagValueNode::class);
throw new ShouldNotHappenException();
}

public function isDoctrineEntityClassWithIdProperty(Class_ $class): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php declare(strict_types=1);

namespace Rector\Doctrine\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\Doctrine\Tests\Rector\MethodCall\ChangeGetUuidMethodCallToGetIdRector\ChangeGetUuidMethodCallToGetIdRectorTest
*/
final class ChangeGetUuidMethodCallToGetIdRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change getUuid() method call to getId()', [
new CodeSample(
<<<'PHP'
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

class SomeClass
{
public function run()
{
$buildingFirst = new Building();

return $buildingFirst->getUuid()->toString();
}
}

/**
* @ORM\Entity
*/
class UuidEntity
{
private $uuid;
public function getUuid(): UuidInterface
{
return $this->uuid;
}
}
PHP
,
<<<'PHP'
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

class SomeClass
{
public function run()
{
$buildingFirst = new Building();

return $buildingFirst->getId()->toString();
}
}

/**
* @ORM\Entity
*/
class UuidEntity
{
private $uuid;
public function getUuid(): UuidInterface
{
return $this->uuid;
}
}
PHP
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

$node->name = new Identifier('getId');

return $node;
}

private function shouldSkip(Node\Expr\MethodCall $methodCall): bool
{
if (! $this->isName($methodCall->name, 'getUuid')) {
return true;
}

$methodVarObjectType = $this->getObjectType($methodCall->var);
if (! $methodVarObjectType instanceof ObjectType) {
return true;
}

return ! $this->isDoctrineEntityClass($methodVarObjectType->getClassName());
}
}
Loading