-
Notifications
You must be signed in to change notification settings - Fork 523
Improve abs()
return type
#3263
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
$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); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.