VS Code doesn't recognize @psalm-import-type InputSpecification

Hello,
It seems that VS Code with PHP tools does not detect the @psalm-import-type InputSpecification from InputFilterInterface in the Laminas\InputFilter\InputFilter (ver. 2.21.0), and throws a warning for “Unknown class”.
And probably because of this it does not like the array that I provide to the inputfilter->add() and shows error:

Argument '1' passed to add() is expected to be of type
Laminas\\InputFilter\\InputFilterInterface|Laminas\\InputFilter\\InputInterface|Laminas\\InputFilter\\InputSpecification|Traversable,
array given
private function addInputFilter() 
{
    // Create main input filter
    $inputFilter = new InputFilter();        
    $this->setInputFilter($inputFilter);
            
    $inputFilter->add([
        'name'     => 'password',
        'required' => TRUE,
        'validators' => [
            [
                'name'    => 'StringLength',
                'options' => [
                    'min' => 5,
                    'max' => 64
                ]
            ],
        ],
    ]);
}

I think this is related to:

No problem in PHPStorm:

phpstorm-inputfilter-add

It looks like this is used in a form class. Right?
If so, please avoid this variant, because this isolates the input filter from your application. For example, custom filters and validators that use a factory or delegator cannot be used here.

Define the input filter via the implementation of the interface Laminas\InputFilter\InputFilterProviderInterface:

class ExampleForm extends Laminas\Form\Form implements Laminas\InputFilter\InputFilterProviderInterface
{
    public function init(): void
    {
        $this->add(
            [
                'name'       => 'password',
                'type'       => Laminas\Form\Element\Password::class,
                'options'    => [
                    'label' => 'Password',
                ],
                'attributes' => [
                    'required' => true,
                    'id'       => 'password',
                ],
            ]
        );

        // …
    }

    public function getInputFilterSpecification(): array
    {
        return [
            [
                'name'       => 'password',
                'required'   => true,
                'validators' => [
                    [
                        'name'    => Laminas\Validator\StringLength::class,
                        'options' => [
                            'min' => 5,
                            'max' => 64,
                        ],
                    ],
                ],
            ],
            // …
        ];
    }
}

Then fetch your form via the form element manager.

2 Likes

Hi, thank you for the great suggestion it does not complain for the type any more.

If I am not asking too much, could you please explain why it works?

If I check the InputFilterProviderInterface, the editor still shows warning for unknown type for InputFilterSpecification|CollectionSpecification, like it does for the inputFilter but now it accepts the array in my form

image

That wasn’t the goal of my answer with the interface. :stuck_out_tongue_winking_eye:
See my motivation above:

It’s just a side effect that VS Code doesn’t complain about the type anymore.
Therefore I think that VS Code simply does not support this syntax. In PHPStorm everything works. :smile:

Yes, I understand. thank you once again for your time and for the effort to show the better practice, I already implemented it.

As for the type I believe it’s because of the :array type here. I am putting it if somebody else needs it.

public function getInputFilterSpecification(): array

Psalm type importing looks useful, I guess that soon it would be implemented in more editors.