Skip to content

Commit

Permalink
[Privatization][TypeDeclaration] Do not replace non-readonly property…
Browse files Browse the repository at this point in the history
… to constant on ChangeReadOnlyPropertyWithDefaultValueToConstantRector+TypedPropertyFromAssignsRector (#3891)

* [Privatization][TypeDeclaration] Do not replace non-readonly property to constant on ChangeReadOnlyPropertyWithDefaultValueToConstantRector+TypedPropertyFromAssignsRector

* eol

* add missing namespace

* Fixed 🎉

* clean up

* increase kernel cache key
  • Loading branch information
samsonasik committed May 19, 2023
1 parent af2a06e commit 9cfeac0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
5 changes: 0 additions & 5 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
Expand Down Expand Up @@ -88,10 +87,6 @@
\Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class => [
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
],

ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class => [
__DIR__ . '/src/Kernel/RectorKernel.php',
],
]);

$rectorConfig->phpstanConfig(__DIR__ . '/phpstan-for-rector.neon');
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v14';
private const CACHE_KEY = 'v15';

private ContainerInterface|null $container = null;

Expand Down
16 changes: 13 additions & 3 deletions src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\FunctionLike;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Util\MultiInstanceofChecker;
use Rector\NodeNameResolver\NodeNameResolver;
Expand All @@ -44,7 +45,8 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly MultiInstanceofChecker $multiInstanceofChecker
private readonly MultiInstanceofChecker $multiInstanceofChecker,
private readonly NodeComparator $nodeComparator
) {
}

Expand Down Expand Up @@ -72,8 +74,16 @@ public function isLeftPartOfAssign(Node $node): bool
$parentNode,
self::MODIFYING_NODE_TYPES
)) {
/** @var Assign|AssignOp|PreDec|PostDec|PreInc|PostInc $parentNode */
return $parentNode->var === $node;
/**
* @var Assign|AssignOp|PreDec|PostDec|PreInc|PostInc $parentNode
*
* Compare start token pos to ensure php_doc_info info not be checked
*/
if ($parentNode->var->getStartTokenPos() !== $node->getStartTokenPos()) {
return false;
}

return $this->nodeComparator->areNodesEqual($parentNode->var, $node);
}

if ($this->isOnArrayDestructuring($parentNode)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DoNotReplaceNonReadonlyPropertyToConstant;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DoNotReplaceNonReadonlyPropertyToConstantTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DoNotReplaceNonReadonlyPropertyToConstant\Fixture;

final class ChangeOnlyType
{
/** @var string|null */
private static $defaultFilesHash;

/**
* @param string[] $configFiles
*/
public function run(array $configFiles): string
{
if (self::$defaultFilesHash === null) {
self::$defaultFilesHash = 'test';
}

return self::$defaultFilesHash . 'test 2';
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\DoNotReplaceNonReadonlyPropertyToConstant\Fixture;

final class ChangeOnlyType
{
private static ?string $defaultFilesHash = null;

/**
* @param string[] $configFiles
*/
public function run(array $configFiles): string
{
if (self::$defaultFilesHash === null) {
self::$defaultFilesHash = 'test';
}

return self::$defaultFilesHash . 'test 2';
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class,
TypedPropertyFromAssignsRector::class,
]);
};

0 comments on commit 9cfeac0

Please sign in to comment.