diff --git a/src/Type/NullType.php b/src/Type/NullType.php index 7b60d5d3cb..6d2c87f074 100644 --- a/src/Type/NullType.php +++ b/src/Type/NullType.php @@ -11,6 +11,7 @@ use PHPStan\Type\Constant\ConstantFloatType; use PHPStan\Type\Constant\ConstantIntegerType; use PHPStan\Type\Constant\ConstantStringType; +use PHPStan\Type\Traits\ConstantScalarTypeTrait; use PHPStan\Type\Traits\FalseyBooleanTypeTrait; use PHPStan\Type\Traits\NonArrayTypeTrait; use PHPStan\Type\Traits\NonCallableTypeTrait; @@ -23,6 +24,7 @@ class NullType implements ConstantScalarType { + use ConstantScalarTypeTrait; use NonArrayTypeTrait; use NonCallableTypeTrait; use NonIterableTypeTrait; @@ -72,73 +74,11 @@ public function generalize(GeneralizePrecision $precision): Type return $this; } - public function accepts(Type $type, bool $strictTypes): TrinaryLogic - { - return $this->acceptsWithReason($type, $strictTypes)->result; - } - - public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult - { - if ($type instanceof self) { - return AcceptsResult::createYes(); - } - - if ($type instanceof CompoundType) { - return $type->isAcceptedWithReasonBy($this, $strictTypes); - } - - return AcceptsResult::createNo(); - } - - public function isSuperTypeOf(Type $type): TrinaryLogic - { - return $this->isSuperTypeOfWithReason($type)->result; - } - - public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult - { - if ($type instanceof self) { - return IsSuperTypeOfResult::createYes(); - } - - if ($type instanceof CompoundType) { - return $type->isSubTypeOfWithReason($this); - } - - return IsSuperTypeOfResult::createNo(); - } - public function equals(Type $type): bool { return $type instanceof self; } - public function isSmallerThan(Type $otherType): TrinaryLogic - { - if ($otherType instanceof ConstantScalarType) { - return TrinaryLogic::createFromBoolean(null < $otherType->getValue()); - } - - if ($otherType instanceof CompoundType) { - return $otherType->isGreaterThan($this); - } - - return TrinaryLogic::createMaybe(); - } - - public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic - { - if ($otherType instanceof ConstantScalarType) { - return TrinaryLogic::createFromBoolean(null <= $otherType->getValue()); - } - - if ($otherType instanceof CompoundType) { - return $otherType->isGreaterThanOrEqual($this); - } - - return TrinaryLogic::createMaybe(); - } - public function describe(VerbosityLevel $level): string { return 'null'; @@ -230,26 +170,6 @@ public function isNull(): TrinaryLogic return TrinaryLogic::createYes(); } - public function isConstantValue(): TrinaryLogic - { - return TrinaryLogic::createYes(); - } - - public function isConstantScalarValue(): TrinaryLogic - { - return TrinaryLogic::createYes(); - } - - public function getConstantScalarTypes(): array - { - return [$this]; - } - - public function getConstantScalarValues(): array - { - return [$this->getValue()]; - } - public function isTrue(): TrinaryLogic { return TrinaryLogic::createNo(); diff --git a/src/Type/Traits/ConstantScalarTypeTrait.php b/src/Type/Traits/ConstantScalarTypeTrait.php index 4c8c6a7294..e31589026c 100644 --- a/src/Type/Traits/ConstantScalarTypeTrait.php +++ b/src/Type/Traits/ConstantScalarTypeTrait.php @@ -13,6 +13,7 @@ use PHPStan\Type\IsSuperTypeOfResult; use PHPStan\Type\LooseComparisonHelper; use PHPStan\Type\Type; +use function get_parent_class; trait ConstantScalarTypeTrait { @@ -32,7 +33,12 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult return $type->isAcceptedWithReasonBy($this, $strictTypes); } - return parent::acceptsWithReason($type, $strictTypes)->and(AcceptsResult::createMaybe()); + if (get_parent_class($this) !== false) { + // @phpstan-ignore class.noParent + return parent::acceptsWithReason($type, $strictTypes)->and(AcceptsResult::createMaybe()); + } + + return AcceptsResult::createNo(); } public function isSuperTypeOf(Type $type): TrinaryLogic @@ -46,7 +52,7 @@ public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult return IsSuperTypeOfResult::createFromBoolean($this->equals($type)); } - if ($type instanceof parent) { + if (get_parent_class($this) !== false && $type instanceof parent) { return IsSuperTypeOfResult::createMaybe(); } @@ -76,18 +82,22 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType return $type->looseCompare($this, $phpVersion); } - return parent::looseCompare($type, $phpVersion); + if (get_parent_class($this) !== false) { + return parent::looseCompare($type, $phpVersion); + } + + return new BooleanType(); } public function equals(Type $type): bool { - return $type instanceof self && $this->value === $type->value; + return $type instanceof self && $this->getValue() === $type->getValue(); } public function isSmallerThan(Type $otherType): TrinaryLogic { if ($otherType instanceof ConstantScalarType) { - return TrinaryLogic::createFromBoolean($this->value < $otherType->getValue()); + return TrinaryLogic::createFromBoolean($this->getValue() < $otherType->getValue()); } if ($otherType instanceof CompoundType) { @@ -100,7 +110,7 @@ public function isSmallerThan(Type $otherType): TrinaryLogic public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic { if ($otherType instanceof ConstantScalarType) { - return TrinaryLogic::createFromBoolean($this->value <= $otherType->getValue()); + return TrinaryLogic::createFromBoolean($this->getValue() <= $otherType->getValue()); } if ($otherType instanceof CompoundType) {