Skip to content

Commit

Permalink
Fix few rules reporting in case of no change (#3090)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 24, 2022
1 parent b981b37 commit 7842486
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
13 changes: 11 additions & 2 deletions rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class ArgumentRemoverRector extends AbstractRector implements Configurable
*/
private array $removedArguments = [];

private bool $hasChanged = false;

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -60,8 +62,10 @@ public function getNodeTypes(): array
/**
* @param MethodCall|StaticCall|ClassMethod $node
*/
public function refactor(Node $node): MethodCall | StaticCall | ClassMethod
public function refactor(Node $node): MethodCall | StaticCall | ClassMethod | null
{
$this->hasChanged = false;

foreach ($this->removedArguments as $removedArgument) {
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
Expand All @@ -77,7 +81,11 @@ public function refactor(Node $node): MethodCall | StaticCall | ClassMethod
$this->processPosition($node, $removedArgument);
}

return $node;
if ($this->hasChanged) {
return $node;
}

return null;
}

/**
Expand Down Expand Up @@ -119,6 +127,7 @@ private function processPosition(
}

if ($this->isArgumentValueMatch($node->args[$argumentRemover->getPosition()], $match)) {
$this->hasChanged = true;
$this->nodeRemover->removeArg($node, $argumentRemover->getPosition());
}
}
Expand Down
22 changes: 12 additions & 10 deletions rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ final class RenamePropertyRector extends AbstractRector implements ConfigurableR
*/
private array $renamedProperties = [];

private bool $hasChanged = false;

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replaces defined old properties by new ones.', [
Expand All @@ -55,7 +57,15 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
if ($node instanceof ClassLike) {
return $this->processFromClassLike($node);
foreach ($this->renamedProperties as $renamedProperty) {
$this->renameProperty($node, $renamedProperty);
}

if ($this->hasChanged) {
return $node;
}

return null;
}

return $this->processFromPropertyFetch($node);
Expand All @@ -70,15 +80,6 @@ public function configure(array $configuration): void
$this->renamedProperties = $configuration;
}

private function processFromClassLike(ClassLike $classLike): ClassLike
{
foreach ($this->renamedProperties as $renamedProperty) {
$this->renameProperty($classLike, $renamedProperty);
}

return $classLike;
}

private function renameProperty(ClassLike $classLike, RenameProperty $renameProperty): void
{
$classLikeName = (string) $this->nodeNameResolver->getName($classLike);
Expand All @@ -105,6 +106,7 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp
return;
}

$this->hasChanged = true;
$property->props[0]->name = new VarLikeIdentifier($newProperty);
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Transform/Rector/ClassMethod/WrapReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function refactor(Node $node): ?Node
return $this->wrap($node, $typeMethodWrap->isArrayWrap());
}

return $node;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function refactor(Node $node): ?Node
return new New_(new FullyQualified($class), $node->args);
}

return $node;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function getNodeTypes(): array
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($this->methodVisibilities === []) {
return null;
}

$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($scope);
if ($parentClassName === null) {
return null;
Expand All @@ -109,7 +113,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return $node;
}

return $node;
return null;
}

/**
Expand Down

0 comments on commit 7842486

Please sign in to comment.