-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#14390: Unconsistant behavior for offsetAccess.notFound #5310
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
Changes from all commits
80c9526
6b43b8d
3bb9cdb
978a497
a09c9df
8f8a19b
f3910e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| <?php // lint >= 8.2 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug14390; | ||
|
|
||
| readonly class Sample | ||
| { | ||
| /** | ||
| * @param array<string, string> $fields | ||
| */ | ||
| public function __construct( | ||
| public array $fields = [], | ||
| ) { | ||
| } | ||
| } | ||
|
|
||
| class Foo | ||
| { | ||
| public function bar( | ||
| Sample $sample, | ||
| ): void { | ||
| if ($sample->fields !== []) { | ||
| echo $sample->fields[array_key_first($sample->fields)]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, string> $fields | ||
| */ | ||
| public function zoo( | ||
| array $fields, | ||
| ): void { | ||
| if ($fields !== []) { | ||
| echo $fields[array_key_first($fields)]; | ||
| } | ||
| } | ||
|
|
||
| public function withKey( | ||
| Sample $sample, | ||
| ): void { | ||
| if ($sample->fields !== []) { | ||
| $key = array_key_first($sample->fields); | ||
| echo $sample->fields[$key]; | ||
| } | ||
| } | ||
|
|
||
| public function arrayKeyLast( | ||
| Sample $sample, | ||
| ): void { | ||
| if ($sample->fields !== []) { | ||
| echo $sample->fields[array_key_last($sample->fields)]; | ||
| } | ||
| } | ||
|
|
||
| public function arrayRand( | ||
| Sample $sample, | ||
| ): void { | ||
| if ($sample->fields !== []) { | ||
| echo $sample->fields[array_rand($sample->fields)]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-list<string> $list | ||
| */ | ||
| public function countMinus1( | ||
| array $list, | ||
| ): void { | ||
| echo $list[count($list) - 1]; | ||
| } | ||
| } | ||
|
|
||
| readonly class SampleList | ||
| { | ||
| /** | ||
| * @param list<string> $items | ||
| */ | ||
| public function __construct( | ||
| public array $items = [], | ||
| ) { | ||
| } | ||
| } | ||
|
|
||
| class Bar | ||
| { | ||
| public function countMinus1Property( | ||
| SampleList $sample, | ||
| ): void { | ||
| if ($sample->items !== []) { | ||
| echo $sample->items[count($sample->items) - 1]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class StaticProps | ||
| { | ||
| /** @var array<string, string> */ | ||
| public static array $fields = []; | ||
|
|
||
| /** @var list<string> */ | ||
| public static array $items = []; | ||
|
|
||
| public function arrayKeyFirstStatic(): void | ||
| { | ||
| if (self::$fields !== []) { | ||
| echo self::$fields[array_key_first(self::$fields)]; | ||
| } | ||
| } | ||
|
|
||
| public function arrayKeyLastStatic(): void | ||
| { | ||
| if (self::$fields !== []) { | ||
| echo self::$fields[array_key_last(self::$fields)]; | ||
| } | ||
| } | ||
|
|
||
| public function arrayRandStatic(): void | ||
| { | ||
| if (self::$fields !== []) { | ||
| echo self::$fields[array_rand(self::$fields)]; | ||
| } | ||
| } | ||
|
|
||
| public function countMinus1Static(): void | ||
| { | ||
| if (self::$items !== []) { | ||
| echo self::$items[count(self::$items) - 1]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function doWithMethods(WithMethods $withMethods) { | ||
| echo $withMethods->pureMethod()[array_key_first($withMethods->pureMethod())]; | ||
| echo $withMethods->impureMethod()[array_key_first($withMethods->impureMethod())]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should expect an error because So you have to find a better fix.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The background |
||
| echo $withMethods->pureMethod($withMethods->impureMethod())[array_key_first($withMethods->pureMethod($withMethods->impureMethod()))]; | ||
| } | ||
|
|
||
| class WithMethods { | ||
| /** | ||
| * @phpstan-pure | ||
| * @return non-empty-array | ||
| */ | ||
| public function pureMethod(mixed ...$args):array {} | ||
| /** | ||
| * @phpstan-impure | ||
| * @return non-empty-array | ||
| */ | ||
| public function impureMethod():array {} | ||
| } | ||
|
|
||
| class NestedArray | ||
| { | ||
| /** | ||
| * @param array<string, non-empty-array<string, string>> $nested | ||
| */ | ||
| public function dimFetchDeterministic(array $nested, string $key): void | ||
| { | ||
| if (isset($nested[$key])) { | ||
| echo $nested[$key][array_key_first($nested[$key])]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-array<string, non-empty-array<string, string>> $nested | ||
| */ | ||
| public function dimFetchWithMethodKey(WithMethods $w, array $nested): void | ||
| { | ||
| echo $nested[$w->impureMethod()][array_key_first($nested[$w->impureMethod()])]; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.