|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\Expression; |
| 6 | + |
| 7 | +use PhpParser\Node\Stmt\Class_; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Arg; |
| 10 | +use PhpParser\Node\Expr; |
| 11 | +use PhpParser\Node\Expr\Array_; |
| 12 | +use PhpParser\Node\Expr\Assign; |
| 13 | +use PhpParser\Node\Expr\MethodCall; |
| 14 | +use PhpParser\Node\Expr\New_; |
| 15 | +use PhpParser\Node\Name\FullyQualified; |
| 16 | +use PhpParser\Node\Stmt\Expression; |
| 17 | +use PHPStan\Reflection\ReflectionProvider; |
| 18 | +use Rector\Doctrine\NodeAnalyzer\DoctrineEntityDetector; |
| 19 | +use Rector\PhpParser\AstResolver; |
| 20 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 21 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 22 | +use Rector\Rector\AbstractRector; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 24 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 25 | + |
| 26 | +/** |
| 27 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Expression\ConfiguredMockEntityToSetterObjectRector\ConfiguredMockEntityToSetterObjectRectorTest |
| 28 | + */ |
| 29 | +final class ConfiguredMockEntityToSetterObjectRector extends AbstractRector |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly ReflectionProvider $reflectionProvider, |
| 33 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 34 | + private readonly ValueResolver $valueResolver, |
| 35 | + private readonly AstResolver $astResolver, |
| 36 | + private readonly DoctrineEntityDetector $doctrineEntityDetector, |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + public function getRuleDefinition(): RuleDefinition |
| 41 | + { |
| 42 | + return new RuleDefinition( |
| 43 | + 'Change createConfigureMock() on Entity/Document object to direct new instance with setters', |
| 44 | + [ |
| 45 | + new CodeSample( |
| 46 | + <<<'CODE_SAMPLE' |
| 47 | +use PHPUnit\Framework\TestCase; |
| 48 | +
|
| 49 | +final class SomeTest extends TestClass |
| 50 | +{ |
| 51 | + public function test() |
| 52 | + { |
| 53 | + $someObject = $this->createConfiguredMock(SomeObject::class, [ |
| 54 | + 'name' => 'John', |
| 55 | + 'surname' => 'Doe', |
| 56 | + ]); |
| 57 | + } |
| 58 | +} |
| 59 | +CODE_SAMPLE |
| 60 | + , |
| 61 | + <<<'CODE_SAMPLE' |
| 62 | +use PHPUnit\Framework\TestCase; |
| 63 | +
|
| 64 | +final class SomeTest extends TestClass |
| 65 | +{ |
| 66 | + public function test() |
| 67 | + { |
| 68 | + $someObject = new SomeObject(); |
| 69 | + $someObject->setName('John'); |
| 70 | + $someObject->setSurname('Doe'); |
| 71 | + } |
| 72 | +} |
| 73 | +CODE_SAMPLE |
| 74 | + ), |
| 75 | + ] |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return array<class-string<Node>> |
| 81 | + */ |
| 82 | + public function getNodeTypes(): array |
| 83 | + { |
| 84 | + return [Expression::class]; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param Expression $node |
| 89 | + * @return Expression[]|null |
| 90 | + */ |
| 91 | + public function refactor(Node $node): ?array |
| 92 | + { |
| 93 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + if (! $node->expr instanceof Assign) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + $assign = $node->expr; |
| 102 | + if (! $assign->expr instanceof MethodCall) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + $objectVariable = $assign->var; |
| 107 | + |
| 108 | + $methodCall = $assign->expr; |
| 109 | + if (! $this->isName($methodCall->name, 'createConfiguredMock')) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + if ($methodCall->isFirstClassCallable()) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + $mockedClassArg = $methodCall->getArgs()[0]; |
| 118 | + |
| 119 | + $doctrineClass = $this->matchDoctrineClassName($mockedClassArg->value); |
| 120 | + if (! is_string($doctrineClass)) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + $definedGettersArg = $methodCall->getArgs()[1]; |
| 125 | + if (! $definedGettersArg->value instanceof Array_) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + $assign->expr = new New_(new FullyQualified($doctrineClass)); |
| 130 | + |
| 131 | + $setterExpressions = $this->createEntitySetterExpressions($definedGettersArg->value, $objectVariable); |
| 132 | + |
| 133 | + return array_merge([$node], $setterExpressions); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return Expression[] |
| 138 | + */ |
| 139 | + private function createEntitySetterExpressions(Array_ $definedGettersArray, Expr $expr): array |
| 140 | + { |
| 141 | + $setterExpressions = []; |
| 142 | + |
| 143 | + foreach ($definedGettersArray->items as $arrayItem) { |
| 144 | + if (! $arrayItem->key instanceof Expr) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + $getterName = $this->valueResolver->getValue($arrayItem->key); |
| 149 | + if (! is_string($getterName)) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + // remove "get" prefix |
| 154 | + if (! str_starts_with($getterName, 'get')) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + $setterName = 'set' . substr($getterName, 3); |
| 159 | + |
| 160 | + $setterMethodCall = new MethodCall($expr, $setterName, [new Arg($arrayItem->value)]); |
| 161 | + $setterExpressions[] = new Expression($setterMethodCall); |
| 162 | + } |
| 163 | + |
| 164 | + return $setterExpressions; |
| 165 | + } |
| 166 | + |
| 167 | + private function matchDoctrineClassName(Expr $expr): string|null |
| 168 | + { |
| 169 | + $mockedClassValue = $this->valueResolver->getValue($expr); |
| 170 | + if (! is_string($mockedClassValue)) { |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + if (! $this->reflectionProvider->hasClass($mockedClassValue)) { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + $mockedClass = $this->astResolver->resolveClassFromName($mockedClassValue); |
| 179 | + if (! $mockedClass instanceof Class_) { |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + if (! $this->doctrineEntityDetector->detect($mockedClass)) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + return $mockedClassValue; |
| 188 | + } |
| 189 | +} |
0 commit comments