Skip to content

Use ConstantScalarTypeTrait in NullType #3643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 2 additions & 82 deletions src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@
class NullType implements ConstantScalarType
{

use ConstantScalarTypeTrait;
use NonArrayTypeTrait;
use NonCallableTypeTrait;
use NonIterableTypeTrait;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 16 additions & 6 deletions src/Type/Traits/ConstantScalarTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\LooseComparisonHelper;
use PHPStan\Type\Type;
use function get_parent_class;

trait ConstantScalarTypeTrait
{
Expand All @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading