Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\Rules\Arrays\AllowedArrayKeysTypes;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
Expand Down Expand Up @@ -267,10 +268,8 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
$offsetType = $offsetType->toArrayKey();
if ($offsetType instanceof ErrorType) {
return TrinaryLogic::createNo();
}
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
$offsetType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();

if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
Expand Down
8 changes: 3 additions & 5 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\PhpVersionStaticAccessor;
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\Rules\Arrays\AllowedArrayKeysTypes;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
Expand Down Expand Up @@ -581,17 +582,14 @@ public function findTypeAndMethodNames(): array

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
$offsetArrayKeyType = $offsetType->toArrayKey();
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();

return $this->recursiveHasOffsetValueType($offsetArrayKeyType);
}

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 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',
],
[
'*ERROR*',
'mixed',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change revert the one in #4272

'$this->yetAnotherAnotherMixedParameter',
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ public function testStrings(): void
'Offset 12.34 might not exist on array|string.',
28,
],
[
'Offset int|object might not exist on array|string.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error was added by #4272

It make sens to not have it since

  • int is a valid offset and produce no error
  • object is not a valid offset (and produce a php crash) and is already reported by the InvalidArrayOffsetRule.

32,
],
]);
}

Expand Down Expand Up @@ -932,10 +928,6 @@ public function testBugObject(): void
'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>.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same, already reported by InvalidArrayOffsetRule and this was added by #4272

21,
],
]);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ public function testBug10610(): void
$this->analyse([__DIR__ . '/data/bug-10610.php'], []);
}

public function testBugDoctrine(): void
{
$this->analyse([__DIR__ . '/data/bug-doctrine.php'], []);
}

#[RequiresPhp('>= 8.4')]
public function testBug12553(): void
{
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BugDoctrine;

class HelloWorld
{
public function sayHello(string|array $a, array $b): void
{
$b[$a] ?? 2;
}
}
Loading