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

Passing string to view() function generates warning that the type should be view-string|null #1463

Closed
robopzet opened this issue Nov 30, 2022 · 4 comments · Fixed by #1464
Closed

Comments

@robopzet
Copy link

  • Larastan Version: 2.2.7
  • --level used: 9
  • Pull request with failing test: -

Description

PHPStan generates this warning for the given code:

Parameter #1 $view of function view expects view-string|null, string given.

The definition of the Laravel view() function describes the first parameter as string|null. The warning says it should be of type view-string|null. I do not understand why passing a string is not valid here.

Laravel code where the issue was found

    /**
     * @param string $fil
     * @param array<string, mixed> $dta
     * @return View
     */
    private function vw (
        string  $fil,
        array   $dta
    ): View {
        return view(sprintf(Settings::VW_BAS_TPL, $fil), $dta);
    }

    // Definition of Settings::VW_BAS_TPL:
    public const VW_BAS_TPL = 'path/to/templates/%s';
@szepeviktor
Copy link
Collaborator

Hello @robopzet! 👋🏻
view-string is our specialty :)
https://github.com/nunomaduro/larastan/blob/master/docs/custom-types.md

Please suppress the error. It does not support generated view names.

@szepeviktor
Copy link
Collaborator

szepeviktor commented Nov 30, 2022

Or you may force the type.

        /** @var view-string $viewName */
        $viewName = sprintf(Settings::VW_BAS_TPL, $fil);

        return view($viewName, $dta);

@mad-briller
Copy link
Contributor

if this is merged you can use view()->exists($viewName) to check it is a view-string:
#1464

@robopzet
Copy link
Author

Thanks for the suggestions, and the quick PR!

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

Successfully merging a pull request may close this issue.

3 participants