Skip to content

Commit 3076c9e

Browse files
Simplify Null class
1 parent 5e3a364 commit 3076c9e

File tree

2 files changed

+17
-88
lines changed

2 files changed

+17
-88
lines changed

src/Type/NullType.php

+2-82
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\Constant\ConstantFloatType;
1212
use PHPStan\Type\Constant\ConstantIntegerType;
1313
use PHPStan\Type\Constant\ConstantStringType;
14+
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
1415
use PHPStan\Type\Traits\FalseyBooleanTypeTrait;
1516
use PHPStan\Type\Traits\NonArrayTypeTrait;
1617
use PHPStan\Type\Traits\NonCallableTypeTrait;
@@ -23,6 +24,7 @@
2324
class NullType implements ConstantScalarType
2425
{
2526

27+
use ConstantScalarTypeTrait;
2628
use NonArrayTypeTrait;
2729
use NonCallableTypeTrait;
2830
use NonIterableTypeTrait;
@@ -72,73 +74,11 @@ public function generalize(GeneralizePrecision $precision): Type
7274
return $this;
7375
}
7476

75-
public function accepts(Type $type, bool $strictTypes): TrinaryLogic
76-
{
77-
return $this->acceptsWithReason($type, $strictTypes)->result;
78-
}
79-
80-
public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
81-
{
82-
if ($type instanceof self) {
83-
return AcceptsResult::createYes();
84-
}
85-
86-
if ($type instanceof CompoundType) {
87-
return $type->isAcceptedWithReasonBy($this, $strictTypes);
88-
}
89-
90-
return AcceptsResult::createNo();
91-
}
92-
93-
public function isSuperTypeOf(Type $type): TrinaryLogic
94-
{
95-
return $this->isSuperTypeOfWithReason($type)->result;
96-
}
97-
98-
public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
99-
{
100-
if ($type instanceof self) {
101-
return IsSuperTypeOfResult::createYes();
102-
}
103-
104-
if ($type instanceof CompoundType) {
105-
return $type->isSubTypeOfWithReason($this);
106-
}
107-
108-
return IsSuperTypeOfResult::createNo();
109-
}
110-
11177
public function equals(Type $type): bool
11278
{
11379
return $type instanceof self;
11480
}
11581

116-
public function isSmallerThan(Type $otherType): TrinaryLogic
117-
{
118-
if ($otherType instanceof ConstantScalarType) {
119-
return TrinaryLogic::createFromBoolean(null < $otherType->getValue());
120-
}
121-
122-
if ($otherType instanceof CompoundType) {
123-
return $otherType->isGreaterThan($this);
124-
}
125-
126-
return TrinaryLogic::createMaybe();
127-
}
128-
129-
public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
130-
{
131-
if ($otherType instanceof ConstantScalarType) {
132-
return TrinaryLogic::createFromBoolean(null <= $otherType->getValue());
133-
}
134-
135-
if ($otherType instanceof CompoundType) {
136-
return $otherType->isGreaterThanOrEqual($this);
137-
}
138-
139-
return TrinaryLogic::createMaybe();
140-
}
141-
14282
public function describe(VerbosityLevel $level): string
14383
{
14484
return 'null';
@@ -230,26 +170,6 @@ public function isNull(): TrinaryLogic
230170
return TrinaryLogic::createYes();
231171
}
232172

233-
public function isConstantValue(): TrinaryLogic
234-
{
235-
return TrinaryLogic::createYes();
236-
}
237-
238-
public function isConstantScalarValue(): TrinaryLogic
239-
{
240-
return TrinaryLogic::createYes();
241-
}
242-
243-
public function getConstantScalarTypes(): array
244-
{
245-
return [$this];
246-
}
247-
248-
public function getConstantScalarValues(): array
249-
{
250-
return [$this->getValue()];
251-
}
252-
253173
public function isTrue(): TrinaryLogic
254174
{
255175
return TrinaryLogic::createNo();

src/Type/Traits/ConstantScalarTypeTrait.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Type\IsSuperTypeOfResult;
1414
use PHPStan\Type\LooseComparisonHelper;
1515
use PHPStan\Type\Type;
16+
use function get_parent_class;
1617

1718
trait ConstantScalarTypeTrait
1819
{
@@ -32,7 +33,11 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
3233
return $type->isAcceptedWithReasonBy($this, $strictTypes);
3334
}
3435

35-
return parent::acceptsWithReason($type, $strictTypes)->and(AcceptsResult::createMaybe());
36+
if (get_parent_class($this) !== false) {
37+
return parent::acceptsWithReason($type, $strictTypes)->and(AcceptsResult::createMaybe());
38+
}
39+
40+
return AcceptsResult::createNo();
3641
}
3742

3843
public function isSuperTypeOf(Type $type): TrinaryLogic
@@ -46,7 +51,7 @@ public function isSuperTypeOfWithReason(Type $type): IsSuperTypeOfResult
4651
return IsSuperTypeOfResult::createFromBoolean($this->equals($type));
4752
}
4853

49-
if ($type instanceof parent) {
54+
if (get_parent_class($this) !== false && $type instanceof parent) {
5055
return IsSuperTypeOfResult::createMaybe();
5156
}
5257

@@ -76,18 +81,22 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
7681
return $type->looseCompare($this, $phpVersion);
7782
}
7883

79-
return parent::looseCompare($type, $phpVersion);
84+
if (get_parent_class($this) !== false) {
85+
return parent::looseCompare($type, $phpVersion);
86+
}
87+
88+
return new BooleanType();
8089
}
8190

8291
public function equals(Type $type): bool
8392
{
84-
return $type instanceof self && $this->value === $type->value;
93+
return $type instanceof self && $this->getValue() === $type->getValue();
8594
}
8695

8796
public function isSmallerThan(Type $otherType): TrinaryLogic
8897
{
8998
if ($otherType instanceof ConstantScalarType) {
90-
return TrinaryLogic::createFromBoolean($this->value < $otherType->getValue());
99+
return TrinaryLogic::createFromBoolean($this->getValue() < $otherType->getValue());
91100
}
92101

93102
if ($otherType instanceof CompoundType) {
@@ -100,7 +109,7 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
100109
public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
101110
{
102111
if ($otherType instanceof ConstantScalarType) {
103-
return TrinaryLogic::createFromBoolean($this->value <= $otherType->getValue());
112+
return TrinaryLogic::createFromBoolean($this->getValue() <= $otherType->getValue());
104113
}
105114

106115
if ($otherType instanceof CompoundType) {

0 commit comments

Comments
 (0)