From 3076c9e6c0cee415a34d35f37c745bc5be27aee0 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 17 Nov 2024 20:39:49 +0100 Subject: [PATCH 1/2] Simplify Null class --- src/Type/NullType.php | 84 +-------------------- src/Type/Traits/ConstantScalarTypeTrait.php | 21 ++++-- 2 files changed, 17 insertions(+), 88 deletions(-) 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..2be0def213 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,11 @@ 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) { + return parent::acceptsWithReason($type, $strictTypes)->and(AcceptsResult::createMaybe()); + } + + return AcceptsResult::createNo(); } public function isSuperTypeOf(Type $type): TrinaryLogic @@ -46,7 +51,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 +81,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 +109,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) { From d4ff508160f85d46ac638c7325217b62c933b6da Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 17 Nov 2024 21:04:49 +0100 Subject: [PATCH 2/2] Ignore error --- src/Type/Traits/ConstantScalarTypeTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Type/Traits/ConstantScalarTypeTrait.php b/src/Type/Traits/ConstantScalarTypeTrait.php index 2be0def213..e31589026c 100644 --- a/src/Type/Traits/ConstantScalarTypeTrait.php +++ b/src/Type/Traits/ConstantScalarTypeTrait.php @@ -34,6 +34,7 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult } if (get_parent_class($this) !== false) { + // @phpstan-ignore class.noParent return parent::acceptsWithReason($type, $strictTypes)->and(AcceptsResult::createMaybe()); }