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

More precise implode() return type #3774

Open
wants to merge 3 commits into
base: 1.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,23 @@ private function implode(Type $arrayType, Type $separatorType): Type
}

$accessoryTypes = [];
$valueTypeAsString = $arrayType->getIterableValueType()->toString();
Copy link
Contributor Author

@staabm staabm Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the actual change is here: we use toString as the implode function internally casts everything.

that way the string types detection also works for unions of strings and non-strings (e.g. 2|'a') and so we get more precise results

if ($arrayType->isIterableAtLeastOnce()->yes()) {
if ($arrayType->getIterableValueType()->isNonFalsyString()->yes() || $separatorType->isNonFalsyString()->yes()) {
if ($valueTypeAsString->isNonFalsyString()->yes() || $separatorType->isNonFalsyString()->yes()) {
$accessoryTypes[] = new AccessoryNonFalsyStringType();
} elseif ($arrayType->getIterableValueType()->isNonEmptyString()->yes() || $separatorType->isNonEmptyString()->yes()) {
} elseif ($valueTypeAsString->isNonEmptyString()->yes() || $separatorType->isNonEmptyString()->yes()) {
$accessoryTypes[] = new AccessoryNonEmptyStringType();
}
}

// implode is one of the four functions that can produce literal strings as blessed by the original RFC: wiki.php.net/rfc/is_literal
if ($arrayType->getIterableValueType()->isLiteralString()->yes() && $separatorType->isLiteralString()->yes()) {
if ($valueTypeAsString->isLiteralString()->yes() && $separatorType->isLiteralString()->yes()) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}
if ($arrayType->getIterableValueType()->isLowercaseString()->yes() && $separatorType->isLowercaseString()->yes()) {
if ($valueTypeAsString->isLowercaseString()->yes() && $separatorType->isLowercaseString()->yes()) {
$accessoryTypes[] = new AccessoryLowercaseStringType();
}
if ($arrayType->getIterableValueType()->isUppercaseString()->yes() && $separatorType->isUppercaseString()->yes()) {
if ($valueTypeAsString->isUppercaseString()->yes() && $separatorType->isUppercaseString()->yes()) {
$accessoryTypes[] = new AccessoryUppercaseStringType();
}

Expand Down
11 changes: 6 additions & 5 deletions src/Type/Php/StrCaseFunctionsReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,19 @@ public function getTypeFromFunctionCall(
}

$accessoryTypes = [];
if ($forceLowercase || ($keepLowercase && $argType->isLowercaseString()->yes())) {
$argStringType = $argType->toString();
if ($forceLowercase || ($keepLowercase && $argStringType->isLowercaseString()->yes())) {
$accessoryTypes[] = new AccessoryLowercaseStringType();
}
if ($forceUppercase || ($keepUppercase && $argType->isUppercaseString()->yes())) {
if ($forceUppercase || ($keepUppercase && $argStringType->isUppercaseString()->yes())) {
$accessoryTypes[] = new AccessoryUppercaseStringType();
}

if ($argType->isNumericString()->yes()) {
if ($argStringType->isNumericString()->yes()) {
$accessoryTypes[] = new AccessoryNumericStringType();
} elseif ($argType->isNonFalsyString()->yes()) {
} elseif ($argStringType->isNonFalsyString()->yes()) {
$accessoryTypes[] = new AccessoryNonFalsyStringType();
} elseif ($argType->isNonEmptyString()->yes()) {
} elseif ($argStringType->isNonEmptyString()->yes()) {
$accessoryTypes[] = new AccessoryNonEmptyStringType();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Php/StrContainingTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
[$hackstackArg, $needleArg] = self::STR_CONTAINING_FUNCTIONS[strtolower($functionReflection->getName())];

$haystackType = $scope->getType($args[$hackstackArg]->value);
$needleType = $scope->getType($args[$needleArg]->value);
$needleType = $scope->getType($args[$needleArg]->value)->toString();

if ($needleType->isNonEmptyString()->yes() && $haystackType->isString()->yes()) {
$accessories = [
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-11201.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function returnsBool(): bool {
assertType('string', $s);

$s = sprintf("%s", implode(', ', array_map('intval', returnsArray())));
assertType('string', $s);
assertType('lowercase-string&uppercase-string', $s);

$s = sprintf('%2$s', 1234, returnsNonFalsyString());
assertType('non-falsy-string', $s);
Expand Down
14 changes: 13 additions & 1 deletion tests/PHPStan/Analyser/nsrt/implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

class Foo
{
/**
* @param array<int> $arr
*/
public function ints(array $arr, int $i)
{
assertType("lowercase-string&uppercase-string", implode($arr));
assertType("lowercase-string&non-empty-string&uppercase-string", implode([$i, $i]));
if ($i !== 0) {
assertType("lowercase-string&non-falsy-string&uppercase-string", implode([$i, $i]));
}
}

const X = 'x';
const ONE = 1;

Expand Down Expand Up @@ -49,6 +61,6 @@ public function constArrays5($constArr) {

/** @param array{0: 1, 1: 'a'|'b', 3?: 'c'|'d', 4?: 'e'|'f', 5?: 'g'|'h', 6?: 'x'|'y'} $constArr */
public function constArrays6($constArr) {
assertType("string", implode('', $constArr));
assertType("literal-string&lowercase-string&non-falsy-string", implode('', $constArr));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class Foo {
*/
public function strContains(string $s, string $s2, $nonES, $nonFalsy, $numS, $literalS, $nonEAndNumericS, int $i): void
{
if (str_contains($i, 0)) {
assertType('int', $i);
}
if (str_contains($s, 0)) {
assertType('non-empty-string', $s);
}
if (str_contains($s, 1)) {
assertType('non-falsy-string', $s);
}

if (str_contains($s, ':')) {
assertType('non-falsy-string', $s);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/non-empty-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function sayHello(int $i): void
// coming from issue #5291
$s = array(1, $i);

assertType('non-falsy-string', implode("a", $s));
assertType('lowercase-string&non-falsy-string', implode("a", $s));
Copy link
Contributor

@mvorisek mvorisek Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assertType('lowercase-string&non-falsy-string', implode("a", $s));
assertType('lowercase-string&non-falsy-string', implode("a", $s));
assertType('uppercase-string&non-falsy-string', implode("A", $s));

}

/**
Expand All @@ -233,7 +233,7 @@ public function sayHello2(int $i): void
// coming from issue #5291
$s = array(1, $i);

assertType('non-falsy-string', join("a", $s));
assertType('lowercase-string&non-falsy-string', join("a", $s));
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/nsrt/non-falsy-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function stringFunctions(string $s, $nonFalsey, $arrayOfNonFalsey, $nonEmptyArra
assertType('non-falsy-string', escapeshellarg($nonFalsey));
assertType('non-falsy-string', escapeshellcmd($nonFalsey));

assertType('non-falsy-string&uppercase-string', strtoupper($s ?: 1));
assertType('non-falsy-string&uppercase-string', strtoupper($nonFalsey));
assertType('lowercase-string&non-falsy-string', strtolower($nonFalsey));
assertType('non-falsy-string&uppercase-string', mb_strtoupper($nonFalsey));
Expand Down
Loading