PHP

PHP

Made by DeepSource

Invalid assignment PHP-W1032

Bug risk
Critical

The assignment is not valid and would raise an error during the runtime.

This issue is raised for the following cases:

  • Use of the null-safe operator ?-> on right side of assignment by reference

The null-safe operator cannot be used with references.

$country = &$customer->getAddress()?->getCountry();

This would result in a fatal error.

  • Use of the null-safe operator ?-> on the left side of assignment

The null-safe operator is read-only. It cannot write/assign values from it.

class Customer
{
    private ?Address $address;

    public function getAddress(): ?Address
    {
        return $this->address;
    }
}

class Address
{
    public string $country;
}

$customer->getAddress()?->country = 'NL';
  • When the expression on left side of assignment is not assignable
function doStuff(\stdClass $s)
{
    $s->wave() = 'test'; // Can't use method return value in write context
}

References