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

Weird error on unset of array #3391

Closed
jackbentley opened this issue Jun 3, 2020 · 10 comments · Fixed by phpstan/phpstan-src#1596
Closed

Weird error on unset of array #3391

jackbentley opened this issue Jun 3, 2020 · 10 comments · Fixed by phpstan/phpstan-src#1596
Labels

Comments

@jackbentley
Copy link

jackbentley commented Jun 3, 2020

Bug report

I'm utilising the Symfony Normalizer in my project. I've only got PHPStan on level 2 but this error occurs when trying to unset a value returned in the serializer.

Cannot unset offset 'id' on array('bar' => 'b').

Code snippet that reproduces the problem

<?php declare(strict_types=1);

class HelloWorld
{
    /**
     * This method represents \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize().
     *
     * @return array|string|int|float|bool|null
     */
    public function getArray()
    {
        return ['id' => 1];
    }

    public function test()
    {
        $data = $this->getArray();

        $data['foo'] = 'a';
        $data['bar'] = 'b';

        unset($data['id']);

        return $data;
    }
}

Expected output

No errors.

Examples

This shows the error:
https://phpstan.org/r/d3f05294-9abc-4f48-ad77-c71d371b4cfd

However, when removing either of lines 8/20/21 the error goes away...
https://phpstan.org/r/fba7b0b0-735f-4ba5-8fc4-b33a94dcff04
or... https://phpstan.org/r/4b79df96-40b4-4720-b270-ac98455f80b3

@ondrejmirtes
Copy link
Member

This is happening because you're getting a union type array|string|int|float|bool|null but you're acting like you always have an array. Compare with: https://phpstan.org/r/f9633645-6866-48d6-9f7a-b4c4bbaeb4de

See: https://phpstan.org/writing-php-code/narrowing-types

I agree something should be done about this bug but you need to fix your code anyway :) Otherwise this issue would be reported on level 7: https://phpstan.org/r/f3e6a76b-5f55-4940-89fb-f7f0c4a2e4e7

@jackbentley
Copy link
Author

Thanks for the response 😄Agreed, the code isn't the best and needs fixing regardless really.

What I find most odd about this bug is that it only shows after altering the array the second time: https://phpstan.org/r/4b79df96-40b4-4720-b270-ac98455f80b3

@ondrejmirtes
Copy link
Member

This is what's happening (PHPStan internals): https://phpstan.org/r/e1bdb0e3-c668-418a-9827-37f855cd8302

@phpstan-bot
Copy link
Contributor

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

@@ @@
-22: Cannot unset offset 'id' on array('bar' => 'b').
+22: Cannot unset offset 'id' on array{bar: 'b'}.
Full report
Line Error
22 Cannot unset offset 'id' on array{bar: 'b'}.

@phpstan-bot
Copy link
Contributor

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

@@ @@
 22: Cannot access offset 'foo' on array|bool|float|int|string.
-25: Cannot unset offset 'id' on array('bar' => 'b').
+25: Cannot unset offset 'id' on array{bar: 'b'}.
Full report
Line Error
22 `Cannot access offset 'foo' on array
25 Cannot unset offset 'id' on array{bar: 'b'}.

@phpstan-bot
Copy link
Contributor

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

@@ @@
-22: Cannot unset offset 'id' on array('bar' => 'b').
+22: Cannot unset offset 'id' on array{bar: 'b'}.
Full report
Line Error
22 Cannot unset offset 'id' on array{bar: 'b'}.

@phpstan-bot
Copy link
Contributor

@jackbentley After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-22: Cannot unset offset 'id' on array('bar' => 'b').
+No errors

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-22: Cannot access offset 'foo' on array|bool|float|int|string.
-25: Cannot unset offset 'id' on array('bar' => 'b').
+22: Cannot access offset 'foo' on array|bool|float|int|string.
Full report
Line Error
22 `Cannot access offset 'foo' on array

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-22: Cannot unset offset 'id' on array('bar' => 'b').
+No errors

@github-actions
Copy link

github-actions bot commented Sep 7, 2022

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 Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants