Skip to content

InlineConstructorDefaultToPropertyRector doesn't check if the variable is being used (in a setter) before the default value is changed. #9680

@rensPols

Description

@rensPols

Bug Report

Subject Details
Rector version v2.3.8

I have a $isConstructed variable with a default value of false, and at the end of __construct() is set to true. Before it is set to true, I use it in the setter of another variable $value. However, the InlineConstructorDefaultToPropertyRector rule doesn't recognise its use and removes the assignment from the constructor and makes the default value true.

Minimal PHP Code Causing Issue

Minimal PHP Code for triggering this behaviour (https://getrector.com/demo/347b80b7-a224-43ef-b8b8-2de751f910f3)

<?php

final class DemoFile
{
    private bool $isConstructed = false; // This is changed to true
    
    public mixed $value = null {
    	get {
        	return $this->value;
        }
        set {
        	if (!$this->isConstructed) {
            	$this->value = $value;
            }
            if (!is_string($value)) {
            	throw TypeError('$value must be a string when it is already constructed!');
            }
            $this->value = $value;
        }
    }
    
    public function __construct(
    	mixed $value = null,
    ) {
        $this->value = $value;
    	$this->isConstructed = true; // This is removed
    }
}

Expected Behaviour

Should check if the variable is being used (in a setter).

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