Skip to content
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

Improve abs() return type #3263

Merged
merged 4 commits into from
Jul 27, 2024
Merged
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
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,11 @@ services:
-
class: PHPStan\Type\BitwiseFlagHelper

-
class: PHPStan\Type\Php\AbsFunctionDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ArgumentBasedFunctionReturnTypeExtension
tags:
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toInteger(): Type
{
return TypeCombinator::union(
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
julienfalque marked this conversation as resolved.
Show resolved Hide resolved
}

public function toInteger(): Type
{
return new IntegerType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
julienfalque marked this conversation as resolved.
Show resolved Hide resolved
}

public function toInteger(): Type
{
return new IntegerType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
julienfalque marked this conversation as resolved.
Show resolved Hide resolved
}

public function toInteger(): Type
{
return new IntegerType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public function toNumber(): Type
]);
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toInteger(): Type
{
return new IntegerType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toInteger(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toInteger(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toInteger(): Type
{
return new ConstantIntegerType(1);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toInteger(): Type
{
return new ConstantIntegerType(1);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toString(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function toNumber(): Type
return $this->toInteger();
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toString(): Type
{
return TypeCombinator::union(
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toString(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toInteger(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Constant/ConstantBooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function toNumber(): Type
return new ConstantIntegerType((int) $this->value);
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toString(): Type
{
return new ConstantStringType((string) $this->value);
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Constant/ConstantFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function abs;
use function ini_get;
use function ini_set;
use function is_finite;
Expand Down Expand Up @@ -76,6 +77,11 @@ public function toInteger(): Type
return new ConstantIntegerType((int) $this->value);
}

public function toAbsoluteNumber(): Type
{
return new self(abs($this->value));
}

public function toArrayKey(): Type
{
return new ConstantIntegerType((int) $this->value);
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Constant/ConstantIntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function abs;
use function sprintf;

/** @api */
Expand Down Expand Up @@ -76,6 +77,11 @@ public function toFloat(): Type
return new ConstantFloatType($this->value);
}

public function toAbsoluteNumber(): Type
{
return new self(abs($this->value));
}

public function toString(): Type
{
return new ConstantStringType((string) $this->value);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toInteger(): Type
{
return new ConstantIntegerType((int) $this->value);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public function toNumber(): Type
return $this;
}

public function toAbsoluteNumber(): Type
{
return $this;
}

public function toFloat(): Type
{
return $this;
Expand Down
15 changes: 15 additions & 0 deletions src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,21 @@ public function toBoolean(): BooleanType
return new ConstantBooleanType(false);
}

public function toAbsoluteNumber(): Type
{
if ($this->min !== null && $this->min >= 0) {
return $this;
}

if ($this->max === null || $this->max >= 0) {
$inversedMin = $this->min !== null ? $this->min * -1 : null;

return self::fromInterval(0, $inversedMin !== null && $this->max !== null ? max($inversedMin, $this->max) : null);
}

return self::fromInterval($this->max * -1, $this->min !== null ? $this->min * -1 : null);
}

public function toString(): Type
{
$isZero = (new ConstantIntegerType(0))->isSuperTypeOf($this);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function toNumber(): Type
return $this;
}

public function toAbsoluteNumber(): Type
{
return IntegerRangeType::createAllGreaterThanOrEqualTo(0);
}

public function toFloat(): Type
{
return new FloatType();
Expand Down
7 changes: 7 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,13 @@ public function toNumber(): Type
return $type;
}

public function toAbsoluteNumber(): Type
{
$type = $this->intersectTypes(static fn (Type $type): Type => $type->toAbsoluteNumber());

return $type;
}

public function toString(): Type
{
$type = $this->intersectTypes(static fn (Type $type): Type => $type->toString());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toString(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ public function toNumber(): Type
]);
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toInteger(): Type
{
return new IntegerType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ public function toNumber(): Type
return $this;
}

public function toAbsoluteNumber(): Type
{
return $this;
}

public function toString(): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NonexistentParentClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return new ErrorType();
}

public function toString(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function toNumber(): Type
return new ConstantIntegerType(0);
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toString(): Type
{
return new ConstantStringType('');
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,11 @@ public function toNumber(): Type
return new ErrorType();
}

public function toAbsoluteNumber(): Type
{
return $this->toNumber()->toAbsoluteNumber();
}

public function toInteger(): Type
{
if ($this->isInstanceOf('SimpleXMLElement')->yes()) {
Expand Down
Loading
Loading