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

Offset 'extension' does not exist on array when using #3700

Closed
chrisrollins65 opened this issue Aug 3, 2020 · 7 comments
Closed

Offset 'extension' does not exist on array when using #3700

chrisrollins65 opened this issue Aug 3, 2020 · 7 comments
Labels
Milestone

Comments

@chrisrollins65
Copy link

chrisrollins65 commented Aug 3, 2020

Bug report

I have the following lines of code:

$pathInfo = pathinfo($filePath);
$extension = empty($pathInfo['extension']) ? '' : '.' . $pathInfo['extension'];

This results in the error PHPStan error:

Offset 'extension' does not exist on array('dirname' => string, 'basename' => string, 'filename' => string, ?'extension' => string).

I understand that the extension may not exist, but since this code explicitly checks that it does exist, I don't understand why this error is still appearing in PHPStan.
Is this a problem with PHPStan or do I need to change something else in my code?

Code snippet that reproduces the problem

https://phpstan.org/r/cf79be4b-a83a-4a68-b8ff-8fdf8e8ed49f

@mergeable
Copy link

mergeable bot commented Aug 3, 2020

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Aug 3, 2020
@amq
Copy link

amq commented Aug 13, 2020

@chrisrollins65 this should work:

$extension = array_key_exists('extension', $pathInfo) ? '.' . $pathInfo['extension'] : '';

@chrisrollins65
Copy link
Author

chrisrollins65 commented Aug 16, 2020

@amq yes, but empty should work as well. I can use array_key_exists to get around this issue in this case, but it is still an issue nonetheless. empty also has the advantage of ensuring that this array element has a value, which array_key_exists and isset do not do.

@Progi1984
Copy link

@phpstan-bot
Copy link
Contributor

@chrisrollins65 After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-9: Offset 'extension' does not exist on array('dirname' => string, 'basename' => string, 'filename' => string, ?'extension' => string).
+No errors

@phpstan-bot
Copy link
Contributor

@Progi1984 After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-28: Offset 'id_customer' does not exist on array()|array(?'id_country' => int, ?'id_customer' => int).
+No errors

@github-actions
Copy link

github-actions bot commented Aug 5, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants