|
7 | 7 | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
8 | 8 | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
9 | 9 | use PHPStan\ShouldNotHappenException; |
| 10 | +use PHPStan\TrinaryLogic; |
10 | 11 | use PHPStan\Turbo\ShadowedByTurboExtension; |
11 | 12 | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
12 | 13 | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
|
15 | 16 | use PHPStan\Type\Constant\ConstantBooleanType; |
16 | 17 | use PHPStan\Type\Constant\ConstantFloatType; |
17 | 18 | use PHPStan\Type\Constant\ConstantIntegerType; |
| 19 | +use function array_map; |
18 | 20 | use function get_class; |
19 | 21 | use function is_finite; |
20 | 22 | use function is_float; |
|
44 | 46 | * integer, and every interval operation the integer one. |
45 | 47 | */ |
46 | 48 | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/FloatRangeType.cpp')] |
47 | | -class FloatRangeType extends FloatType |
| 49 | +class FloatRangeType extends FloatType implements CompoundType |
48 | 50 | { |
49 | 51 |
|
50 | 52 | private function __construct(private float $min, private float $max) |
@@ -176,14 +178,34 @@ public function getMax(): float |
176 | 178 |
|
177 | 179 | public function describe(VerbosityLevel $level): string |
178 | 180 | { |
179 | | - return sprintf( |
180 | | - 'float<%s, %s>', |
181 | | - $this->min === -INF ? 'min' : (new ConstantFloatType($this->min))->describe(VerbosityLevel::precise()), |
182 | | - $this->max === INF ? 'max' : (new ConstantFloatType($this->max))->describe(VerbosityLevel::precise()), |
| 181 | + return $level->handle( |
| 182 | + static fn (): string => 'float', |
| 183 | + fn (): string => sprintf( |
| 184 | + 'float<%s, %s>', |
| 185 | + $this->min === -INF ? 'min' : (new ConstantFloatType($this->min))->describe(VerbosityLevel::precise()), |
| 186 | + $this->max === INF ? 'max' : (new ConstantFloatType($this->max))->describe(VerbosityLevel::precise()), |
| 187 | + ), |
183 | 188 | ); |
184 | 189 | } |
185 | 190 |
|
186 | 191 | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
| 192 | + { |
| 193 | + $result = $this->acceptsWithoutReasons($type, $strictTypes); |
| 194 | + if ($result->yes() || (new ConstantFloatType(NAN))->isSuperTypeOf($type)->no()) { |
| 195 | + return $result; |
| 196 | + } |
| 197 | + |
| 198 | + // tell why when NAN is all that is not accepted |
| 199 | + if (!$this->acceptsWithoutReasons(TypeCombinator::remove($type, new ConstantFloatType(NAN)), $strictTypes)->yes()) { |
| 200 | + return $result; |
| 201 | + } |
| 202 | + |
| 203 | + return new AcceptsResult($result->result, [ |
| 204 | + sprintf('NAN is in no float range. Use %s|NAN to accept it.', $this->describe(VerbosityLevel::precise())), |
| 205 | + ]); |
| 206 | + } |
| 207 | + |
| 208 | + private function acceptsWithoutReasons(Type $type, bool $strictTypes): AcceptsResult |
187 | 209 | { |
188 | 210 | // an integer is coerced to the float closest to it, which keeps the order of the integers |
189 | 211 | if ($type instanceof ConstantIntegerType) { |
@@ -241,6 +263,83 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult |
241 | 263 | return IsSuperTypeOfResult::createNo(); |
242 | 264 | } |
243 | 265 |
|
| 266 | + public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
| 267 | + { |
| 268 | + if ($otherType instanceof parent || $otherType instanceof IntersectionType) { |
| 269 | + return $otherType->isSuperTypeOf($this); |
| 270 | + } |
| 271 | + |
| 272 | + if ($otherType instanceof UnionType) { |
| 273 | + return IsSuperTypeOfResult::createNo()->or(...array_map(fn (Type $innerType): IsSuperTypeOfResult => $innerType->isSuperTypeOf($this), $otherType->getTypes())); |
| 274 | + } |
| 275 | + |
| 276 | + // the other type cannot tell floats apart |
| 277 | + return $otherType->isSuperTypeOf(new FloatType()); |
| 278 | + } |
| 279 | + |
| 280 | + public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
| 281 | + { |
| 282 | + if ($acceptingType instanceof parent || $acceptingType instanceof IntersectionType) { |
| 283 | + return $acceptingType->isSuperTypeOf($this)->toAcceptsResult(); |
| 284 | + } |
| 285 | + |
| 286 | + if ($acceptingType instanceof UnionType) { |
| 287 | + return AcceptsResult::createNo()->or(...array_map(fn (Type $innerType): AcceptsResult => $innerType->accepts($this, $strictTypes), $acceptingType->getTypes())); |
| 288 | + } |
| 289 | + |
| 290 | + // the other type cannot tell floats apart |
| 291 | + return $acceptingType->accepts(new FloatType(), $strictTypes); |
| 292 | + } |
| 293 | + |
| 294 | + public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 295 | + { |
| 296 | + return $this->compareEachPoint($otherType, false, false, $phpVersion) ?? parent::isSmallerThan($otherType, $phpVersion); |
| 297 | + } |
| 298 | + |
| 299 | + public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 300 | + { |
| 301 | + return $this->compareEachPoint($otherType, false, true, $phpVersion) ?? parent::isSmallerThanOrEqual($otherType, $phpVersion); |
| 302 | + } |
| 303 | + |
| 304 | + public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 305 | + { |
| 306 | + return $this->compareEachPoint($otherType, true, false, $phpVersion) ?? $otherType->isSmallerThan(new FloatType(), $phpVersion); |
| 307 | + } |
| 308 | + |
| 309 | + public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 310 | + { |
| 311 | + return $this->compareEachPoint($otherType, true, true, $phpVersion) ?? $otherType->isSmallerThanOrEqual(new FloatType(), $phpVersion); |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * Compares $otherType with each float of the range, the other type on the |
| 316 | + * left of `<` or `<=` when $otherOnLeft. Numbers, booleans and null compare |
| 317 | + * with the floats in their order, so the comparison holds for the whole range |
| 318 | + * when it holds for both bounds. A bool or null compares by truthiness, which |
| 319 | + * changes only at zero. Null for other types, which can compare with floats |
| 320 | + * in any order. |
| 321 | + */ |
| 322 | + private function compareEachPoint(Type $otherType, bool $otherOnLeft, bool $orEqual, PhpVersion $phpVersion): ?TrinaryLogic |
| 323 | + { |
| 324 | + $ordered = TypeCombinator::union(new IntegerType(), new FloatType(), new BooleanType(), new NullType()); |
| 325 | + if (!$ordered->isSuperTypeOf($otherType)->yes()) { |
| 326 | + return null; |
| 327 | + } |
| 328 | + |
| 329 | + $points = [new ConstantFloatType($this->min), new ConstantFloatType($this->max)]; |
| 330 | + if (!$this->excludesZero()) { |
| 331 | + $points[] = new ConstantFloatType(0.0); |
| 332 | + } |
| 333 | + |
| 334 | + $results = []; |
| 335 | + foreach ($points as $point) { |
| 336 | + [$left, $right] = $otherOnLeft ? [$otherType, $point] : [$point, $otherType]; |
| 337 | + $results[] = $orEqual ? $left->isSmallerThanOrEqual($right, $phpVersion) : $left->isSmallerThan($right, $phpVersion); |
| 338 | + } |
| 339 | + |
| 340 | + return TrinaryLogic::extremeIdentity(...$results); |
| 341 | + } |
| 342 | + |
244 | 343 | public function equals(Type $type): bool |
245 | 344 | { |
246 | 345 | return $type instanceof self && $this->min === $type->min && $this->max === $type->max; |
|
0 commit comments