Skip to content

Commit

Permalink
Implement isNull() on Type
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 7, 2022
1 parent 7eb0b92 commit d9d1263
Show file tree
Hide file tree
Showing 41 changed files with 212 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/MutatingScope.php
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Php/PhpParameterFromParserNodeReflection.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/Reflection/Php/PhpParameterReflection.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Rules/Operators/InvalidComparisonOperationRule.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/RuleLevelHelper.php
Expand Up @@ -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);
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [];
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [];
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Expand Up @@ -252,6 +252,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Expand Up @@ -166,6 +166,11 @@ public function toArrayKey(): Type
return $this;
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Expand Up @@ -166,6 +166,11 @@ public function toArrayKey(): Type
return $this;
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Expand Up @@ -166,6 +166,11 @@ public function toArrayKey(): Type
return $this;
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Expand Up @@ -169,6 +169,11 @@ public function toArrayKey(): Type
return new IntegerType();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Expand Up @@ -169,6 +169,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Expand Up @@ -220,6 +220,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Expand Up @@ -237,6 +237,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Expand Up @@ -236,6 +236,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Expand Up @@ -260,6 +260,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/BooleanType.php
Expand Up @@ -88,6 +88,11 @@ public function toArrayKey(): Type
return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Expand Up @@ -322,6 +322,11 @@ public function isOversizedArray(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Expand Up @@ -391,6 +391,11 @@ public function traverse(callable $cb): Type
);
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/FloatType.php
Expand Up @@ -121,6 +121,11 @@ public function toArrayKey(): Type
return new IntegerType();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntegerType.php
Expand Up @@ -88,6 +88,11 @@ public function toArrayKey(): Type
return $this;
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Expand Up @@ -613,6 +613,11 @@ public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType));
}

public function isNull(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isNull());
}

public function isTrue(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isTrue());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Expand Up @@ -260,6 +260,11 @@ public function getLastIterableValueType(): Type
return $this->getItemType();
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/JustNullableTypeTrait.php
Expand Up @@ -52,6 +52,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function isNull(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isTrue(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down

0 comments on commit d9d1263

Please sign in to comment.