Updated Rector to commit a44e8351e20e9ad994cd0ff130575b9c3def45c5

a44e8351e2 [Privatization] Allow privatize protected promoted property on PrivatizeFinalClassPropertyRector (#6360)
This commit is contained in:
Tomas Votruba 2024-10-08 08:58:13 +00:00
parent 91ba9919c7
commit 842acb42c9
5 changed files with 40 additions and 13 deletions

View File

@ -22,9 +22,10 @@ use RectorPrefix202410\Webmozart\Assert\Assert;
final class AddSensitiveParameterAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
*/
protected $phpAttributeAnalyzer;
private $phpAttributeAnalyzer;
public const SENSITIVE_PARAMETERS = 'sensitive_parameters';
/**
* @var string[]

View File

@ -58,7 +58,10 @@ final class ParentPropertyLookupGuard
$this->propertyManipulator = $propertyManipulator;
$this->classReflectionAnalyzer = $classReflectionAnalyzer;
}
public function isLegal(Property $property, ?ClassReflection $classReflection) : bool
/**
* @param \PhpParser\Node\Stmt\Property|string $property
*/
public function isLegal($property, ?ClassReflection $classReflection) : bool
{
if (!$classReflection instanceof ClassReflection) {
return \false;
@ -66,7 +69,7 @@ final class ParentPropertyLookupGuard
if ($classReflection->isAnonymous()) {
return \false;
}
$propertyName = $this->nodeNameResolver->getName($property);
$propertyName = $property instanceof Property ? $this->nodeNameResolver->getName($property) : $property;
if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) {
return \false;
}

View File

@ -24,7 +24,7 @@ final class VisibilityManipulator
}
/**
* @api
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Param $node
*/
public function makeStatic($node) : void
{
@ -95,7 +95,7 @@ final class VisibilityManipulator
$this->replaceVisibilityFlag($node, Visibility::PROTECTED);
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Param $node
*/
public function makePrivate($node) : void
{
@ -150,7 +150,7 @@ final class VisibilityManipulator
}
/**
* This way "abstract", "static", "final" are kept
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Param $node
*/
private function removeVisibility($node) : void
{
@ -158,6 +158,10 @@ final class VisibilityManipulator
if ($node->flags === 0) {
return;
}
if ($node instanceof Param) {
$node->flags = 0;
return;
}
if ($node->isPublic()) {
$node->flags |= Class_::MODIFIER_PUBLIC;
$node->flags -= Class_::MODIFIER_PUBLIC;
@ -185,7 +189,7 @@ final class VisibilityManipulator
$node->flags &= ~$visibility;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Param $node
*/
private function replaceVisibilityFlag($node, int $visibility) : void
{

View File

@ -5,12 +5,15 @@ namespace Rector\Privatization\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Privatization\Guard\ParentPropertyLookupGuard;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\Visibility;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -71,20 +74,36 @@ CODE_SAMPLE
return null;
}
$hasChanged = \false;
$classReflection = null;
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (!$classReflection instanceof ClassReflection) {
return null;
}
foreach ($node->getProperties() as $property) {
if ($this->shouldSkipProperty($property)) {
continue;
}
if (!$classReflection instanceof ClassReflection) {
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
}
if (!$this->parentPropertyLookupGuard->isLegal($property, $classReflection)) {
continue;
}
$this->visibilityManipulator->makePrivate($property);
$hasChanged = \true;
}
$construct = $node->getMethod(MethodName::CONSTRUCT);
if ($construct instanceof ClassMethod) {
foreach ($construct->params as $param) {
if ($param->flags === 0) {
continue;
}
if (!$this->visibilityManipulator->hasVisibility($param, Visibility::PROTECTED)) {
continue;
}
if (!$this->parentPropertyLookupGuard->isLegal((string) $this->getName($param), $classReflection)) {
continue;
}
$this->visibilityManipulator->makePrivate($param);
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '4c84d6c8fc7e8e2ceece04be14e196816b0ed99b';
public const PACKAGE_VERSION = 'a44e8351e20e9ad994cd0ff130575b9c3def45c5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-10-07 21:54:37';
public const RELEASE_DATE = '2024-10-08 15:55:55';
/**
* @var int
*/