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

How to Supress PossiblyInvalidArgument for Request Objects? #7

Open
deleugpn opened this issue Mar 8, 2019 · 6 comments
Open

How to Supress PossiblyInvalidArgument for Request Objects? #7

deleugpn opened this issue Mar 8, 2019 · 6 comments
Labels
bug Something isn't working

Comments

@deleugpn
Copy link

deleugpn commented Mar 8, 2019

Laravel has Form Request classes that can look like the following:

<?php

namespace App\Modules\Accounts\Requests;

use App\Http\Requests\FormRequest;

class SaveAccountRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'string|max:128',
            'email' => 'required|string|email',
        ];
    }

    public function email(): string
    {
        return $this->input('email');
    }
}

The input() method has a return signature declared as * @return string|array|null. For that reason, Psalm end up giving PossiblyInvalidArgument [...] possibly different type array<array-key, mixed>|string|null provided.

I tried the following:

    <issueHandlers>
        <PossiblyInvalidArgument>
            <errorLevel type="suppress">
                <file name="*Request.php" />
            </errorLevel>
        </PossiblyInvalidArgument>
    </issueHandlers>

but that configuration leads to

/app # vendor/bin/psalm
Could not resolve config path to /app//*Request.php

What can I do to suppress this problem but only for Request objects or for any interaction with the input method of a Form Request class?

@muglug
Copy link
Member

muglug commented Mar 11, 2019

You'll want to use <file name="**/*Request.php" /> or <file name="**/**/*Request.php" /> as appropriate given the nesting of those files in your file system.

@muglug muglug added the bug Something isn't working label Mar 11, 2019
@muglug
Copy link
Member

muglug commented Mar 11, 2019

But really the plugin should be able to interpret the rules in a given class.

@deleugpn
Copy link
Author

That could get tricky as Laravel supports custom rules and require_if / require_with, etc. Maybe limit to support only the basic rules might be a sane option.

@caugner
Copy link
Contributor

caugner commented Jul 13, 2021

@deleugpn I'm wondering if you still have this issue?

If so, I would recommend checking if you could declare your request attributes as @propertys (that's how I do it):

<?php

namespace App\Modules\Accounts\Requests;

use App\Http\Requests\FormRequest;

/**
  * @property string|null $name
  * @property string $email
  */
class SaveAccountRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'string|max:128',
            'email' => 'required|string|email',
        ];
    }
}

PS: Make sure to enable usePhpDocPropertiesWithoutMagicCall="true" in your psalm.xml.dist (or psalm.xml).

@deleugpn
Copy link
Author

I gave up on using Psalm, sorry.

@caugner
Copy link
Contributor

caugner commented Jul 13, 2021

I'm sorry to hear that, but maybe you might want to give it another try in about one month?

These days, I have resolved a couple of annoying issues, and I'm committed to fixing any other issues you will have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants