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
27 changes: 22 additions & 5 deletions src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function count;
Expand Down Expand Up @@ -60,6 +63,14 @@ public function check(
}

if ($type->hasOffsetValueType($dimType)->no()) {
if ($type->isArray()->yes()) {
$validArrayDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
if ($validArrayDimType instanceof NeverType) {
// Already reported by InvalidKeyInArrayDimFetchRule
return [];
}
}

return [
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(count($dimType->getConstantStrings()) > 0 ? VerbosityLevel::precise() : VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))
->identifier('offsetAccess.notFound')
Expand All @@ -76,28 +87,34 @@ public function check(
$flattenedTypes = TypeUtils::flattenTypes($type);
}

$validArrayDimType = $dimType instanceof MixedType
? $dimType
: TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);

foreach ($flattenedTypes as $innerType) {
$dimTypeToCheck = $innerType->isArray()->yes() ? $validArrayDimType : $dimType;

if (
$this->reportPossiblyNonexistentGeneralArrayOffset
&& $innerType->isArray()->yes()
&& !$innerType->isConstantArray()->yes()
&& !$innerType->hasOffsetValueType($dimType)->yes()
&& !$innerType->hasOffsetValueType($dimTypeToCheck)->yes()
) {
$report = true;
break;
}
if (
$this->reportPossiblyNonexistentConstantArrayOffset
&& $innerType->isConstantArray()->yes()
&& !$innerType->hasOffsetValueType($dimType)->yes()
&& !$innerType->hasOffsetValueType($dimTypeToCheck)->yes()
) {
$report = true;
break;
}
if ($dimType instanceof BenevolentUnionType) {
$flattenedInnerTypes = [$dimType];
if ($dimTypeToCheck instanceof BenevolentUnionType) {
$flattenedInnerTypes = [$dimTypeToCheck];
} else {
$flattenedInnerTypes = TypeUtils::flattenTypes($dimType);
$flattenedInnerTypes = TypeUtils::flattenTypes($dimTypeToCheck);
}
foreach ($flattenedInnerTypes as $innerDimType) {
if (
Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"message": "Invalid array key type DateTimeImmutable.",
"line": 17,
"ignorable": true
},
{
"message": "Offset DateTimeImmutable does not exist on array.",
"line": 17,
"ignorable": true
}
]
25 changes: 0 additions & 25 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-7.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,24 @@
[
{
"message": "Offset int|object might not exist on array.",
"line": 19,
"ignorable": true
},
{
"message": "Possibly invalid array key type int|object.",
"line": 19,
"ignorable": true
},
{
"message": "Offset object|null might not exist on array.",
"line": 20,
"ignorable": true
},
{
"message": "Possibly invalid array key type object|null.",
"line": 20,
"ignorable": true
},
{
"message": "Offset DateTimeImmutable might not exist on array|ArrayAccess.",
"line": 26,
"ignorable": true
},
{
"message": "Possibly invalid array key type DateTimeImmutable.",
"line": 26,
"ignorable": true
},
{
"message": "Offset int|object might not exist on array|ArrayAccess.",
"line": 28,
"ignorable": true
},
{
"message": "Possibly invalid array key type int|object.",
"line": 28,
"ignorable": true
},
{
"message": "Offset object|null might not exist on array|ArrayAccess.",
"line": 29,
"ignorable": true
},
{
"message": "Possibly invalid array key type object|null.",
"line": 29,
Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Levels/data/stringOffsetAccess-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,5 @@
"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
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,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>.',
21,
],
]);
}

Expand Down Expand Up @@ -1049,6 +1045,60 @@ public function testBug13538(): void
]);
}

public function testPR4385(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
$this->reportPossiblyNonexistentConstantArrayOffset = true;

$this->analyse([__DIR__ . '/data/pr-4385.php'], [
[
'Offset int might not exist on array<int>.',
24,
],
[
'Offset string might not exist on array<int>.',
25,
],
[
'Offset array<int>|int might not exist on array<int>.',
28,
],
[
'Offset array<int>|string might not exist on array<int>.',
29,
],
[
'Offset 0|array<int> might not exist on array<int>.',
30,
],
[
'Offset int might not exist on array{string}.',
33,
],
[
'Offset string might not exist on array{string}.',
34,
],
[
'Offset array<int>|int might not exist on array{string}.',
37,
],
[
'Offset array<int>|string might not exist on array{string}.',
38,
],
[
'Offset array<int>|int might not exist on array<int>|string.',
41,
],
]);
}

public function testPR4385Bis(): void
{
$this->analyse([__DIR__ . '/data/pr-4385-bis.php'], []);
}

public function testBug12805(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/pr-4385-bis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Pr4385Bis;

class HelloWorld
{
/** @param array<string, int> $a */
public function sayHello(array $a, mixed $m): void
{
echo $a[$m];
}
}
43 changes: 43 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/pr-4385.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Pr4385;

class Foo
{
/**
* @param array<int> $array
* @param int $int
* @param string $string
* @param object $object
* @param array{0: string} $constantArray
*
* @return void
*/
public function test($array, $int, $string, $object, $constantArray)
{
$arrayOrObject = rand(0, 1) ? $array : $object;
$arrayOrInt = rand(0, 1) ? $array : $int;
$arrayOrString = rand(0, 1) ? $array : $string;
$arrayOrZero = rand(0, 1) ? $array : 0;

$array[$array];
$array[$int];
$array[$string];
$array[$object]; // Reported by InvalidKeyInArrayDimFetchRule
$array[$arrayOrObject]; // Reported by InvalidKeyInArrayDimFetchRule
$array[$arrayOrInt];
$array[$arrayOrString];
$array[$arrayOrZero];

$constantArray[$array];
$constantArray[$int];
$constantArray[$string];
$constantArray[$object]; // Reported by InvalidKeyInArrayDimFetchRule
$constantArray[$arrayOrObject]; // Reported by InvalidKeyInArrayDimFetchRule
$constantArray[$arrayOrInt];
$constantArray[$arrayOrString];
$constantArray[$arrayOrZero]; // Reported by InvalidKeyInArrayDimFetchRule

$arrayOrString[$arrayOrInt];
}
}
Loading