forked from vimeo/psalm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f2ae28
commit 51b1b46
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; | ||
use Psalm\Type; | ||
|
||
use function array_values; | ||
use function count; | ||
|
||
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); | ||
|
||
if (count($call_args) > 1) { | ||
$precision_val = $call_args[1]->value; | ||
} else { | ||
$precision_val = 0; | ||
} | ||
|
||
if ($num_arg !== null && $num_arg->isSingle()) { | ||
$num_type = array_values($num_arg->getAtomicTypes())[0]; | ||
if ($num_type instanceof Type\Atomic\TFloat) { | ||
if ($precision_val > 0) { | ||
return new Type\Union([new Type\Atomic\TFloat()]); | ||
} | ||
|
||
return new Type\Union([new Type\Atomic\TInt()]); | ||
} | ||
|
||
if ($num_type instanceof Type\Atomic\TInt) { | ||
return new Type\Union([new Type\Atomic\TInt()]); | ||
} | ||
} | ||
|
||
return new Type\Union([new Type\Atomic\TInt(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Psalm\Tests\ReturnTypeProvider; | ||
|
||
use Psalm\Tests\TestCase; | ||
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait; | ||
|
||
class RoundTest extends TestCase | ||
{ | ||
use ValidCodeAnalysisTestTrait; | ||
|
||
public function providerValidCodeParse(): iterable | ||
{ | ||
yield 'roundWithStrictReturnType' => [ | ||
'<?php | ||
/** | ||
* @param float $a | ||
* @return int | ||
*/ | ||
function f(float $a): int | ||
{ | ||
return round($a); | ||
} | ||
/** | ||
* @param float $a | ||
* @return positive-int | ||
*/ | ||
function g(float $a): int | ||
{ | ||
return round($a, -1); | ||
} | ||
/** | ||
* @param float $a | ||
* @return float | ||
*/ | ||
function h(float $a): int | ||
{ | ||
return round($a, 2); | ||
} | ||
', | ||
]; | ||
} | ||
} |