Skip to content
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
17 changes: 16 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
includes:
- vendor/symplify/phpstan-rules/config/symplify-rules.neon
- vendor/symplify/phpstan-rules/config/rector-rules.neon

rules:
Expand Down Expand Up @@ -422,3 +421,19 @@ parameters:
-
identifier: varTag.nativeType
path: src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php


-
identifier: symplify.noNullableServiceInConstructor
paths:
- src/NodeTypeResolver/PHPStan/ObjectWithoutClassTypeWithParentTypes.php

- identifier: symplify.commandHasAsCommandAttribute
paths:
- src/Console/Command/ValidateConfigCommand.php
- src/Console/Command/WorkerCommand.php
- src/Console/Command/ProcessCommand.php
- src/Console/Command/ListRulesCommand.php
- src/Console/Command/CustomRuleCommand.php
- src/Console/Command/ComposerBasedCommand.php
- src/Console/Command/SetupCICommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture;

/**
* Both trait and class here on purpose to show that the property being renamed in both trait and class
*/
trait InstanceConfigTrait
{
protected array $config = [];

public function initCfg(): void
{
$this->config = $this->defaultConfig;
}
}

final class UseTraitInClass
{
use InstanceConfigTrait;

protected array $defaultConfig = [
'fields' => [],
];
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture;

/**
* Both trait and class here on purpose to show that the property being renamed in both trait and class
*/
trait InstanceConfigTrait
{
protected array $config = [];

public function initCfg(): void
{
$this->config = $this->defaultConfigNew;
}
}

final class UseTraitInClass
{
use InstanceConfigTrait;

protected array $defaultConfigNew = [
'fields' => [],
];
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
'_config',
'config'
),
new RenameProperty(
'Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\Fixture\InstanceConfigTrait',
'defaultConfig',
'defaultConfigNew'
),
]);
};
29 changes: 23 additions & 6 deletions rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\VarLikeIdentifier;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Renaming\ValueObject\RenameProperty;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -31,6 +33,11 @@ final class RenamePropertyRector extends AbstractRector implements ConfigurableR

private bool $hasChanged = false;

public function __construct(
private readonly ReflectionResolver $reflectionResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace defined old properties by new ones', [
Expand Down Expand Up @@ -83,6 +90,11 @@ public function configure(array $configuration): void

private function renameProperty(ClassLike $classLike, RenameProperty $renameProperty): void
{
$property = $classLike->getProperty($renameProperty->getOldProperty());
if (! $property instanceof Property) {
return;
}

$classLikeName = (string) $this->getName($classLike);
$renamePropertyObjectType = $renameProperty->getObjectType();
$className = $renamePropertyObjectType->getClassName();
Expand All @@ -92,12 +104,7 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp

$isSuperType = $classNameObjectType->isSuperTypeOf($classLikeNameObjectType)
->yes();
if ($classLikeName !== $className && ! $isSuperType) {
return;
}

$property = $classLike->getProperty($renameProperty->getOldProperty());
if (! $property instanceof Property) {
if ($classLikeName !== $className && ! $isSuperType && ! $this->isUsingTrait($classLike, $className)) {
return;
}

Expand All @@ -111,6 +118,16 @@ private function renameProperty(ClassLike $classLike, RenameProperty $renameProp
$property->props[0]->name = new VarLikeIdentifier($newProperty);
}

private function isUsingTrait(ClassLike $classLike, string $traitName): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classLike);
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->hasTraitUse($traitName);
}

private function refactorPropertyFetch(
PropertyFetch|StaticPropertyFetch $propertyFetch
): null|PropertyFetch|StaticPropertyFetch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testSeparateFilesAndDirectories(): void
$this->assertCount(1, $files);
$this->assertCount(1, $directories);

$this->assertSame($files, [__DIR__ . '/FileAndDirectoryFilterTest.php']);
$this->assertSame($directories, [__DIR__]);
$this->assertSame([__DIR__ . '/FileAndDirectoryFilterTest.php'], $files);
$this->assertSame([__DIR__], $directories);
}
}
Loading