1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

better inference of string increment (#5777)

* better inference of string increment

* fix combination
This commit is contained in:
orklah 2021-05-17 14:44:53 +02:00 committed by GitHub
parent f6bdeb6234
commit 4c4d574bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 7 deletions

View File

@ -384,9 +384,9 @@ class NonDivArithmeticOpAnalyzer
$has_string_increment = true;
if (!$result_type) {
$result_type = Type::getString();
$result_type = Type::getNonEmptyString();
} else {
$result_type = Type::combineUnionTypes(Type::getString(), $result_type);
$result_type = Type::combineUnionTypes(Type::getNonEmptyString(), $result_type);
}
$has_valid_left_operand = true;

View File

@ -42,7 +42,7 @@ class IncDecExpressionAnalyzer
$stmt_var_type = $statements_analyzer->node_data->getType($stmt->var);
if ($stmt instanceof PostInc || $stmt instanceof PostDec) {
$statements_analyzer->node_data->setType($stmt, $stmt_var_type ?: Type::getMixed());
$statements_analyzer->node_data->setType($stmt, $stmt_var_type ?? Type::getMixed());
}
if (($stmt_var_type = $statements_analyzer->node_data->getType($stmt->var))
@ -64,9 +64,8 @@ class IncDecExpressionAnalyzer
$context
);
$stmt_type = clone $stmt_var_type;
$statements_analyzer->node_data->setType($stmt, $stmt_type);
$result_type = $return_type ?? Type::getMixed();
$statements_analyzer->node_data->setType($stmt, $result_type);
BinaryOpAnalyzer::addDataFlow(
$statements_analyzer,
@ -81,7 +80,7 @@ class IncDecExpressionAnalyzer
$codebase = $statements_analyzer->getCodebase();
if ($var_id && isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = $stmt_type;
$context->vars_in_scope[$var_id] = $result_type;
if ($codebase->find_unused_variables && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
$context->assigned_var_ids[$var_id] = (int) $stmt->var->getAttribute('startFilePos');

View File

@ -384,6 +384,18 @@ class BinaryOperationTest extends TestCase
'$a' => 'string',
],
],
'stringIncrementWithCheck' => [
'<?php
/** @psalm-suppress StringIncrement */
for($a = "a"; $a != "z"; $a++){
if($a === "b"){
echo "b reached";
}
}',
'assertions' => [
'$a===' => 'non-empty-string',
],
],
'nullCoalescingAssignment' => [
'<?php
function foo(?string $s): string {