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

Skip MakeTypedPropertyNullableIfCheckedRector for constructor assigment. #3074

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,22 @@
<?php

namespace Rector\Tests\Restoration\Rector\Property\MakeTypedPropertyNullableIfCheckedRector\Fixture;

class SkipOnConstructorAssignment
{
private string $message;

public function __construct()
{
$this->message = 'foo';
}

private function getMessage(): ?string
{
if (!$this->message) {
return null;
}

return $this->message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpParser\Node\Stmt\PropertyProperty;
use Rector\Core\Rector\AbstractRector;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -26,7 +27,8 @@
final class MakeTypedPropertyNullableIfCheckedRector extends AbstractRector
{
public function __construct(
private readonly VisibilityManipulator $visibilityManipulator
private readonly VisibilityManipulator $visibilityManipulator,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}

Expand Down Expand Up @@ -90,7 +92,17 @@ public function refactor(Node $node): ?Node
return null;
}

$isPropertyNullChecked = $this->isPropertyNullChecked($onlyProperty);
$classLike = $this->betterNodeFinder->findParentType($onlyProperty, Class_::class);
if (! $classLike instanceof Class_) {
return null;
}

$isPropertyConstructorAssigned = $this->isPropertyConstructorAssigned($classLike, $onlyProperty);
if ($isPropertyConstructorAssigned) {
return null;
}

$isPropertyNullChecked = $this->isPropertyNullChecked($classLike, $onlyProperty);
if (! $isPropertyNullChecked) {
return null;
}
Expand Down Expand Up @@ -127,18 +139,19 @@ private function shouldSkipProperty(Property $property): bool
return $property->type instanceof NullableType;
}

private function isPropertyNullChecked(PropertyProperty $onlyPropertyProperty): bool
private function isPropertyConstructorAssigned(Class_ $class, PropertyProperty $onlyPropertyProperty): bool
{
$classLike = $this->betterNodeFinder->findParentType($onlyPropertyProperty, Class_::class);
if (! $classLike instanceof Class_) {
return false;
}
$propertyName = $this->nodeNameResolver->getName($onlyPropertyProperty);
return $this->constructorAssignDetector->isPropertyAssigned($class, $propertyName);
}

if ($this->isIdenticalOrNotIdenticalToNull($classLike, $onlyPropertyProperty)) {
private function isPropertyNullChecked(Class_ $class, PropertyProperty $onlyPropertyProperty): bool
{
if ($this->isIdenticalOrNotIdenticalToNull($class, $onlyPropertyProperty)) {
return true;
}

return $this->isBooleanNot($classLike, $onlyPropertyProperty);
return $this->isBooleanNot($class, $onlyPropertyProperty);
}

private function isIdenticalOrNotIdenticalToNull(Class_ $class, PropertyProperty $onlyPropertyProperty): bool
Expand Down