Skip to content
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

Phpstan ignores possibly undefined array offsets #7553

Closed
ptomulik opened this issue Jun 29, 2022 · 11 comments · Fixed by phpstan/phpstan-src#3028
Closed

Phpstan ignores possibly undefined array offsets #7553

ptomulik opened this issue Jun 29, 2022 · 11 comments · Fixed by phpstan/phpstan-src#3028

Comments

@ptomulik
Copy link

Bug report

The following code should raise an error, but it passes with level 9 + strict rules.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/**
 * @param array<mixed> $array
 * @return mixed
 */
function foo(array $array) {
	return $array['foo'];
}

https://phpstan.org/r/5347b67e-f0ad-4505-aeca-cfb074dcb3e7

Expected output

It should raise an error about possibly undefined array offset, same way as psalm does. Quite obvious.

https://psalm.dev/r/e3d927af68

@ondrejmirtes
Copy link
Member

There's some extra code that makes sure this IS NOT reported: https://github.com/phpstan/phpstan-src/blob/4bfe42193d092be3bf004b03575ebeedbd957da6/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php#L49-L60

Even in Psalm it's an extra couple of options off by default: https://psalm.dev/docs/running_psalm/configuration/#ensurearraystringoffsetsexist

I'd accept a patch that adds an extra option for this behaviour.

@herndlm
Copy link
Contributor

herndlm commented Jun 30, 2022

There are also more Info's about this at #7450 (reply in thread)

@desmax
Copy link

desmax commented Jul 7, 2022

@ondrejmirtes may I ask why you chose not to report this?

@ondrejmirtes
Copy link
Member

@desmax It'd report too many annoying things in existing codebases, like thousands of errors that aren't actually bugs...

@ptomulik
Copy link
Author

ptomulik commented Sep 2, 2022

@desmax It'd report too many annoying things in existing codebases, like thousands of errors that aren't actually bugs...

Well, most of things reported by static analysis tools are not actual bugs, but "just" potential bugs.

@herndlm
Copy link
Contributor

herndlm commented Sep 2, 2022

It was mentioned that a PR that adds opt-in for this would be accepted. Shouldn't that be enough?

@ryazanov13
Copy link

It was mentioned that a PR that adds opt-in for this would be accepted. Shouldn't that be enough?

Hi, how soon can we expect the release of this feature?

@ondrejmirtes
Copy link
Member

When someone implements it 😊

@ondrejmirtes
Copy link
Member

Thanks to phpstan/phpstan-src#3028 in PHPStan 1.11 there will be two new options:

parameters:
    reportPossiblyNonexistentGeneralArrayOffset: true
    reportPossiblyNonexistentConstantArrayOffset: true

@ondrejmirtes
Copy link
Member

Docs update 074f438

@ryazanov13
Copy link

Thanks to phpstan/phpstan-src#3028 in PHPStan 1.11 there will be two new options:

parameters:
    reportPossiblyNonexistentGeneralArrayOffset: true
    reportPossiblyNonexistentConstantArrayOffset: true

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants