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 substr() comparison with constant string
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 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
79 changes: 79 additions & 0 deletions
79
tests/PHPStan/Analyser/data/non-empty-string-substr-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,79 @@ | ||
<?php | ||
|
||
namespace NonEmptyStringSubstr; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo { | ||
public function nonEmptySubstr(string $s, int $offset, int $length): void | ||
{ | ||
if (substr($s, 10) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === substr($s, 10)) { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, -10) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('hallo' === substr($s, -10)) { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, 10, 5) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, 10, -5) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, $offset) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, $offset, $length) === 'hallo') { | ||
assertType('non-empty-string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, $offset, $length) !== 'hallo') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, $offset, $length) === '') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('' === substr($s, $offset, $length)) { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (substr($s, $offset, $length) == '') { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
if ('' == substr($s, $offset, $length)) { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
$x = (substr($s, 10) === 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
|
||
$x = (substr($s, 10) !== 'hallo'); | ||
assertType('string', $s); | ||
var_dump($x); | ||
} | ||
} |