Skip to content

Commit

Permalink
Fix make ReadOnlyPropertyRector skip if traits are used, as not relia…
Browse files Browse the repository at this point in the history
…ble (#5548)

* Added test case property declared readonly when only set in trait

* make ReadOnlyPropertyRector skip if traits are used, as not reliable

---------

Co-authored-by: David Grüner <david@vworld.at>
  • Loading branch information
TomasVotruba and vworldat committed Feb 3, 2024
1 parent b0ad364 commit 06fa177
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

use stdClass;

final class SkipWriteInTraitProperty
{
use WritableTrait;

public function __construct(private stdClass $myProperty)
{
}
}

trait WritableTrait
{
public function setProperty(stdClass $myProperty): void
{
$this->myProperty = $myProperty;
}
}
5 changes: 5 additions & 0 deletions rules/Php81/Rector/Property/ReadOnlyPropertyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ private function shouldSkip(Class_ $class): bool
return true;
}

// not safe
if ($class->getTraitUses() !== []) {
return true;
}

// skip "clone $this" cases, as can create unexpected write to local constructor property
return $this->hasCloneThis($class);
}
Expand Down

0 comments on commit 06fa177

Please sign in to comment.