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

conditional types do not take into account default values #2639

Open
calebdw opened this issue Apr 15, 2024 · 4 comments
Open

conditional types do not take into account default values #2639

calebdw opened this issue Apr 15, 2024 · 4 comments
Labels

Comments

@calebdw
Copy link

calebdw commented Apr 15, 2024

Hello!

PHPStan supports conditional return types---it would be great if phpactor could understand this. Currently it looks like these are just typed as null

Thanks!

@dantleech
Copy link
Collaborator

dantleech commented Apr 16, 2024

PHpactor also supports conditional return types, have you got an example of a conditional type that doesn't work?

https://github.com/phpactor/phpactor/blob/master/lib/WorseReflection/Tests/Inference/type/conditional-type-nested.test

@calebdw
Copy link
Author

calebdw commented Apr 16, 2024

Interesting---I have this Laraval helper function that I was trying to add a condition return type to:

if (! function_exists('logger')) {
    /**
     * Log a debug message to the logs.
     *
     * @param  string|null  $message
     * @param  array  $context
     * @return ($message is null ? \Illuminate\Log\LogManager : null)
     */
    function logger($message = null, array $context = [])
    {
        if (is_null($message)) {
            return app('log');
        }

        app('log')->debug($message, $context);

        return null;
    }
}

But the variable appears to be null:

image

It does seem to be picking up the return type in the docs though:

image

@calebdw
Copy link
Author

calebdw commented Apr 16, 2024

Ah, this seems to work so I guess it's just not recognizing the absence as a null value:

image

@dantleech dantleech changed the title feature request: support conditional return types in phpdoc conditional types do not take into account default values Apr 16, 2024
@dantleech dantleech added the bug label Apr 16, 2024
@dantleech
Copy link
Collaborator

thanks, updated title and added the bug label

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

No branches or pull requests

2 participants