Skip to content

Commit

Permalink
UselessPrivatePropertyNullabilityRule: fix when mixed is assigned (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Apr 17, 2023
1 parent 2967a49 commit 54144bb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Rule/UselessPrivatePropertyNullabilityRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NullType;
use PHPStan\Type\TypeCombinator;
use ShipMonk\PHPStan\Visitor\ClassPropertyAssignmentVisitor;

Expand Down Expand Up @@ -62,9 +63,10 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$nullType = new NullType();
$assignedType = $propertyUsage->getScope()->getType($assignedExpr);

if (TypeCombinator::containsNull($assignedType)) {
if ($assignedType->accepts($nullType, $scope->isDeclareStrictTypes())->yes()) {
$nullabilityNeeded[$propertyName] = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ExampleClass

private ?int $isPrivate; // error: Property UselessPrivatePropertyNullabilityRule\ExampleClass::isPrivate is defined as nullable, but null is never assigned

private ?int $isPrivateMixedAssigned;

private ?int $isPrivateAssigned;

private ?int $isPrivateWithConditionalAssignment;
Expand All @@ -24,6 +26,7 @@ class ExampleClass
private $isUninitializedWithoutTypehint;

public function __construct(
mixed $mixed,
int $isPublic,
int $isProtected,
int $isPrivate,
Expand All @@ -35,6 +38,7 @@ public function __construct(
$this->isPublic = $isPublic;
$this->isProtected = $isProtected;
$this->isPrivate = $isPrivate;
$this->isPrivateMixedAssigned = $mixed;
$this->isPrivateWithConditionalAssignment = $isPrivateWithConditionalAssignment === 0 ? null : 1;
$this->isPrivateWithDefaultNull = $isPrivateWithDefaultNull;
$this->isPrivateWithDefaultNotNull = $isPrivateWithDefaultNotNull;
Expand Down

0 comments on commit 54144bb

Please sign in to comment.