Skip to content

Commit

Permalink
Replace some LNumber usages
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 19, 2022
1 parent d71e7af commit 8013b0e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

- [BC] The only optional boolean parameter of `TKeyedArray::getGenericArrayType` was removed, and was replaced with a string parameter with a different meaning.

- [BC] The `TDependentListKey` type was removed, replacing it with an optional property of the `TIntRange` type.
- [BC] The `TDependentListKey` type was removed and replaced with an optional property of the `TIntRange` type.

# Upgrading from Psalm 4 to Psalm 5
## Changed
Expand Down
7 changes: 4 additions & 3 deletions src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ public static function analyze(
if (count($stmt->init) === 1
&& count($stmt->cond) === 1
&& $cond instanceof PhpParser\Node\Expr\BinaryOp
&& $cond->right instanceof PhpParser\Node\Scalar\LNumber
&& ($cond_value = $statements_analyzer->node_data->getType($cond->right))
&& ($cond_value->isSingleIntLiteral() || $cond_value->isSingleStringLiteral())
&& $cond->left instanceof PhpParser\Node\Expr\Variable
&& is_string($cond->left->name)
&& isset($init_var_types[$cond->left->name])
&& $init_var_types[$cond->left->name]->isSingleIntLiteral()
) {
$init_value = $init_var_types[$cond->left->name]->getSingleIntLiteral()->value;
$cond_value = $cond->right->value;
$init_value = $init_var_types[$cond->left->name]->getSingleLiteral()->value;
$cond_value = $cond_value->getSingleLiteral()->value;

if ($cond instanceof PhpParser\Node\Expr\BinaryOp\Smaller && $init_value < $cond_value) {
$always_enters_loop = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static function analyze(
Context $context
): ?bool {
$while_true = ($stmt->cond instanceof PhpParser\Node\Expr\ConstFetch && $stmt->cond->name->parts === ['true'])
|| ($stmt->cond instanceof PhpParser\Node\Scalar\LNumber && $stmt->cond->value > 0);
|| (($t = $statements_analyzer->node_data->getType($stmt->cond))
&& $t->isAlwaysTruthy());

$pre_context = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,16 @@ public static function getPathTo(
if ($stmt->getArgs()[1]->value instanceof PhpParser\Node\Scalar\LNumber) {
$dir_level = $stmt->getArgs()[1]->value->value;
} else {
return null;
if ($statements_analyzer) {
$t = $statements_analyzer->node_data->getType($stmt->getArgs()[1]->value);
if ($t && $t->isSingleIntLiteral()) {
$dir_level = $t->getSingleIntLiteral()->value;
} else {
return null;
}
} else {
return null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Psalm\Internal\Provider\ReturnTypeProvider;

use PhpParser;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
Expand Down Expand Up @@ -54,15 +53,21 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$key_type = $first_arg_array->getGenericKeyType();
}

if (!$second_arg
|| ($second_arg instanceof PhpParser\Node\Scalar\LNumber && $second_arg->value === 1)
if (!$second_arg) {
return $key_type;
}

$second_arg_type = $statements_source->node_data->getType($second_arg);
if ($second_arg_type
&& $second_arg_type->isSingleIntLiteral()
&& $second_arg_type->getSingleIntLiteral()->value === 1
) {
return $key_type;
}

$arr_type = Type::getList($key_type);

if ($second_arg instanceof PhpParser\Node\Scalar\LNumber) {
if ($second_arg_type && $second_arg_type->isSingleIntLiteral()) {
return $arr_type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev

$can_return_empty = isset($call_args[2])
&& (
!$call_args[2]->value instanceof PhpParser\Node\Scalar\LNumber
|| $call_args[2]->value->value < 0
!($third_arg_type = $statements_source->node_data->getType($call_args[2]->value))
|| !$third_arg_type->isSingleIntLiteral()
|| $third_arg_type->getSingleIntLiteral()->value < 0
);

if ($call_args[0]->value instanceof PhpParser\Node\Scalar\String_) {
Expand Down

0 comments on commit 8013b0e

Please sign in to comment.