Skip to content

[PropertyInfo] In PHP 8.4, an expanded setter type is ignored when determining property type. #61163

@BusterNeece

Description

@BusterNeece

Symfony version(s) affected

7.3.1

Description

I'm in the process of migrating my codebase from using getter/setter methods to using property hooks as it greatly improves the conciseness of my code. In some cases, I have properties that are quite a bit more permissive in what they accept from setters than what their getters return. Historically, PropertyInfo extractors like ReflectionExtractor look for mutators first, and then return their types preferentially over those of getters or the property itself; this has the side effect of making denormalization target the (more permissive) setter's types.

Currently, ReflectionExtractor doesn't appear to look at the new ReflectionProperty::getSettableType type at all. This means that the same code written with property hooks instead of getters/setters yields different type results, which may affect downstream code in unexpected ways.

How to reproduce

Without property hooks:

<?php
class Foo {
  private string $bar;

  public function getBar(): string {
    return $this->bar;
  }

  public function setBar(string|null $value): void {
    $this->bar = $value ?? 'Some Default';
  }
}

$reflectionExtractor = new Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor();
var_dump($reflectionExtractor->getType(Foo::class, 'bar'));

Yields a NullableType accepting both string and null.

With property hooks:

<?php
class Foo {
  public string $bar {
    set (string|null $value) => $value ?? 'Some Default';
  }
}

$reflectionExtractor = new Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor();
var_dump($reflectionExtractor->getType(Foo::class, 'bar'));

Yields a BuiltinType of string with no nullable, as the setter hook is never checked.

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy