From aaa752ab89aad06ec296fa756fe79bb23c7d5071 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 7 Nov 2022 13:53:39 +0100 Subject: [PATCH] drop uses of `instanceof NullType` --- src/Analyser/MutatingScope.php | 4 ++-- src/Analyser/NodeScopeResolver.php | 2 +- src/Reflection/Php/PhpParameterFromParserNodeReflection.php | 3 +-- src/Reflection/Php/PhpParameterReflection.php | 3 +-- src/Rules/Operators/InvalidComparisonOperationRule.php | 5 ++--- src/Rules/RuleLevelHelper.php | 2 +- .../TooWideArrowFunctionReturnTypehintRule.php | 3 +-- .../TooWideTypehints/TooWideClosureReturnTypehintRule.php | 3 +-- .../TooWideTypehints/TooWideFunctionReturnTypehintRule.php | 3 +-- .../TooWideTypehints/TooWideMethodReturnTypehintRule.php | 5 ++--- 10 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 77d7502bb1..46e7c6dc41 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -875,7 +875,7 @@ private function resolveType(Expr $node): Type $node->left instanceof Node\Expr\PropertyFetch || $node->left instanceof Node\Expr\StaticPropertyFetch ) - && $rightType instanceof NullType + && $rightType->isNull()->yes() && !$this->hasPropertyNativeType($node->left) ) { return new BooleanType(); @@ -886,7 +886,7 @@ private function resolveType(Expr $node): Type $node->right instanceof Node\Expr\PropertyFetch || $node->right instanceof Node\Expr\StaticPropertyFetch ) - && $leftType instanceof NullType + && $leftType->isNull()->yes() && !$this->hasPropertyNativeType($node->right) ) { return new BooleanType(); diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index dcf482b642..f6235d4bae 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -2167,7 +2167,7 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra $thisType = null; if (isset($expr->getArgs()[1])) { $argType = $scope->getType($expr->getArgs()[1]->value); - if ($argType instanceof NullType) { + if ($argType->isNull()->yes()) { $thisType = null; } else { $thisType = $argType; diff --git a/src/Reflection/Php/PhpParameterFromParserNodeReflection.php b/src/Reflection/Php/PhpParameterFromParserNodeReflection.php index 492b6eacb9..ad6410ccd1 100644 --- a/src/Reflection/Php/PhpParameterFromParserNodeReflection.php +++ b/src/Reflection/Php/PhpParameterFromParserNodeReflection.php @@ -5,7 +5,6 @@ use PHPStan\Reflection\ParameterReflectionWithPhpDocs; use PHPStan\Reflection\PassedByReference; use PHPStan\Type\MixedType; -use PHPStan\Type\NullType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypehintHelper; @@ -43,7 +42,7 @@ public function getType(): Type if ($this->type === null) { $phpDocType = $this->phpDocType; if ($phpDocType !== null && $this->defaultValue !== null) { - if ($this->defaultValue instanceof NullType) { + if ($this->defaultValue->isNull()->yes()) { $inferred = $phpDocType->inferTemplateTypes($this->defaultValue); if ($inferred->isEmpty()) { $phpDocType = TypeCombinator::addNull($phpDocType); diff --git a/src/Reflection/Php/PhpParameterReflection.php b/src/Reflection/Php/PhpParameterReflection.php index a1999f37dd..be16f41bcb 100644 --- a/src/Reflection/Php/PhpParameterReflection.php +++ b/src/Reflection/Php/PhpParameterReflection.php @@ -8,7 +8,6 @@ use PHPStan\Reflection\ParameterReflectionWithPhpDocs; use PHPStan\Reflection\PassedByReference; use PHPStan\Type\MixedType; -use PHPStan\Type\NullType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\TypehintHelper; @@ -52,7 +51,7 @@ public function getType(): Type $this->reflection->getDefaultValueExpression(), InitializerExprContext::fromReflectionParameter($this->reflection), ); - if ($defaultValueType instanceof NullType) { + if ($defaultValueType->isNull()->yes()) { $phpDocType = TypeCombinator::addNull($phpDocType); } } diff --git a/src/Rules/Operators/InvalidComparisonOperationRule.php b/src/Rules/Operators/InvalidComparisonOperationRule.php index c07e43fed9..59165f9b4b 100644 --- a/src/Rules/Operators/InvalidComparisonOperationRule.php +++ b/src/Rules/Operators/InvalidComparisonOperationRule.php @@ -11,7 +11,6 @@ use PHPStan\Type\ErrorType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; -use PHPStan\Type\NullType; use PHPStan\Type\ObjectWithoutClassType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; @@ -106,7 +105,7 @@ private function isPossiblyNullableObjectType(Scope $scope, Node\Expr $expr): bo return false; } - if (TypeCombinator::containsNull($type) && !$type instanceof NullType) { + if (TypeCombinator::containsNull($type) && !$type->isNull()->yes()) { $type = TypeCombinator::removeNull($type); } @@ -127,7 +126,7 @@ private function isPossiblyNullableArrayType(Scope $scope, Node\Expr $expr): boo static fn (Type $type): bool => $type->isArray()->yes(), )->getType(); - if (TypeCombinator::containsNull($type) && !$type instanceof NullType) { + if (TypeCombinator::containsNull($type) && !$type->isNull()->yes()) { $type = TypeCombinator::removeNull($type); } diff --git a/src/Rules/RuleLevelHelper.php b/src/Rules/RuleLevelHelper.php index 21d59e804a..900a4b6eab 100644 --- a/src/Rules/RuleLevelHelper.php +++ b/src/Rules/RuleLevelHelper.php @@ -169,7 +169,7 @@ public function findTypeToCheck( return new FoundTypeResult(new ErrorType(), [], [], null); } $type = $scope->getType($var); - if (!$this->checkNullables && !$type instanceof NullType) { + if (!$this->checkNullables && !$type->isNull()->yes()) { $type = TypeCombinator::removeNull($type); } diff --git a/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php index 01b4e33160..63eb0662e5 100644 --- a/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideArrowFunctionReturnTypehintRule.php @@ -7,7 +7,6 @@ use PHPStan\Node\InArrowFunctionNode; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; -use PHPStan\Type\NullType; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; use function sprintf; @@ -40,7 +39,7 @@ public function processNode(Node $node, Scope $scope): array } $returnType = $scope->getType($expr); - if ($returnType instanceof NullType) { + if ($returnType->isNull()->yes()) { return []; } $messages = []; diff --git a/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php index 60cd322ce6..d5cf38c3ea 100644 --- a/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideClosureReturnTypehintRule.php @@ -7,7 +7,6 @@ use PHPStan\Node\ClosureReturnStatementsNode; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; -use PHPStan\Type\NullType; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; @@ -62,7 +61,7 @@ public function processNode(Node $node, Scope $scope): array } $returnType = TypeCombinator::union(...$returnTypes); - if ($returnType instanceof NullType) { + if ($returnType->isNull()->yes()) { return []; } diff --git a/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php index 35f4080685..8a0be0f2b7 100644 --- a/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideFunctionReturnTypehintRule.php @@ -10,7 +10,6 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; -use PHPStan\Type\NullType; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; @@ -71,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array continue; } - if ($type instanceof NullType && !$node->hasNativeReturnTypehint()) { + if ($type->isNull()->yes() && !$node->hasNativeReturnTypehint()) { foreach ($node->getExecutionEnds() as $executionEnd) { if ($executionEnd->getStatementResult()->isAlwaysTerminating()) { continue; diff --git a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php index 259487e3bf..c851cbf2ae 100644 --- a/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php +++ b/src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php @@ -11,7 +11,6 @@ use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; use PHPStan\Type\Constant\ConstantBooleanType; -use PHPStan\Type\NullType; use PHPStan\Type\TypeCombinator; use PHPStan\Type\UnionType; use PHPStan\Type\VerbosityLevel; @@ -80,7 +79,7 @@ public function processNode(Node $node, Scope $scope): array $returnType = TypeCombinator::union(...$returnTypes); if ( !$method->isPrivate() - && ($returnType instanceof NullType || $returnType instanceof ConstantBooleanType) + && ($returnType->isNull()->yes() || $returnType instanceof ConstantBooleanType) && !$isFirstDeclaration ) { return []; @@ -92,7 +91,7 @@ public function processNode(Node $node, Scope $scope): array continue; } - if ($type instanceof NullType && !$node->hasNativeReturnTypehint()) { + if ($type->isNull()->yes() && !$node->hasNativeReturnTypehint()) { foreach ($node->getExecutionEnds() as $executionEnd) { if ($executionEnd->getStatementResult()->isAlwaysTerminating()) { continue;