Skip to content

ClassPropertyAssignToConstructorPromotionRector promotes a property with a wider type #9809

Description

@vudaltsov

Bug Report

Subject Details
Rector version v2.5.1

ClassPropertyAssignToConstructorPromotionRector promotes a property whose type is wider than the constructor parameter's type.

In example below the property is ?object because it is nulled out later; the parameter is object because null is not a valid input. Promotion collapses both into the property's wider type, so new Holder(null) now type-checks.

Minimal PHP Code Causing Issue

https://getrector.com/demo/980360e4-321c-4a02-ac29-7062848188cf

Expected Behaviour

No changes.

Duplicate

I've seen a duplicate issue: #9564. I would like to point out that this is not a design problem. Constructor API has nothing to do with the property type.

There is a big difference between the state an object is created in and the state it may arrive at as its logic unfolds. In my case null means "gone", not "never was".

In other words, a property's type is the union of all states over the object's lifetime. A parameter's type is the set of valid inputs. These are different questions, and PHP lets us answer them separately. Collapsing a constructor parameter and a property into a promoted property is only valid when their types match exactly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions