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

concat should never remove non empty non falsy from string #9411 #9422

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,10 +29,12 @@
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TLowercaseString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNonFalsyString;
use Psalm\Type\Atomic\TNonspecificLiteralString;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumericString;
Expand Down Expand Up @@ -277,6 +279,31 @@ public static function analyze(
}
}
}
} elseif ($left_type || $right_type) {
/**
* @var Union $known_operand
*/
$known_operand = $right_type ?? $left_type;

if ($known_operand->isSingle()) {
$known_operands_atomic = $known_operand->getSingleAtomic();

if ($known_operands_atomic instanceof TNonEmptyString) {
$result_type = Type::getNonEmptyString();
}

if ($known_operands_atomic instanceof TNonFalsyString) {
$result_type = Type::getNonFalsyString();
}
EgorBakulin marked this conversation as resolved.
Show resolved Hide resolved

if ($known_operands_atomic instanceof TLiteralString) {
if ($known_operands_atomic->value) {
$result_type = Type::getNonFalsyString();
} elseif ($known_operands_atomic->value !== '') {
$result_type = Type::getNonEmptyString();
}
}
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/BinaryOperationTest.php
Expand Up @@ -489,6 +489,30 @@ function foobar(string $bar): string
foobar($foo . $bar);
',
],
'concatenateNonFalsyStringWithUndefinedConstant' => [
'code' => '<?php
/**
* @param non-falsy-string $arg
* @return non-falsy-string
*/
function foo( $arg ) {
/** @psalm-suppress UndefinedConstant */
return FOO . $arg;
}
',
],
'concatenateNonEmptyStringWithUndefinedConstant' => [
'code' => '<?php
/**
* @param non-empty-string $arg
* @return non-empty-string
*/
function foo( $arg ) {
/** @psalm-suppress UndefinedConstant */
return FOO . $arg;
}
',
],
'possiblyInvalidAdditionOnBothSides' => [
'code' => '<?php
function foo(string $s) : int {
Expand Down