Skip to content

[8.4] Widening property hook's type for promoted property does not work #16638

@Wirone

Description

@Wirone

Description

The following code:

<?php

declare(strict_types=1);

class Foo
{
    public function __construct(
        public int $baz { set (int|string|stdClass $value) {
            if (is_int($value)) {
                $this->baz = $value;
            } else if ($value instanceof stdClass
                && property_exists($value, 'baz')
                && is_int($value->baz)
            ) {
                $this->baz = $value->baz;
            } else if (is_numeric($value)) {
                $this->baz = (int) $value;
            } else {
                throw new InvalidArgumentException('Invalid value');
            }
        } },
    ) {}
}

$obj = new stdClass();
$obj->baz = 123;

var_dump(new Foo($obj));

Resulted in this output:

Fatal error: Uncaught TypeError: Foo::__construct(): Argument #1 ($baz) must be of type int, stdClass given, called in /in/2mZY5 on line 29 and defined in /in/2mZY5:7
Stack trace:
#0 /in/2mZY5(29): Foo->__construct(Object(stdClass))
#1 {main}
  thrown in /in/2mZY5 on line 7

But I expect this to work instead, just like when property is not promoted.

Originally encountered when preparing Twitter thread.

PHP Version

8.4 (current master)

Operating System

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