Skip to content

Weird error on unset of array #3391

@jackbentley

Description

@jackbentley

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions