Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Renaming] Skip used by trait as property promotion on RenamePropertyToMatchTypeRector #6665

Merged
merged 4 commits into from
Jan 11, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Fixture;

use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\EliteManager;
use Rector\Tests\Naming\Rector\Class_\RenamePropertyToMatchTypeRector\Source\UseEliteManagerTrait;

class SkipUsedByTraitAsPropertyPromotion
{
use UseEliteManagerTrait;

public function __construct(private EliteManager $eventManager)
{
}
}
14 changes: 14 additions & 0 deletions rules/Naming/PropertyRenamer/PropertyPromotionRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Reflection\ClassReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
Expand All @@ -20,8 +21,10 @@
use Rector\Naming\ValueObject\ParamRename;
use Rector\Naming\ValueObjectFactory\ParamRenameFactory;
use Rector\Naming\VariableRenamer;
use Rector\NodeManipulator\PropertyManipulator;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php\PhpVersionProvider;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;

Expand All @@ -37,6 +40,8 @@ public function __construct(
private NodeNameResolver $nodeNameResolver,
private VariableRenamer $variableRenamer,
private DocBlockUpdater $docBlockUpdater,
private ReflectionResolver $reflectionResolver,
private PropertyManipulator $propertyManipulator
) {
}

Expand All @@ -53,6 +58,11 @@ public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
return false;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($classLike);
if (! $classReflection instanceof ClassReflection) {
return false;
}

// resolve possible and existing param names
$blockingParamNames = $this->resolveBlockingParamNames($constructClassMethod);

Expand All @@ -76,6 +86,10 @@ public function renamePropertyPromotion(Class_|Interface_ $classLike): bool
continue;
}

if ($this->propertyManipulator->isUsedByTrait($classReflection, $currentParamName)) {
continue;
}

$this->renameParamVarNameAndVariableUsage($classLike, $constructClassMethod, $desiredPropertyName, $param);
$hasChanged = true;
}
Expand Down
20 changes: 11 additions & 9 deletions rules/Php81/Rector/New_/MyCLabsConstructorCallToEnumFromRector.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Php81\Rector\New_;

use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\Enum\ObjectReference;
Expand Down Expand Up @@ -69,37 +71,37 @@ public function getRuleDefinition(): RuleDefinition
);
}

private function refactorConstructorCallToStaticFromCall(New_ $node): ?StaticCall
private function refactorConstructorCallToStaticFromCall(New_ $new): ?StaticCall
{
if (! $this->isObjectType($node->class, new ObjectType(self::MY_C_LABS_CLASS))) {
if (! $this->isObjectType($new->class, new ObjectType(self::MY_C_LABS_CLASS))) {
return null;
}

$classname = $this->getName($node->class);
$classname = $this->getName($new->class);
if (in_array($classname, [ObjectReference::SELF, ObjectReference::STATIC], true)) {
$classname = ScopeFetcher::fetch($node)->getClassReflection()?->getName();
$classname = ScopeFetcher::fetch($new)->getClassReflection()?->getName();
}

if ($classname === null) {
return null;
}

if (! $this->isMyCLabsConstructor($node, $classname)) {
if (! $this->isMyCLabsConstructor($new, $classname)) {
return null;
}

return new StaticCall(new Name\FullyQualified($classname), self::DEFAULT_ENUM_CONSTRUCTOR, $node->args);
return new StaticCall(new FullyQualified($classname), self::DEFAULT_ENUM_CONSTRUCTOR, $new->args);
}

private function isMyCLabsConstructor(New_ $node, string $classname): bool
private function isMyCLabsConstructor(New_ $new, string $classname): bool
{
$classReflection = $this->reflectionProvider->getClass($classname);
if (! $classReflection->hasMethod(MethodName::CONSTRUCT)) {
return true;
}

return $classReflection
->getMethod(MethodName::CONSTRUCT, ScopeFetcher::fetch($node))
->getMethod(MethodName::CONSTRUCT, ScopeFetcher::fetch($new))
->getDeclaringClass()
->getName() === self::MY_C_LABS_CLASS;
}
Expand Down
5 changes: 3 additions & 2 deletions src/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public function isInIf(Node $node): bool
return $node->getAttribute(AttributeKey::IS_IN_IF) === true;
}

public function isChangeableContext(PropertyFetch | StaticPropertyFetch | NullsafePropertyFetch $propertyFetch): bool
{
public function isChangeableContext(
PropertyFetch | StaticPropertyFetch | NullsafePropertyFetch $propertyFetch
): bool {
if ($propertyFetch->getAttribute(AttributeKey::IS_UNSET_VAR, false)) {
return true;
}
Expand Down
Loading