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

Eval'd code with static return type wrongly overriden do not show correct message #9407

Closed
shavounet opened this issue Aug 23, 2022 · 1 comment

Comments

@shavounet
Copy link

Description

This might be only an issue in the error message since I didn't find any issue with a valid eval'd code.
It's quite minor, but I did loose some time trying to understand it :)

The following code:

<?php
class Request {
    public function duplicate(): static
    {
        return new Request;
    }
}

class TestGenerator {
    public static function generate(): void
    {
        $code = <<<PHP
class MockRequest extends Request {
    public function duplicate(): Request
    {
        return new MockRequest;
    }
}
PHP;
        eval($code);
    }
}

TestGenerator::generate();
$b = new MockRequest;
$b->duplicate();

Resulted in this output:

PHP Fatal error:  Declaration of MockRequest::duplicate(): Request must be compatible with Request::duplicate(): TestGenerator in var/test.php(20) : eval()'d code on line 2

But I expected this output instead:

PHP Fatal error:  Declaration of MockRequest::duplicate(): Request must be compatible with Request::duplicate(): static in var/test.php(20) : eval()'d code on line 2

With a correct eval'd code (static return type), I do not have issues.
With a bad not-eval'd code, I do have a correct error message.
I can reproduce it with self and parent return types.

PHP Version

PHP 8.1.9

Operating System

No response

@cmb69
Copy link
Member

cmb69 commented Aug 23, 2022

The problem is that zend_type_to_string_resolved() uses the called scope to resolve the static type, and obviously, that isn't correct here.

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

No branches or pull requests

4 participants
@iluuu1994 @cmb69 @shavounet and others