Skip to content

Commit bef2801

Browse files
Nothing is lower than null
1 parent 5878035 commit bef2801

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

src/Type/NullType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryL
110110
return $otherType->isGreaterThan($this, $phpVersion);
111111
}
112112

113+
if ($otherType->isObject()->yes()) {
114+
return TrinaryLogic::createYes();
115+
}
116+
113117
return TrinaryLogic::createMaybe();
114118
}
115119

@@ -123,6 +127,10 @@ public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): T
123127
return $otherType->isGreaterThanOrEqual($this, $phpVersion);
124128
}
125129

130+
if ($otherType->isObject()->yes()) {
131+
return TrinaryLogic::createYes();
132+
}
133+
126134
return TrinaryLogic::createMaybe();
127135
}
128136

src/Type/Traits/UndecidedComparisonCompoundTypeTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ trait UndecidedComparisonCompoundTypeTrait
1313

1414
public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
1515
{
16+
if ($otherType->isNull()->yes() && $this->isObject()->yes()) {
17+
return TrinaryLogic::createYes();
18+
}
19+
1620
return TrinaryLogic::createMaybe();
1721
}
1822

1923
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
2024
{
25+
if ($otherType->isNull()->yes()) {
26+
return TrinaryLogic::createYes();
27+
}
28+
2129
return TrinaryLogic::createMaybe();
2230
}
2331

src/Type/Traits/UndecidedComparisonTypeTrait.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55
use PHPStan\Php\PhpVersion;
66
use PHPStan\TrinaryLogic;
77
use PHPStan\Type\MixedType;
8+
use PHPStan\Type\NullType;
89
use PHPStan\Type\Type;
910

1011
trait UndecidedComparisonTypeTrait
1112
{
1213

1314
public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
1415
{
16+
if ($otherType->isNull()->yes()) {
17+
return TrinaryLogic::createNo();
18+
}
19+
1520
return TrinaryLogic::createMaybe();
1621
}
1722

1823
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
1924
{
25+
if ($otherType->isNull()->yes() && $this->isObject()->yes()) {
26+
return TrinaryLogic::createNo();
27+
}
28+
2029
return TrinaryLogic::createMaybe();
2130
}
2231

@@ -32,11 +41,15 @@ public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
3241

3342
public function getGreaterType(PhpVersion $phpVersion): Type
3443
{
35-
return new MixedType();
44+
return new MixedType(subtractedType: new NullType());
3645
}
3746

3847
public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
3948
{
49+
if ($this->isObject()->yes()) {
50+
return new MixedType(subtractedType: new NullType());
51+
}
52+
4053
return new MixedType();
4154
}
4255

src/Type/TypeCombinator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,11 @@ private static function unionWithSubtractedType(
544544
$subtractedType = $type->getSubtractedType() === null
545545
? $subtractedType
546546
: self::union($type->getSubtractedType(), $subtractedType);
547+
548+
$subtractedType = self::intersect(
549+
$type->getTypeWithoutSubtractedType(),
550+
$subtractedType,
551+
);
547552
if ($subtractedType instanceof NeverType) {
548553
$subtractedType = null;
549554
}

tests/PHPStan/Analyser/nsrt/bcmath-number.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ public function bcVsNull(Number $a): void
190190
assertType('*ERROR*', $a ** $b);
191191
assertType('*ERROR*', $a << $b);
192192
assertType('*ERROR*', $a >> $b);
193-
assertType('bool', $a < $b);
194-
assertType('bool', $a <= $b);
195-
assertType('bool', $a > $b);
196-
assertType('bool', $a >= $b);
193+
assertType('false', $a < $b);
194+
assertType('false', $a <= $b);
195+
assertType('true', $a > $b);
196+
assertType('true', $a >= $b);
197197
assertType('int<-1, 1>', $a <=> $b);
198198
assertType('bool', $a == $b);
199199
assertType('bool', $a != $b);

0 commit comments

Comments
 (0)