Skip to content
Open
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,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Fixture;

final class SkipUsedByPropertyHook
{
private bool $isConstructed = false;

public mixed $value = null {
get {
return $this->value;
}
set {
if (!$this->isConstructed) {
$this->value = $value;
return;
}

if (!is_string($value)) {
throw new \TypeError('$value must be a string when it is already constructed!');
}

$this->value = $value;
}
}

public function __construct(
mixed $value = null,
) {
$this->value = $value;
$this->isConstructed = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use PhpParser\Node\Stmt\Property;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -27,6 +29,8 @@ final class InlineConstructorDefaultToPropertyRector extends AbstractRector
{
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PropertyFetchFinder $propertyFetchFinder
) {
}

Expand Down Expand Up @@ -150,6 +154,32 @@ private function matchAssignedLocalPropertyName(Assign $assign): ?string
return $propertyName;
}

private function isFoundInAnyPropertyHooks(Class_ $class, string $propertyName): bool
{
foreach ($class->getProperties() as $property) {
if ($property->hooks === []) {
continue;
}

$isFoundInPropertyAnyHooks = (bool) $this->betterNodeFinder->findFirst($property->hooks, function (Node $subNode) use (
$class,
$propertyName
): bool {
if (! $subNode instanceof PropertyFetch) {
return false;
}

return $this->propertyFetchFinder->isLocalPropertyFetchByName($subNode, $class, $propertyName);
});

if ($isFoundInPropertyAnyHooks) {
return true;
}
}

return false;
}

private function refactorProperty(
Class_ $class,
string $propertyName,
Expand All @@ -161,6 +191,10 @@ private function refactorProperty(
return false;
}

if ($this->isFoundInAnyPropertyHooks($class, $propertyName)) {
return false;
}

foreach ($class->stmts as $classStmt) {
if (! $classStmt instanceof Property) {
continue;
Expand Down