Skip to content

Fix false positives on non-existing-offset's #3868

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

Merged
merged 2 commits into from
Mar 11, 2025
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
45 changes: 45 additions & 0 deletions src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function count;
use function in_array;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -96,6 +98,49 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (
$node->dim instanceof Node\Expr\FuncCall
&& $node->dim->name instanceof Node\Name
&& in_array($node->dim->name->toLowerString(), ['array_key_first', 'array_key_last'], true)
&& count($node->dim->getArgs()) >= 1
) {
$arrayArg = $node->dim->getArgs()[0]->value;
$arrayType = $scope->getType($arrayArg);
if (
$arrayArg instanceof Node\Expr\Variable
&& $node->var instanceof Node\Expr\Variable
&& is_string($arrayArg->name)
&& $arrayArg->name === $node->var->name
&& $arrayType->isArray()->yes()
&& $arrayType->isIterableAtLeastOnce()->yes()
) {
return [];
}
}

if (
$node->dim instanceof Node\Expr\BinaryOp\Minus
&& $node->dim->left instanceof Node\Expr\FuncCall
&& $node->dim->left->name instanceof Node\Name
&& in_array($node->dim->left->name->toLowerString(), ['count', 'sizeof'], true)
&& count($node->dim->left->getArgs()) >= 1
&& $node->dim->right instanceof Node\Scalar\Int_
&& $node->dim->right->value === 1
) {
$arrayArg = $node->dim->left->getArgs()[0]->value;
$arrayType = $scope->getType($arrayArg);
if (
$arrayArg instanceof Node\Expr\Variable
&& $node->var instanceof Node\Expr\Variable
&& is_string($arrayArg->name)
&& $arrayArg->name === $node->var->name
&& $arrayType->isList()->yes()
&& $arrayType->isIterableAtLeastOnce()->yes()
) {
return [];
}
}

return $this->nonexistentOffsetInArrayDimFetchCheck->check(
$scope,
$node->var,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,26 @@ public function testArrayDimFetchAfterArraySearch(): void
]);
}

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

$this->analyse([__DIR__ . '/data/array-dim-fetch-on-array-key-first-last.php'], [
[
'Offset 0|null might not exist on list<string>.',
12,
],
[
'Offset (int|string) might not exist on non-empty-list<string>.',
16,
],
[
'Offset int<-1, max> might not exist on non-empty-list<string>.',
45,
],
]);
}

public function testBug8649(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace ArrayDimFetchOnArrayKeyFirstOrLast;

class Hello {
/**
* @param list<string> $hellos
*/
public function first(array $hellos, array $anotherArray): string
{
if (rand(0,1)) {
return $hellos[array_key_first($hellos)];
}
if ($hellos !== []) {
if ($anotherArray !== []) {
return $hellos[array_key_first($anotherArray)];
}

return $hellos[array_key_first($hellos)];
}
return '';
}

/**
* @param array<string> $hellos
*/
public function last(array $hellos): string
{
if ($hellos !== []) {
return $hellos[array_key_last($hellos)];
}
return '';
}

/**
* @param list<string> $hellos
*/
public function countOnArray(array $hellos, array $anotherArray): string
{
if ($hellos === []) {
return 'nothing';
}

if (rand(0,1)) {
return $hellos[count($anotherArray) - 1];
}

return $hellos[count($hellos) - 1];
}
}
Loading