-
Notifications
You must be signed in to change notification settings - Fork 541
Truthy isset($arr[$k])
should narrow $k
#3453
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
Merged
+308
−0
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f3ddac2
Truthy `isset($arr[$k])` should narrow `$k`
staabm 4d2b54e
non benevolent
staabm 06a7b60
fix
staabm 9e51c2c
fix
staabm 1ebfbca
no resource
staabm 1026c44
fix empty string
staabm 1c36120
fix '0' and '1'
staabm 76bd658
don't narrow floats
staabm 3804294
simplify
staabm b0876ac
test we don't narrow non-array offset-accesibles
staabm ea4f13b
Update bug-11716.php
staabm 10c3f2e
simplify
staabm c22193f
test numeric-string to int conversion
staabm ac7a9e5
Delete bug-10577.php
staabm fbd20f9
support non-variables
staabm 7c66cb7
Update bug-11716.php
staabm d2df514
fix 7.3/7.4 build
staabm 0c70de1
Update bug-11716.php
staabm 8b9f079
added regression test
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,214 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace Bug11716; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class TypeExpression | ||
{ | ||
/** | ||
* @return '&'|'|' | ||
*/ | ||
public function parse(string $glue): string | ||
{ | ||
$seenGlues = ['|' => false, '&' => false]; | ||
|
||
assertType("array{|: false, &: false}", $seenGlues); | ||
|
||
if ($glue !== '') { | ||
assertType('non-empty-string', $glue); | ||
|
||
\assert(isset($seenGlues[$glue])); | ||
$seenGlues[$glue] = true; | ||
|
||
assertType("'&'|'|'", $glue); | ||
assertType('array{|: bool, &: bool}', $seenGlues); | ||
} else { | ||
assertType("''", $glue); | ||
} | ||
|
||
assertType("''|'&'|'|'", $glue); | ||
assertType("array{|: bool, &: bool}", $seenGlues); | ||
|
||
return array_key_first($seenGlues); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<int, string> $arr | ||
*/ | ||
function narrowKey($mixed, string $s, int $i, array $generalArr, array $arr): void { | ||
if (isset($generalArr[$mixed])) { | ||
assertType('mixed~(array|object|resource)', $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
if (isset($generalArr[$i])) { | ||
assertType('int', $i); | ||
} else { | ||
assertType('int', $i); | ||
} | ||
assertType('int', $i); | ||
|
||
if (isset($generalArr[$s])) { | ||
assertType('string', $s); | ||
} else { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
if (isset($arr[$mixed])) { | ||
assertType('mixed~(array|object|resource)', $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
if (isset($arr[$i])) { | ||
assertType('int', $i); | ||
} else { | ||
assertType('int', $i); | ||
} | ||
assertType('int', $i); | ||
|
||
if (isset($arr[$s])) { | ||
assertType('string', $s); | ||
} else { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
} | ||
|
||
/** | ||
* @param array<int, array<string, float>> $arr | ||
*/ | ||
function multiDim($mixed, $mixed2, array $arr) { | ||
if (isset($arr[$mixed])) { | ||
assertType('mixed~(array|object|resource)', $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
if (isset($arr[$mixed]) && isset($arr[$mixed][$mixed2])) { | ||
assertType('mixed~(array|object|resource)', $mixed); | ||
assertType('mixed~(array|object|resource)', $mixed2); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
if (isset($arr[$mixed][$mixed2])) { | ||
assertType('mixed~(array|object|resource)', $mixed); | ||
assertType('mixed~(array|object|resource)', $mixed2); | ||
} else { | ||
assertType('mixed', $mixed); | ||
assertType('mixed', $mixed2); | ||
} | ||
assertType('mixed', $mixed); | ||
assertType('mixed', $mixed2); | ||
} | ||
|
||
/** | ||
* @param array<int, string> $arr | ||
*/ | ||
function emptyArrr($mixed, array $arr) | ||
{ | ||
if (count($arr) !== 0) { | ||
return; | ||
} | ||
|
||
assertType('array{}', $arr); | ||
if (isset($arr[$mixed])) { | ||
assertType('mixed', $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
} | ||
|
||
function emptyString($mixed) | ||
{ | ||
// see https://3v4l.org/XHZdr | ||
$arr = ['' => 1, 'a' => 2]; | ||
if (isset($arr[$mixed])) { | ||
assertType("''|'a'|null", $mixed); | ||
} else { | ||
assertType('mixed', $mixed); // could be mixed~(''|'a'|null) | ||
} | ||
assertType('mixed', $mixed); | ||
} | ||
|
||
function numericString($mixed, int $i, string $s) | ||
{ | ||
$arr = ['1' => 1, '2' => 2]; | ||
if (isset($arr[$mixed])) { | ||
assertType("1|2|'1'|'2'|float|true", $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
$arr = ['0' => 1, '2' => 2]; | ||
if (isset($arr[$mixed])) { | ||
assertType("0|2|'0'|'2'|float|false", $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
$arr = ['1' => 1, '2' => 2]; | ||
if (isset($arr[$i])) { | ||
assertType("1|2", $i); | ||
} else { | ||
assertType('int', $i); | ||
} | ||
assertType('int', $i); | ||
|
||
$arr = ['1' => 1, '2' => 2, 3 => 3]; | ||
if (isset($arr[$s])) { | ||
assertType("'1'|'2'|'3'", $s); | ||
} else { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
|
||
$arr = ['1' => 1, '2' => 2, 3 => 3]; | ||
if (isset($arr[substr($s, 10)])) { | ||
assertType("string", $s); | ||
assertType("'1'|'2'|'3'", substr($s, 10)); | ||
} else { | ||
assertType('string', $s); | ||
} | ||
assertType('string', $s); | ||
} | ||
|
||
function intKeys($mixed) | ||
{ | ||
$arr = [1 => 1, 2 => 2]; | ||
if (isset($arr[$mixed])) { | ||
assertType("1|2|'1'|'2'|float|true", $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
|
||
$arr = [0 => 0, 1 => 1, 2 => 2]; | ||
if (isset($arr[$mixed])) { | ||
assertType("0|1|2|'0'|'1'|'2'|bool|float", $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
} | ||
|
||
function arrayAccess(\ArrayAccess $arr, $mixed) { | ||
if (isset($arr[$mixed])) { | ||
assertType("mixed", $mixed); | ||
} else { | ||
assertType('mixed', $mixed); | ||
} | ||
assertType('mixed', $mixed); | ||
} |
This file contains hidden or 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,40 @@ | ||
<?php | ||
|
||
namespace Bug8559; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class X | ||
{ | ||
const KEYS = ['a' => 1, 'b' => 2]; | ||
|
||
/** | ||
* @phpstan-assert key-of<self::KEYS> $key | ||
* @return value-of<self::KEYS> | ||
*/ | ||
public static function get(string $key): int | ||
{ | ||
assert(isset(self::KEYS[$key])); | ||
assertType("'a'|'b'", $key); | ||
return self::KEYS[$key]; | ||
} | ||
|
||
/** | ||
* @phpstan-assert key-of<self::KEYS> $key | ||
* @return value-of<self::KEYS> | ||
*/ | ||
public static function get2(string $key): int | ||
{ | ||
assert(in_array($key, array_keys(self::KEYS), true)); | ||
assertType("'a'|'b'", $key); | ||
return self::KEYS[$key]; | ||
} | ||
} | ||
|
||
$key = 'x'; | ||
$v = X::get($key); | ||
assertType("*NEVER*", $key); | ||
|
||
$key = 'a'; | ||
$v = X::get($key); | ||
assertType("'a'", $key); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.