-
Notifications
You must be signed in to change notification settings - Fork 660
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
Showing
3 changed files
with
88 additions
and
0 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
57 changes: 57 additions & 0 deletions
57
src/Psalm/Internal/Provider/ReturnTypeProvider/MinMaxReturnTypeProvider.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 function count; | ||
use Psalm\Internal\Type\ArrayType; | ||
use Psalm\Type; | ||
|
||
class MinMaxReturnTypeProvider implements FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['min', 'max']; | ||
} | ||
|
||
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(); | ||
|
||
if (count($call_args) === 1 | ||
&& ($array_arg_type = $nodeTypeProvider->getType($call_args[0]->value)) | ||
&& $array_arg_type->isSingle() | ||
&& $array_arg_type->hasArray() | ||
&& ($array_type = ArrayType::infer($array_arg_type->getAtomicTypes()['array'])) | ||
) { | ||
return $array_type->value; | ||
} | ||
|
||
$atomics = []; | ||
foreach ($call_args as $arg) { | ||
if ($array_arg_type = $nodeTypeProvider->getType($arg->value)) { | ||
foreach ($array_arg_type->getAtomicTypes() as $atomicType) { | ||
$atomics[] = $atomicType; | ||
} | ||
} else { | ||
return Type::getMixed(); | ||
} | ||
} | ||
|
||
if ($atomics === []) { | ||
return Type::getMixed(); | ||
} | ||
|
||
return new Type\Union($atomics); | ||
} | ||
} |
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