Skip to content

Commit

Permalink
Skip MakeTypedPropertyNullableIfCheckedRector for constructor assigme…
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohlie committed Nov 18, 2022
1 parent a1df08d commit b5ddb02
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
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

0 comments on commit b5ddb02

Please sign in to comment.