Skip to content

Commit

Permalink
More precise string-containing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 11, 2025
1 parent 04dbcd4 commit 922859e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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/implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,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

0 comments on commit 922859e

Please sign in to comment.