Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
$offsetType = $offsetType->toArrayKey();
if ($offsetType instanceof ErrorType) {
return TrinaryLogic::createNo();
}

if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
Expand Down
4 changes: 4 additions & 0 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic

private function recursiveHasOffsetValueType(Type $offsetType): TrinaryLogic
{
if ($offsetType instanceof ErrorType) {
return TrinaryLogic::createNo();
}

if ($offsetType instanceof UnionType) {
$results = [];
foreach ($offsetType->getTypes() as $innerType) {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function toArray(): Type

public function toArrayKey(): Type
{
return $this->toString();
return new ErrorType();
}

public function toCoercedArgumentType(bool $strictTypes): Type
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Traits/ObjectTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPStan\Type\BooleanType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

Expand Down Expand Up @@ -271,7 +270,7 @@ public function toArray(): Type

public function toArrayKey(): Type
{
return new StringType();
return new ErrorType();
}

public function toCoercedArgumentType(bool $strictTypes): Type
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ public static function dataProperties(): array
'$this->resource',
],
[
'mixed',
'*ERROR*',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phpdoc use array[array]

'$this->yetAnotherAnotherMixedParameter',
],
[
Expand Down
12 changes: 11 additions & 1 deletion tests/PHPStan/Levels/data/stringOffsetAccess-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
"line": 16,
"ignorable": true
},
{
"message": "Offset int|object does not exist on array{baz: 21}|array{foo: 17, bar: 19}.",
"line": 55,
"ignorable": true
},
{
"message": "Invalid array key type stdClass.",
"line": 59,
"ignorable": true
},
{
"message": "Offset stdClass does not exist on array{baz: 21}|array{foo: 17, bar: 19}.",
"line": 59,
"ignorable": true
}
]
]
5 changes: 5 additions & 0 deletions tests/PHPStan/Levels/data/stringOffsetAccess-7.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"line": 31,
"ignorable": true
},
{
"message": "Offset int|object might not exist on array|string.",
"line": 35,
"ignorable": true
},
{
"message": "Possibly invalid array key type int|object.",
"line": 35,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ public function testStrings(): void
'Offset 12.34 might not exist on array|string.',
28,
],
[
'Offset int|object might not exist on array|string.',
32,
],
]);
}

Expand Down Expand Up @@ -921,6 +925,20 @@ public function testBug12593(): void
$this->analyse([__DIR__ . '/data/bug-12593.php'], []);
}

public function testBugObject(): void
{
$this->analyse([__DIR__ . '/data/bug-object.php'], [
[
'Offset int|object does not exist on array{baz: 21}|array{foo: 17, bar: 19}.',
12,
],
[
'Offset object does not exist on array<string, int>.',
21,
],
]);
}

public function testBug3747(): void
{
$this->analyse([__DIR__ . '/data/bug-3747.php'], []);
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace BugObject;

class B {
/**
* @param array{baz: 21}|array{foo: 17, bar: 19} $array
* @param int|object $key
*/
public function foo($array, $key)
{
$array[$key];
}

/**
* @param array<string, int> $array
* @param object $key
*/
public function foo2($array, $key)
{
$array[$key];
}
}
Loading