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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector\Fixture;

final class AssignedInConstruct
{
public $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector\Fixture;

final class AssignedInConstruct
{
public string $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Rector\TypeDeclaration\Rector\Property;

use PhpParser\Node\Scalar\String_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
Expand All @@ -20,6 +20,7 @@
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Privatization\Guard\ParentPropertyLookupGuard;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\GetterTypeDeclarationPropertyTypeInferer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -34,7 +35,8 @@ public function __construct(
private readonly GetterTypeDeclarationPropertyTypeInferer $getterTypeDeclarationPropertyTypeInferer,
private readonly VarTagRemover $varTagRemover,
private readonly ParentPropertyLookupGuard $parentPropertyLookupGuard,
private readonly ReflectionResolver $reflectionResolver
private readonly ReflectionResolver $reflectionResolver,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}

Expand Down Expand Up @@ -100,8 +102,15 @@ public function refactor(Node $node): null|Class_
continue;
}

$isAssignedInConstructor = $this->constructorAssignDetector->isPropertyAssigned(
$node,
$this->getName($property)
);

// if property is public, it should be nullable
if ($property->isPublic() && ! TypeCombinator::containsNull($getterReturnType)) {
if ($property->isPublic() && ! TypeCombinator::containsNull(
$getterReturnType
) && ! $isAssignedInConstructor) {
$getterReturnType = TypeCombinator::addNull($getterReturnType);
}

Expand All @@ -119,7 +128,7 @@ public function refactor(Node $node): null|Class_
}

$property->type = $propertyTypeNode;
$this->decorateDefaultExpr($getterReturnType, $property);
$this->decorateDefaultExpr($getterReturnType, $property, $isAssignedInConstructor);

$this->refactorPhpDoc($property);

Expand All @@ -138,8 +147,12 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::TYPED_PROPERTIES;
}

private function decorateDefaultExpr(Type $propertyType, Property $property): void
private function decorateDefaultExpr(Type $propertyType, Property $property, bool $isAssignedInConstructor): void
{
if ($isAssignedInConstructor) {
return;
}

$propertyProperty = $property->props[0];

// already has a default value
Expand Down