Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String increment on numeric string #6166

Merged
merged 7 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,28 @@ private static function analyzeNonDivOperands(

if ($left_type_part instanceof Type\Atomic\TString
&& $right_type_part instanceof TInt
&& $parent instanceof PhpParser\Node\Expr\PostInc
&& (
$parent instanceof PhpParser\Node\Expr\PostInc ||
$parent instanceof PhpParser\Node\Expr\PreInc
)
) {
$has_string_increment = true;
if ($left_type_part instanceof Type\Atomic\TNumericString || (
$left_type_part instanceof Type\Atomic\TLiteralString &&
//Matches '1', '1.', '-1', '-1.'. Does not match '.1'
preg_match('/^\-?\d+\.?\d*$/', $left_type_part->value)
weirdan marked this conversation as resolved.
Show resolved Hide resolved
)
) {
$new_result_type = new Type\Union([new TFloat(), new TInt()]);
$new_result_type->from_calculation = true;
} else {
$new_result_type = Type::getNonEmptyString();
$has_string_increment = true;
}

if (!$result_type) {
$result_type = Type::getNonEmptyString();
if ($result_type) {
$result_type = Type::combineUnionTypes($new_result_type, $result_type);
} else {
$result_type = Type::combineUnionTypes(Type::getNonEmptyString(), $result_type);
$result_type = $new_result_type;
}

$has_valid_left_operand = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TNonEmptyString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Psalm\Type\Atomic;

/**
* Denotes a string, that is also non-empty
* Denotes a string, that is also non-empty (every string except '')
*/
class TNonEmptyString extends TString
{
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TNonFalsyString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Psalm\Type\Atomic;

/**
* Denotes a string, that is also non-empty
* Denotes a string, that is also non-falsy (every string except '' and '0')
*/
class TNonFalsyString extends TNonEmptyString
{
Expand Down
24 changes: 24 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,30 @@ function foo(int $s1): string {
return "Hello $s1 $s2";
}',
],
'NumericStringIncrement' => [
'<?php
function scope(array $a): int|float {
$offset = array_search("foo", $a);
if(is_numeric($offset)){
return $offset++;
}
else{
return 0;
}
}',
],
'NumericStringIncrementLiteral' => [
'<?php
$a = "123";
$b = "123";
$a++;
++$b;
',
'assertions' => [
'$a' => 'float|int',
'$b' => 'float|int',
],
],
];
}

Expand Down