Skip to content

Readonly property modification through clone() is not reported outside allowed scope #14063

@MartkCz

Description

@MartkCz

Feature request

Hi,
I found a case where PHPStan does not report any error, but PHP throws a fatal error at runtime.

The following code is accepted by PHPStan without any issues:

<?php declare(strict_types = 1);

final readonly class Obj
{
    public function __construct(public string $value) {}
}

$obj = new Obj('val');
$newObj = clone($obj, ['value' => 'newVal']);

PHPStan reports no error.

However, when running this code in PHP, it results in a fatal error:

Fatal error: Uncaught Error: Cannot modify protected(set) readonly property Obj::$value from global scope

https://phpstan.org/r/78bd8f63-aea6-4d1b-8e97-c3a96bb44572

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions