-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry-pick: Try to provide literal int types when possible (fixes #6966
) (#7071) * Fixed #6966 * Only accept >= 0 values for mode argument in round() * Made round() only return float or literal float values and remove unneeded test * Registered RoundReturnTypeProvider * Updated cast analyzer to handle single string literal int values as literal ints * Fixed psalm errors * Fix invalid property accesses * Addressed comments * Added Tests * Marked RoundReturnTypeProvider as internal * Fixed CS
- Loading branch information
1 parent
da68e9d
commit bf1c032
Showing
6 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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
78 changes: 78 additions & 0 deletions
78
src/Psalm/Internal/Provider/ReturnTypeProvider/RoundReturnTypeProvider.php
This file contains 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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Internal\Analyzer\StatementsAnalyzer; | ||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; | ||
use Psalm\Type; | ||
|
||
use function array_values; | ||
use function count; | ||
use function round; | ||
|
||
use const PHP_ROUND_HALF_UP; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class RoundReturnTypeProvider implements FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['round']; | ||
} | ||
|
||
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union | ||
{ | ||
$call_args = $event->getCallArgs(); | ||
if (count($call_args) === 0) { | ||
return null; | ||
} | ||
|
||
$statements_source = $event->getStatementsSource(); | ||
$nodeTypeProvider = $statements_source->getNodeTypeProvider(); | ||
|
||
$num_arg = $nodeTypeProvider->getType($call_args[0]->value); | ||
|
||
$precision_val = 0; | ||
if ($statements_source instanceof StatementsAnalyzer && count($call_args) > 1) { | ||
$type = $statements_source->node_data->getType($call_args[1]->value); | ||
|
||
if ($type !== null && $type->isSingle()) { | ||
$atomic_type = array_values($type->getAtomicTypes())[0]; | ||
if ($atomic_type instanceof Type\Atomic\TLiteralInt) { | ||
$precision_val = $atomic_type->value; | ||
} | ||
} | ||
} | ||
|
||
$mode_val = PHP_ROUND_HALF_UP; | ||
if ($statements_source instanceof StatementsAnalyzer && count($call_args) > 2) { | ||
$type = $statements_source->node_data->getType($call_args[2]->value); | ||
|
||
if ($type !== null && $type->isSingle()) { | ||
$atomic_type = array_values($type->getAtomicTypes())[0]; | ||
if ($atomic_type instanceof Type\Atomic\TLiteralInt) { | ||
/** @var positive-int|0 $mode_val */ | ||
$mode_val = $atomic_type->value; | ||
} | ||
} | ||
} | ||
|
||
if ($num_arg !== null && $num_arg->isSingle()) { | ||
$num_type = array_values($num_arg->getAtomicTypes())[0]; | ||
if ($num_type instanceof Type\Atomic\TLiteralFloat || $num_type instanceof Type\Atomic\TLiteralInt) { | ||
$rounded_val = round($num_type->value, $precision_val, $mode_val); | ||
return new Type\Union([new Type\Atomic\TLiteralFloat($rounded_val)]); | ||
} | ||
} | ||
|
||
return new Type\Union([new Type\Atomic\TFloat()]); | ||
} | ||
} |
This file contains 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 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