Skip to content

Commit d9d1263

Browse files
authored
Implement isNull() on Type
1 parent 7eb0b92 commit d9d1263

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+212
-20
lines changed

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ private function resolveType(Expr $node): Type
875875
$node->left instanceof Node\Expr\PropertyFetch
876876
|| $node->left instanceof Node\Expr\StaticPropertyFetch
877877
)
878-
&& $rightType instanceof NullType
878+
&& $rightType->isNull()->yes()
879879
&& !$this->hasPropertyNativeType($node->left)
880880
) {
881881
return new BooleanType();
@@ -886,7 +886,7 @@ private function resolveType(Expr $node): Type
886886
$node->right instanceof Node\Expr\PropertyFetch
887887
|| $node->right instanceof Node\Expr\StaticPropertyFetch
888888
)
889-
&& $leftType instanceof NullType
889+
&& $leftType->isNull()->yes()
890890
&& !$this->hasPropertyNativeType($node->right)
891891
) {
892892
return new BooleanType();

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2167,7 +2167,7 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra
21672167
$thisType = null;
21682168
if (isset($expr->getArgs()[1])) {
21692169
$argType = $scope->getType($expr->getArgs()[1]->value);
2170-
if ($argType instanceof NullType) {
2170+
if ($argType->isNull()->yes()) {
21712171
$thisType = null;
21722172
} else {
21732173
$thisType = $argType;

src/Reflection/Php/PhpParameterFromParserNodeReflection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
66
use PHPStan\Reflection\PassedByReference;
77
use PHPStan\Type\MixedType;
8-
use PHPStan\Type\NullType;
98
use PHPStan\Type\Type;
109
use PHPStan\Type\TypeCombinator;
1110
use PHPStan\Type\TypehintHelper;
@@ -43,7 +42,7 @@ public function getType(): Type
4342
if ($this->type === null) {
4443
$phpDocType = $this->phpDocType;
4544
if ($phpDocType !== null && $this->defaultValue !== null) {
46-
if ($this->defaultValue instanceof NullType) {
45+
if ($this->defaultValue->isNull()->yes()) {
4746
$inferred = $phpDocType->inferTemplateTypes($this->defaultValue);
4847
if ($inferred->isEmpty()) {
4948
$phpDocType = TypeCombinator::addNull($phpDocType);

src/Reflection/Php/PhpParameterReflection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
99
use PHPStan\Reflection\PassedByReference;
1010
use PHPStan\Type\MixedType;
11-
use PHPStan\Type\NullType;
1211
use PHPStan\Type\Type;
1312
use PHPStan\Type\TypeCombinator;
1413
use PHPStan\Type\TypehintHelper;
@@ -52,7 +51,7 @@ public function getType(): Type
5251
$this->reflection->getDefaultValueExpression(),
5352
InitializerExprContext::fromReflectionParameter($this->reflection),
5453
);
55-
if ($defaultValueType instanceof NullType) {
54+
if ($defaultValueType->isNull()->yes()) {
5655
$phpDocType = TypeCombinator::addNull($phpDocType);
5756
}
5857
}

src/Rules/Operators/InvalidComparisonOperationRule.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Type\ErrorType;
1212
use PHPStan\Type\FloatType;
1313
use PHPStan\Type\IntegerType;
14-
use PHPStan\Type\NullType;
1514
use PHPStan\Type\ObjectWithoutClassType;
1615
use PHPStan\Type\Type;
1716
use PHPStan\Type\TypeCombinator;
@@ -106,7 +105,7 @@ private function isPossiblyNullableObjectType(Scope $scope, Node\Expr $expr): bo
106105
return false;
107106
}
108107

109-
if (TypeCombinator::containsNull($type) && !$type instanceof NullType) {
108+
if (TypeCombinator::containsNull($type) && !$type->isNull()->yes()) {
110109
$type = TypeCombinator::removeNull($type);
111110
}
112111

@@ -127,7 +126,7 @@ private function isPossiblyNullableArrayType(Scope $scope, Node\Expr $expr): boo
127126
static fn (Type $type): bool => $type->isArray()->yes(),
128127
)->getType();
129128

130-
if (TypeCombinator::containsNull($type) && !$type instanceof NullType) {
129+
if (TypeCombinator::containsNull($type) && !$type->isNull()->yes()) {
131130
$type = TypeCombinator::removeNull($type);
132131
}
133132

src/Rules/RuleLevelHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function findTypeToCheck(
169169
return new FoundTypeResult(new ErrorType(), [], [], null);
170170
}
171171
$type = $scope->getType($var);
172-
if (!$this->checkNullables && !$type instanceof NullType) {
172+
if (!$this->checkNullables && !$type->isNull()->yes()) {
173173
$type = TypeCombinator::removeNull($type);
174174
}
175175

src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Node\InArrowFunctionNode;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
10-
use PHPStan\Type\NullType;
1110
use PHPStan\Type\UnionType;
1211
use PHPStan\Type\VerbosityLevel;
1312
use function sprintf;
@@ -40,7 +39,7 @@ public function processNode(Node $node, Scope $scope): array
4039
}
4140

4241
$returnType = $scope->getType($expr);
43-
if ($returnType instanceof NullType) {
42+
if ($returnType->isNull()->yes()) {
4443
return [];
4544
}
4645
$messages = [];

src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Node\ClosureReturnStatementsNode;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
10-
use PHPStan\Type\NullType;
1110
use PHPStan\Type\TypeCombinator;
1211
use PHPStan\Type\UnionType;
1312
use PHPStan\Type\VerbosityLevel;
@@ -62,7 +61,7 @@ public function processNode(Node $node, Scope $scope): array
6261
}
6362

6463
$returnType = TypeCombinator::union(...$returnTypes);
65-
if ($returnType instanceof NullType) {
64+
if ($returnType->isNull()->yes()) {
6665
return [];
6766
}
6867

src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\ShouldNotHappenException;
13-
use PHPStan\Type\NullType;
1413
use PHPStan\Type\TypeCombinator;
1514
use PHPStan\Type\UnionType;
1615
use PHPStan\Type\VerbosityLevel;
@@ -71,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array
7170
continue;
7271
}
7372

74-
if ($type instanceof NullType && !$node->hasNativeReturnTypehint()) {
73+
if ($type->isNull()->yes() && !$node->hasNativeReturnTypehint()) {
7574
foreach ($node->getExecutionEnds() as $executionEnd) {
7675
if ($executionEnd->getStatementResult()->isAlwaysTerminating()) {
7776
continue;

src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PHPStan\Rules\RuleErrorBuilder;
1212
use PHPStan\ShouldNotHappenException;
1313
use PHPStan\Type\Constant\ConstantBooleanType;
14-
use PHPStan\Type\NullType;
1514
use PHPStan\Type\TypeCombinator;
1615
use PHPStan\Type\UnionType;
1716
use PHPStan\Type\VerbosityLevel;
@@ -80,7 +79,7 @@ public function processNode(Node $node, Scope $scope): array
8079
$returnType = TypeCombinator::union(...$returnTypes);
8180
if (
8281
!$method->isPrivate()
83-
&& ($returnType instanceof NullType || $returnType instanceof ConstantBooleanType)
82+
&& ($returnType->isNull()->yes() || $returnType instanceof ConstantBooleanType)
8483
&& !$isFirstDeclaration
8584
) {
8685
return [];
@@ -92,7 +91,7 @@ public function processNode(Node $node, Scope $scope): array
9291
continue;
9392
}
9493

95-
if ($type instanceof NullType && !$node->hasNativeReturnTypehint()) {
94+
if ($type->isNull()->yes() && !$node->hasNativeReturnTypehint()) {
9695
foreach ($node->getExecutionEnds() as $executionEnd) {
9796
if ($executionEnd->getStatementResult()->isAlwaysTerminating()) {
9897
continue;

0 commit comments

Comments
 (0)