Skip to content
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

This file was deleted.

This file was deleted.

This file was deleted.

77 changes: 10 additions & 67 deletions rules/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,19 @@
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\VarLikeIdentifier;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SuffixMockObjectPropertyRector\SuffixMockObjectPropertyRectorTest
* @deprecated This rule is deprecated, as renaming properties can produce invalid variable names and collide with
* existing ones. Use a coding standard to enforce the "Mock" suffix instead.
*/
final class SuffixMockObjectPropertyRector extends AbstractRector
final class SuffixMockObjectPropertyRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MockObjectPropertyDetector $mockObjectPropertyDetector
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -88,59 +79,11 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

$hasChanged = false;

foreach ($node->getProperties() as $property) {
if (! $this->mockObjectPropertyDetector->detect($property)) {
continue;
}

$propertyName = $this->getName($property);
if (str_ends_with($propertyName, 'Mock')) {
continue;
}

$newPropertyName = $propertyName . 'Mock';
$property->props[0]->name = new VarLikeIdentifier($newPropertyName);

$this->renamePropertyUsagesInClass($node, $propertyName, $newPropertyName);
$hasChanged = true;
}

if (! $hasChanged) {
return null;
}

return $node;
}

private function renamePropertyUsagesInClass(Class_ $class, string $oldPropertyName, string $newPropertyName): void
{
$this->traverseNodesWithCallable($class, function (Node $node) use (
$oldPropertyName,
$newPropertyName
): ?Node {
if (! $node instanceof PropertyFetch) {
return null;
}

// is local property?
if (! $node->var instanceof Variable && ! $this->isName($node->var, 'this')) {
return null;
}

if (! $this->isName($node->name, $oldPropertyName)) {
return null;
}

$node->name = new Identifier($newPropertyName);
return $node;
});
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated, as renaming properties can produce invalid variable names. Use a coding standard to enforce the "Mock" suffix instead.',
self::class,
));
}
}
Loading