Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Literal inferring for array_column, array_combine, array_fill_keys, a…
…rray_fill (#8850) * Squash * Remove BC break * Suppress * Possibly fix * Fixes * Fix test * Implement literal array_column * Improve array_column inference * Improve logic * Add array_combine return type provider * Cleanup * Fix #8868 (add array_fill_keys/array_fill return type) * cs-fix * Fix
- Loading branch information
Showing
7 changed files
with
596 additions
and
78 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
132 changes: 132 additions & 0 deletions
132
src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayCombineReturnTypeProvider.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,132 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use Psalm\Internal\Analyzer\StatementsAnalyzer; | ||
use Psalm\Issue\InvalidArgument; | ||
use Psalm\IssueBuffer; | ||
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; | ||
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; | ||
use Psalm\Type; | ||
use Psalm\Type\Atomic\TArray; | ||
use Psalm\Type\Atomic\TKeyedArray; | ||
use Psalm\Type\Union; | ||
|
||
use function array_combine; | ||
use function assert; | ||
use function count; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class ArrayCombineReturnTypeProvider implements FunctionReturnTypeProviderInterface | ||
{ | ||
/** | ||
* @return array<lowercase-string> | ||
*/ | ||
public static function getFunctionIds(): array | ||
{ | ||
return ['array_combine']; | ||
} | ||
|
||
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union | ||
{ | ||
$statements_source = $event->getStatementsSource(); | ||
$call_args = $event->getCallArgs(); | ||
if (!$statements_source instanceof StatementsAnalyzer | ||
|| count($call_args) < 2 | ||
) { | ||
return Type::getNever(); | ||
} | ||
|
||
if (!$keys_type = $statements_source->node_data->getType($call_args[0]->value)) { | ||
return null; | ||
} | ||
if (!$keys_type->isArray()) { | ||
return null; | ||
} | ||
|
||
$keys = $keys_type->getArray(); | ||
if ($keys instanceof TArray && $keys->isEmptyArray()) { | ||
$keys = []; | ||
} elseif (!$keys instanceof TKeyedArray || $keys->fallback_params) { | ||
return null; | ||
} else { | ||
$keys = $keys->properties; | ||
} | ||
|
||
if (!$values_type = $statements_source->node_data->getType($call_args[1]->value)) { | ||
return null; | ||
} | ||
if (!$values_type->isArray()) { | ||
return null; | ||
} | ||
|
||
$values = $values_type->getArray(); | ||
if ($values instanceof TArray && $values->isEmptyArray()) { | ||
$values = []; | ||
} elseif (!$values instanceof TKeyedArray || $values->fallback_params) { | ||
return null; | ||
} else { | ||
$values = $values->properties; | ||
} | ||
|
||
|
||
$keys_array = []; | ||
$is_list = true; | ||
$prev_key = -1; | ||
foreach ($keys as $key) { | ||
if ($key->possibly_undefined) { | ||
return null; | ||
} | ||
if ($key->isSingleIntLiteral()) { | ||
$key = $key->getSingleIntLiteral()->value; | ||
$keys_array []= $key; | ||
if ($is_list && $key-1 !== $prev_key) { | ||
$is_list = false; | ||
} | ||
$prev_key = $key; | ||
} elseif ($key->isSingleStringLiteral()) { | ||
$keys_array []= $key->getSingleStringLiteral()->value; | ||
$is_list = false; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
foreach ($values as $value) { | ||
if ($value->possibly_undefined) { | ||
return null; | ||
} | ||
} | ||
|
||
if (count($keys_array) !== count($values)) { | ||
IssueBuffer::maybeAdd( | ||
new InvalidArgument( | ||
'The keys array ' . $keys_type->getId() . ' must have exactly the same ' | ||
. 'number of elements as the values array ' | ||
. $values_type->getId(), | ||
$event->getCodeLocation(), | ||
'array_combine' | ||
), | ||
$statements_source->getSuppressedIssues() | ||
); | ||
return $statements_source->getCodebase()->analysis_php_version_id >= 8_00_00 | ||
? Type::getNever() | ||
: Type::getFalse(); | ||
} | ||
|
||
$result = array_combine( | ||
$keys_array, | ||
$values | ||
); | ||
|
||
assert($result !== false); | ||
|
||
if (!$result) { | ||
return Type::getEmptyArray(); | ||
} | ||
|
||
return new Union([new TKeyedArray($result, null, null, $is_list)]); | ||
} | ||
} |
Oops, something went wrong.