Skip to content

Commit

Permalink
infer non-falsy-string in string containing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Aug 30, 2022
1 parent ad8335d commit b9665c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/Type/Php/StrContainingTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\IntersectionType;
Expand Down Expand Up @@ -66,9 +67,14 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
if ($needleType->isNonEmptyString()->yes() && $haystackType->isString()->yes()) {
$accessories = [
new StringType(),
new AccessoryNonEmptyStringType(),
];

if ($needleType->isNonFalsyString()->yes()) {
$accessories[] = new AccessoryNonFalsyStringType();
} else {
$accessories[] = new AccessoryNonEmptyStringType();
}

if ($haystackType->isLiteralString()->yes()) {
$accessories[] = new AccessoryLiteralStringType();
}
Expand Down
33 changes: 23 additions & 10 deletions tests/PHPStan/Analyser/data/non-empty-string-str-containing-fns.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
class Foo {
/**
* @param non-empty-string $nonES
* @param non-falsy-string $nonFalsy
* @param numeric-string $numS
* @param literal-string $literalS
* @param non-empty-string&numeric-string $nonEAndNumericS
*/
public function strContains(string $s, string $s2, $nonES, $numS, $literalS, $nonEAndNumericS, int $i): void
public function strContains(string $s, string $s2, $nonES, $nonFalsy, $numS, $literalS, $nonEAndNumericS, int $i): void
{
if (str_contains($s, ':')) {
assertType('non-falsy-string', $s);
}
assertType('string', $s);

if (str_contains($s, '0')) {
assertType('non-empty-string', $s);
}
assertType('string', $s);
Expand Down Expand Up @@ -49,26 +55,33 @@ public function strContains(string $s, string $s2, $nonES, $numS, $literalS, $no
if (str_contains($i, $s2)) {
assertType('int', $i);
}

if (str_contains($s, $nonFalsy)) {
assertType('non-falsy-string', $s);
}
if (str_contains($numS, $nonFalsy)) {
assertType('non-falsy-string&numeric-string', $numS);
}
}

public function variants(string $s) {
if (fnmatch("*gr[ae]y", $s)) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);

if (str_starts_with($s, ':')) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);

if (str_ends_with($s, ':')) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);

if (strpos($s, ':') !== false) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);
if (strpos($s, ':') === false) {
Expand All @@ -86,17 +99,17 @@ public function variants(string $s) {
assertType('string', $s);

if (strrpos($s, ':') !== false) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);

if (stripos($s, ':') !== false) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);

if (strripos($s, ':') !== false) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);

Expand All @@ -109,13 +122,13 @@ public function variants(string $s) {
}
assertType('string', $s);
if (strstr($s, ':', true) !== false) {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);
if (strstr($s, ':', true) === false) {
assertType('string', $s);
} else {
assertType('non-empty-string', $s);
assertType('non-falsy-string', $s);
}
assertType('string', $s);
}
Expand Down

0 comments on commit b9665c3

Please sign in to comment.