Skip to content

Commit 54144bb

Browse files
authored
UselessPrivatePropertyNullabilityRule: fix when mixed is assigned (#103)
1 parent 2967a49 commit 54144bb

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Rule/UselessPrivatePropertyNullabilityRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Rules\Rule;
1313
use PHPStan\Rules\RuleError;
1414
use PHPStan\Rules\RuleErrorBuilder;
15+
use PHPStan\Type\NullType;
1516
use PHPStan\Type\TypeCombinator;
1617
use ShipMonk\PHPStan\Visitor\ClassPropertyAssignmentVisitor;
1718

@@ -62,9 +63,10 @@ public function processNode(Node $node, Scope $scope): array
6263
continue;
6364
}
6465

66+
$nullType = new NullType();
6567
$assignedType = $propertyUsage->getScope()->getType($assignedExpr);
6668

67-
if (TypeCombinator::containsNull($assignedType)) {
69+
if ($assignedType->accepts($nullType, $scope->isDeclareStrictTypes())->yes()) {
6870
$nullabilityNeeded[$propertyName] = true;
6971
}
7072
}

tests/Rule/data/UselessPrivatePropertyNullabilityRule/code.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class ExampleClass
1010

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

13+
private ?int $isPrivateMixedAssigned;
14+
1315
private ?int $isPrivateAssigned;
1416

1517
private ?int $isPrivateWithConditionalAssignment;
@@ -24,6 +26,7 @@ class ExampleClass
2426
private $isUninitializedWithoutTypehint;
2527

2628
public function __construct(
29+
mixed $mixed,
2730
int $isPublic,
2831
int $isProtected,
2932
int $isPrivate,
@@ -35,6 +38,7 @@ public function __construct(
3538
$this->isPublic = $isPublic;
3639
$this->isProtected = $isProtected;
3740
$this->isPrivate = $isPrivate;
41+
$this->isPrivateMixedAssigned = $mixed;
3842
$this->isPrivateWithConditionalAssignment = $isPrivateWithConditionalAssignment === 0 ? null : 1;
3943
$this->isPrivateWithDefaultNull = $isPrivateWithDefaultNull;
4044
$this->isPrivateWithDefaultNotNull = $isPrivateWithDefaultNotNull;

0 commit comments

Comments
 (0)