-
Notifications
You must be signed in to change notification settings - Fork 550
Report non existent offset on non empty array #4399
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
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
93c2f7f
91377ed
7df12e1
150b073
1059ad8
8431d18
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 |
|---|---|---|
|
|
@@ -2,14 +2,18 @@ | |
|
|
||
| namespace PHPStan\Type; | ||
|
|
||
| use PHPStan\Internal\CombinationsHelper; | ||
| use PHPStan\Type\Accessory\AccessoryType; | ||
| use PHPStan\Type\Accessory\HasPropertyType; | ||
| use PHPStan\Type\Constant\ConstantArrayType; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPStan\Type\Generic\TemplateBenevolentUnionType; | ||
| use PHPStan\Type\Generic\TemplateType; | ||
| use PHPStan\Type\Generic\TemplateUnionType; | ||
| use function array_filter; | ||
| use function array_map; | ||
| use function array_merge; | ||
| use function iterator_to_array; | ||
|
|
||
| /** | ||
| * @api | ||
|
|
@@ -133,17 +137,28 @@ public static function flattenTypes(Type $type): array | |
| return $type->getAllArrays(); | ||
| } | ||
|
|
||
| if ($type instanceof IntersectionType && $type->isConstantArray()->yes()) { | ||
|
Member
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. Wouldn't it be better to just do something with |
||
| $newTypes = []; | ||
| foreach ($type->getTypes() as $innerType) { | ||
| $newTypes[] = self::flattenTypes($innerType); | ||
| } | ||
|
|
||
| return array_filter( | ||
| array_map( | ||
| static fn (array $types): Type => TypeCombinator::intersect(...$types), | ||
| iterator_to_array(CombinationsHelper::combinations($newTypes)), | ||
| ), | ||
| static fn (Type $type): bool => !$type instanceof NeverType, | ||
| ); | ||
| } | ||
|
|
||
| if ($type instanceof UnionType) { | ||
| $types = []; | ||
| foreach ($type->getTypes() as $innerType) { | ||
| if ($innerType instanceof ConstantArrayType) { | ||
| foreach ($innerType->getAllArrays() as $array) { | ||
| $types[] = $array; | ||
| } | ||
| continue; | ||
| $flattenTypes = self::flattenTypes($innerType); | ||
| foreach ($flattenTypes as $flattenType) { | ||
| $types[] = $flattenType; | ||
| } | ||
|
|
||
| $types[] = $innerType; | ||
| } | ||
|
|
||
| return $types; | ||
|
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace Bug7143; | ||
|
|
||
| class Foo | ||
| { | ||
| /** | ||
| * @param array{foo?: string, bar?: string}&non-empty-array $arr | ||
| */ | ||
| public function test(array $arr): void | ||
| { | ||
| echo $arr['foo']; | ||
| echo $arr['bar']; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{foo?: string, bar?: string, 1?:1, 2?:2, 3?:3, 4?:4, 5?:5, 6?:6, 7?:7, 8?:8, 9?:9}&non-empty-array $arr | ||
| */ | ||
| public function test2(array $arr): void | ||
| { | ||
| echo $arr['foo']; | ||
| echo $arr['bar']; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$type instanceof IntersectionType && $type->isConstantArray()->maybe()doesn't seems to be a relevant case.