Skip to content

Commit

Permalink
separate fix of vimeo#10481 to a reusable function
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Mar 22, 2024
1 parent 120e312 commit 6030995
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@
use function array_merge;
use function array_values;
use function count;
use function filter_var;
use function in_array;
use function is_int;
use function is_numeric;
use function is_string;
use function preg_match;
use function trim;

use const FILTER_VALIDATE_INT;
use const PHP_INT_MAX;

/**
Expand Down Expand Up @@ -237,6 +242,38 @@ public static function analyze(
return true;
}

/**
* @param string|int $literal_array_key
* @return false|int
* @psalm-assert-if-false !numeric $literal_array_key
*/
public static function getLiteralArrayKeyInt(
$literal_array_key
) {
if (is_int($literal_array_key)) {
return $literal_array_key;
}

if (!is_numeric($literal_array_key)) {
return false;
}

// PHP 8 values with whitespace after number are counted as numeric
// and filter_var treats them as such too
// ensures that '15 ' will stay '15 '
if (trim($literal_array_key) !== $literal_array_key) {
return false;
}

// '+5' will pass the filter_var check but won't be changed in keys
if ($literal_array_key[0] === '+') {
return false;
}

// e.g. 015 is numeric but won't be typecast as it's not a valid int
return filter_var($literal_array_key, FILTER_VALIDATE_INT);
}

private static function analyzeArrayItem(
StatementsAnalyzer $statements_analyzer,
Context $context,
Expand Down
10 changes: 2 additions & 8 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psalm\Codebase;
use Psalm\Exception\TypeParseTreeException;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\ArrayAnalyzer;
use Psalm\Internal\Type\ParseTree\CallableParamTree;
use Psalm\Internal\Type\ParseTree\CallableTree;
use Psalm\Internal\Type\ParseTree\CallableWithReturnTypeTree;
Expand Down Expand Up @@ -86,7 +87,6 @@
use function defined;
use function end;
use function explode;
use function filter_var;
use function get_class;
use function in_array;
use function is_int;
Expand All @@ -100,9 +100,6 @@
use function strtolower;
use function strtr;
use function substr;
use function trim;

use const FILTER_VALIDATE_INT;

/**
* @psalm-suppress InaccessibleProperty Allowed during construction
Expand Down Expand Up @@ -669,11 +666,8 @@ private static function getTypeFromGenericTree(
}

foreach ($generic_params[0]->getAtomicTypes() as $key => $atomic_type) {
// PHP 8 values with whitespace after number are counted as numeric
// and filter_var treats them as such too
if ($atomic_type instanceof TLiteralString
&& ($string_to_int = filter_var($atomic_type->value, FILTER_VALIDATE_INT)) !== false
&& trim($atomic_type->value) === $atomic_type->value
&& ($string_to_int = ArrayAnalyzer::getLiteralArrayKeyInt($atomic_type->value)) !== false
) {
$builder = $generic_params[0]->getBuilder();
$builder->removeType($key);
Expand Down

0 comments on commit 6030995

Please sign in to comment.