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

**URL:** <https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090>\
**Category:** Components & MVC\
**Tags:** laminas-inputfilter\
**Created:** [October 26, 2022, 9:35am UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090 "2022-10-26T09:35:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivel](https://avatars.discourse-cdn.com/v4/letter/i/9fc348/32.png) [@ivel](https://discourse.laminas.dev/u/ivel)\
**Post date:** [October 26, 2022, 9:35am UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/1 "2022-10-26T09:35:33Z")

</div>

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:

```auto
Argument '1' passed to add() is expected to be of type
Laminas\\InputFilter\\InputFilterInterface|Laminas\\InputFilter\\InputInterface|Laminas\\InputFilter\\InputSpecification|Traversable,
array given

```

```auto
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
                ]
            ],
        ],
    ]);
}

```

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 26, 2022, 9:46am UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/2 "2022-10-26T09:46:01Z")

</div>

I think this is related to:

> <https://github.com/laminas/laminas-inputfilter/issues/75>
>
> For example (though this may be the only case of this issue in this project, I h…aven’t checked in detail yet) in \[InputFilterProviderInterface.php:17\](https://github.com/laminas/laminas-inputfilter/blob/2.21.0/src/InputFilterProviderInterface.php#L17), the return type of \`InputFilterProviderInterface::getInputFilterSpecification()\` is specified as \`InputFilterSpecification|CollectionSpecification\` using the common \`@return\` annotation. These virtual types are in turn defined for Psalm in \[InputFilterInterface.php:38-47\](https://github.com/laminas/laminas-inputfilter/blob/2.21.0/src/InputFilterInterface.php#L38-L47) (using \`@psalm-import-type\` and \`@psalm-type\`). Any tool that does not use Psalm typehints may (should?) fail to understand the return type of the method. For example, PHPStan thinks the type is \`never\`, so from its point of view, it’s impossible to implement the method (in a \`Form\` or \`Fieldset\` subclass, for example).

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 26, 2022, 9:49am UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/3 "2022-10-26T09:49:33Z")

</div>

No problem in PHPStorm:

![phpstorm-inputfilter-add](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/0/00353a23d2d430278ea79c4359ef386592088943.png)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 26, 2022, 9:59am UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/4 "2022-10-26T09:59:16Z")

</div>

> [@ivel](#):
>
> ```auto
> 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
> ]
> ],
> ],
> ]);
> }
> 
> ```

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`:

```php
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.

---

<div class="post-metadata">

**Author:** ![ivel](https://avatars.discourse-cdn.com/v4/letter/i/9fc348/32.png) [@ivel](https://discourse.laminas.dev/u/ivel)\
**Post date:** [October 26, 2022, 11:29am UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/5 "2022-10-26T11:29:00Z")

</div>

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](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/b/b7e89ec797ea83bc0587e37df5174ba68d7dcf5c.png)

 ![image](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/2X/9/9b4b45303016b065010af7e23e3f83ec2f0b2345.png)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 26, 2022, 1:40pm UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/6 "2022-10-26T13:40:06Z")

</div>

> [@ivel](#):
>
> 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?

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

> [@froschdesign](#):
>
> …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.

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. 😄

---

<div class="post-metadata">

**Author:** ![ivel](https://avatars.discourse-cdn.com/v4/letter/i/9fc348/32.png) [@ivel](https://discourse.laminas.dev/u/ivel)\
**Post date:** [October 26, 2022, 6:51pm UTC](https://discourse.laminas.dev/t/vs-code-doesnt-recognize-psalm-import-type-inputspecification/3090/7 "2022-10-26T18:51:51Z")

</div>

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.
