Skip to content

Commit

Permalink
[CodingStyle] Skip override parent method on UnSpreadOperatorRector (#…
Browse files Browse the repository at this point in the history
…2223)

* Add failing test fixture for UnSpreadOperatorRector

# Failing Test for UnSpreadOperatorRector

Based on https://getrector.org/demo/f83cf736-ff98-4bc4-86a8-4611c8dd8804

* Fixes #2220

* fixture

* [ci-review] Rector Rectify

Co-authored-by: Alies Lapatsin <lptn@users.noreply.github.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed May 4, 2022
1 parent e978256 commit 6327904
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector\Fixture;

use Rector\Tests\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector\Source\ParentClass;

final class SkipOverrideMethod extends ParentClass
{
public static function make(... $arguments): static
{
return new static();
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector\Source;

class ParentClass
{
public static function make(... $arguments): static
{
return new static();
}
}
30 changes: 15 additions & 15 deletions rules/CodingStyle/Rector/ClassMethod/UnSpreadOperatorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use Rector\CodingStyle\NodeAnalyzer\SpreadVariablesCollector;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -29,7 +28,8 @@ final class UnSpreadOperatorRector extends AbstractRector
public function __construct(
private readonly SpreadVariablesCollector $spreadVariablesCollector,
private readonly ReflectionResolver $reflectionResolver,
private readonly VendorLocationDetector $vendorLocationDetector
private readonly VendorLocationDetector $vendorLocationDetector,
private readonly ClassChildAnalyzer $classChildAnalyzer
) {
}

Expand Down Expand Up @@ -90,7 +90,17 @@ public function refactor(Node $node): ?Node

private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
{
if ($this->isInPHPUnitTestCase($classMethod)) {
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
return null;
}

if ($this->isInPHPUnitTestCase($classReflection, $classMethod)) {
return null;
}

$methodName = $this->nodeNameResolver->getName($classMethod);
if ($this->classChildAnalyzer->hasParentClassMethod($classReflection, $methodName)) {
return null;
}

Expand Down Expand Up @@ -216,22 +226,12 @@ private function hasUnpackedArgs(array $args): bool
return false;
}

private function isInPHPUnitTestCase(ClassMethod $classMethod): bool
private function isInPHPUnitTestCase(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

if (! $classMethod->isPublic()) {
return false;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->isSubclassOf('PHPUnit\Framework\TestCase');
}
}

0 comments on commit 6327904

Please sign in to comment.