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 2 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
119 changes: 119 additions & 0 deletions src/Type/Php/AbsFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function abs;
use function count;
use function max;

class AbsFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'abs';
}

public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): ?Type
{
$args = $functionCall->getArgs();

if (!isset($args[0])) {
return null;
}

$type = $scope->getType($args[0]->value);

if ($type instanceof UnionType) {
julienfalque marked this conversation as resolved.
Show resolved Hide resolved
$absUnionTypes = [];

foreach ($type->getTypes() as $unionType) {
$absUnionType = $this->tryAbsType($unionType);

if ($absUnionType === null) {
return null;
}

foreach ($absUnionTypes as $index => $otherAbsUnionType) {
if (!($otherAbsUnionType instanceof IntegerRangeType)) {
continue;
}

$unionRange = $otherAbsUnionType->tryUnion($absUnionType);

if ($unionRange !== null) {
$absUnionTypes[$index] = $unionRange;

continue 2;
}
}

$absUnionTypes[] = $absUnionType;
}

if (count($absUnionTypes) === 1) {
return $absUnionTypes[0];
}

return new UnionType($absUnionTypes);
}

return $this->tryAbsType($type);
}

private function tryAbsType(Type $type): ?Type
{
$numberType = $type->toNumber();

if (
$numberType instanceof IntegerRangeType
|| $numberType instanceof ConstantIntegerType
|| $numberType instanceof ConstantFloatType

) {
return $this->absType($numberType);
}

return null;
}

private function absType(ConstantIntegerType|IntegerRangeType|ConstantFloatType $type): Type
{
if ($type instanceof ConstantIntegerType) {
return new ConstantIntegerType(abs($type->getValue()));
}

if ($type instanceof ConstantFloatType) {
return new ConstantFloatType(abs($type->getValue()));
}

$min = $type->getMin();
$max = $type->getMax();

if ($min !== null && $min >= 0) {
return IntegerRangeType::fromInterval($min, $max);
}

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

return IntegerRangeType::fromInterval(0, $inversedMin !== null && $max !== null ? max($inversedMin, $max) : null);
}

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

}
166 changes: 166 additions & 0 deletions tests/PHPStan/Analyser/nsrt/abs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php declare(strict_types = 1);

namespace Abs;

use function PHPStan\Testing\assertType;

class Foo
{

public function singleIntegerRange(int $int): void
{
/** @var int $int */
assertType('int<0, max>', abs($int));

/** @var positive-int $int */
assertType('int<1, max>', abs($int));

/** @var negative-int $int */
assertType('int<1, max>', abs($int));

/** @var non-negative-int $int */
assertType('int<0, max>', abs($int));

/** @var non-positive-int $int */
assertType('int<0, max>', abs($int));

/** @var int<0, max> $int */
assertType('int<0, max>', abs($int));

/** @var int<0, 123> $int */
assertType('int<0, 123>', abs($int));

/** @var int<-123, 0> $int */
assertType('int<0, 123>', abs($int));

/** @var int<1, max> $int */
assertType('int<1, max>', abs($int));

/** @var int<123, max> $int */
assertType('int<123, max>', abs($int));

/** @var int<123, 456> $int */
assertType('int<123, 456>', abs($int));

/** @var int<min, 0> $int */
assertType('int<0, max>', abs($int));

/** @var int<min, -1> $int */
assertType('int<1, max>', abs($int));

/** @var int<min, -123> $int */
assertType('int<123, max>', abs($int));

/** @var int<-456, -123> $int */
assertType('int<123, 456>', abs($int));

/** @var int<-123, 123> $int */
assertType('int<0, 123>', abs($int));

/** @var int<min, max> $int */
assertType('int<0, max>', abs($int));
}

public function multipleIntegerRanges(int $int): void
{
/** @var non-zero-int $int */
assertType('int<1, max>', abs($int));

/** @var int<min, -1>|int<1, max> $int */
assertType('int<1, max>', abs($int));

/** @var int<-20, -10>|int<5, 25> $int */
assertType('int<5, 25>', abs($int));

/** @var int<-20, -5>|int<10, 25> $int */
assertType('int<5, 25>', abs($int));

/** @var int<-25, -10>|int<5, 20> $int */
assertType('int<5, 25>', abs($int));

/** @var int<-20, -10>|int<20, 30> $int */
assertType('int<10, 30>', abs($int));
}

public function constantInteger(int $int): void
{
/** @var 0 $int */
assertType('0', abs($int));

/** @var 1 $int */
assertType('1', abs($int));

/** @var -1 $int */
assertType('1', abs($int));
}

public function mixedIntegerUnion(int $int): void
{
/** @var 123|int<456, max> $int */
assertType('123|int<456, max>', abs($int));

/** @var int<min, -456>|-123 $int */
assertType('123|int<456, max>', abs($int));

/** @var -123|int<124, 125> $int */
assertType('int<123, 125>', abs($int));

/** @var int<124, 125>|-123 $int */
assertType('int<123, 125>', abs($int));
}

public function constantFloat(float $float): void
{
/** @var 0.0 $float */
assertType('0.0', abs($float));

/** @var 1.0 $float */
assertType('1.0', abs($float));

/** @var -1.0 $float */
assertType('1.0', abs($float));
}

public function string(string $string): void
{
/** @var string $string */
assertType('float|int<0, max>', abs($string));

/** @var numeric-string $string */
assertType('float|int<0, max>', abs($string));

/** @var '-1' $string */
assertType('1', abs($string));

/** @var '-1'|'-2.0'|'3.0'|'4' $string */
assertType('1|2.0|3.0|4', abs($string));
}

public function mixedUnion(mixed $value): void
{
/** @var 1.0|int<2, 3> $value */
assertType('1.0|int<2, 3>', abs($value));

/** @var -1.0|int<-3, -2> $value */
assertType('1.0|int<2, 3>', abs($value));

/** @var 2.0|int<1, 3> $value */
assertType('2.0|int<1, 3>', abs($value));

/** @var -2.0|int<-3, -1> $value */
assertType('2.0|int<1, 3>', abs($value));

/** @var -1.0|int<2, 3>|numeric-string $value */
assertType('float|int<0, max>', abs($value));
}

public function invalidType(mixed $nonInt): void
{
/** @var string $nonInt */
assertType('float|int<0, max>', abs($nonInt));

/** @var string|positive-int $nonInt */
assertType('float|int<0, max>', abs($nonInt));
}

}
Loading