Skip to content

Commit

Permalink
[Privatization][TypeDeclaration] Skip inside class with @Final docblo…
Browse files Browse the repository at this point in the history
…ck on PrivatizeFinalClassMethodRector and AddVoidReturnTypeWhereNoReturnRector (#3190)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 12, 2022
1 parent 96c457b commit bea2e12
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;

/**
* @final
*/
class SkipFinalDocblock
{
protected function someMethod()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector\Fixture;

/**
* @final
*/
class SkipFinalDocblock
{
protected function someMethod()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Rector\Privatization\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -72,11 +74,12 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $classReflection->isClass()) {
$class = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $class instanceof Class_) {
return null;
}

if (! $classReflection->isFinal()) {
if (! $class->isFinal()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -17,6 +18,7 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnVendorLockResolver;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand Down Expand Up @@ -176,6 +178,11 @@ private function isInsideFinalClass(ClassMethod $classMethod): bool
return false;
}

return $classReflection->isFinal();
$node = $classMethod->getAttribute(AttributeKey::PARENT_NODE);
if (! $node instanceof Class_) {
return false;
}

return $node->isFinal();
}
}

0 comments on commit bea2e12

Please sign in to comment.