Skip to content

Incorrect behavior of ReadOnlyPropertyRector #8095

@ChrisB9

Description

@ChrisB9

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/05da0364-f380-433e-b61c-06cd305d5f30

Non-final version:
https://getrector.com/demo/b6044ca3-5d01-4c37-a8ec-7e2f441282b4

<?php

final class DemoFile
{
    private string $method;
    
    public static function create(int $a): static
    {
        $self = new static();
        $self->method = 'hi';
        $self->method = 'hello';
        return $self;
    }
}

Responsible rules

  • ReadOnlyPropertyRector

Expected Behavior

https://3v4l.org/hv7TE#v8.2.7

This does not work. The rule should not transform the property to readonly, as it is being overwritten immediately.
The rule works properly if new self() instead of new static() is used.

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