Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type of ternary conditional with anonymous classes is computed incorrectly #4823

Open
MatmaRex opened this issue Dec 11, 2023 · 0 comments
Open

Comments

@MatmaRex
Copy link

The following code:

<?php

class Foo {
    function test() { echo 'first'; }
}
class Foo2 extends Foo {
    function test() { echo 'second'; }
}

$a = rand() % 2 ?
    new class() extends Foo {} :
    new class() extends Foo2 {};

'@phan-debug-var $a';

$a->test();

(demo)

Results in the following errors being reported:

input:4: PhanUnreferencedPublicMethod Possibly zero references to public method \Foo::test()
input:10: PhanDebugAnnotation @phan-debug-var requested for variable $a - it has union type \anonymous_class_b2cc6026(real=object)
input:16: PhanUndeclaredClassMethod Call to method test from undeclared class \anonymous_class_b2cc6026

I would expect no errors, as $a is always a subclass of Foo.

The errors do not occur when using an if() statement instead of ternary, or when not using anonymous classes:

if ( rand() % 2 ) {
    $a = new class() extends Foo {};
} else {
    $a = new class() extends Foo2 {};
}

(demo)

$a = rand() % 2 ?
    new Foo() :
    new Foo2();

(demo)

(tested at https://phan.github.io/demo/ – Phan 5.4.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant