-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infer non-empty-string on strstr() comparison with constant string
- Loading branch information
Showing
5 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/PHPStan/Analyser/data/non-empty-string-strrchr-specifying.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace NonEmptyStringStrrchr; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo { | ||
public function nonEmptyStrrchr(string $s, string $needle): void | ||
{ | ||
if (strrchr($s, 'abc') === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === strrchr($s, 'abc')) { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strrchr($s, $needle) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === strrchr($s, $needle)) { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
$x = (strrchr($s, $needle) === 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
|
||
$x = (strrchr($s, $needle) !== 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
tests/PHPStan/Analyser/data/non-empty-string-strstr-specifying.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace NonEmptyStringStrstr; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo { | ||
public function nonEmptyStrstr(string $s, string $needle, bool $before_needle): void | ||
{ | ||
if (strstr($s, 'abc') === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === strstr($s, 'abc')) { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === strstr($s, $needle)) { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle, true) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle, false) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle, $before_needle) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle, $before_needle) !== 'hallo') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle, $before_needle) === '') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('' === strstr($s, $needle, $before_needle)) { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (strstr($s, $needle, $before_needle) == '') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('' == strstr($s, $needle, $before_needle)) { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
$x = (strstr($s, $needle) === 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
|
||
$x = (strstr($s, $needle) !== 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
} | ||
|
||
public function nonEmptyStristr(string $s, string $needle, bool $before_needle): void | ||
{ | ||
if (stristr($s, 'abc') === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === stristr($s, 'abc')) { | ||
assertType('non-empty-string', $s); | ||
} | ||
|
||
if (stristr($s, $needle, $before_needle) == '') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('' == stristr($s, $needle, $before_needle)) { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
$x = (stristr($s, $needle) === 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
} | ||
|
||
|
||
// strchr() is an alias of strstr() | ||
public function nonEmptyStrchr(string $s, string $needle, bool $before_needle): void | ||
{ | ||
if (strchr($s, 'abc') === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === strchr($s, 'abc')) { | ||
assertType('non-empty-string', $s); | ||
} | ||
|
||
if (strchr($s, $needle, $before_needle) == '') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('' == strchr($s, $needle, $before_needle)) { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
$x = (strchr($s, $needle) === 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
} | ||
} |