laminas / laminas-inputfilter

Normalize and validate input sets from the web, APIs, the CLI, and more, including files
https://docs.laminas.dev/laminas-inputfilter/
BSD 3-Clause "New" or "Revised" License
41 stars 28 forks source link

InputFilter->add() no longer accepts arrays #93

Closed elstamey closed 1 year ago

elstamey commented 1 year ago

Bug Report

Q A
Version(s) ^2.16

Summary

We have our Classes that extend Laminas\InputFilter set up like this documentation:

Psalm shows InvalidArgument whereever we use $this->add() with an array of input specification in the way that is shown in documentation throughout the docs.

ERROR: InvalidArgument - module/MyAPI/src/MyAPI/V1/Rest/Reports/ReportsSearchFilter.php:48:13 - Argument 1 of MyAPI\V1\Rest\Reports\ReportsSearchFilter::add
expects Laminas\InputFilter\InputFilterInterface|Laminas\InputFilter\InputInterface|Traversable|type-alias(Laminas\InputFilter\InputFilterInterface::InputSpecification), array{filters: array{array{name: Laminas\Filter\StringToLower::class}}, name: "file_format", required: false, validators: array{array{name: Laminas\Validator\InArray::class, options: array{haystack: array{"csv", "xlsx", "pdf"}, strict: true}}}} provided

Current behavior

Psalm error throws InvalidArgument where we use InputFilter->add because an array is no longer an expected parameter.

How to reproduce

  1. With laminas-inputfilter installed with a minimum version of 2.16

  2. create a class that extends InputFilter and add an input specification that is setup as an array class ReportsSearchFilter extends InputFilter { public function init(): void { $this->add( [ 'name' => 'account_id', 'required' => false, 'validators' => [ ['name' => Digits::class,] ], ] ); } }

  3. run psalm on the created class ReportsSearchFilter. I can confirm the invalid argument error is thrown in version 4.7, but probably before this version.

  4. You should see an Invalid Argument error in the psalm output

Expected behavior

I expected that $this->add([]); would accept an array. as input since an array is used in so many documentation examples.

In a commit, It looks like a new type was added to the DocBlock. At the same time InputSpecification was added as an input parament, array was removed from it.
see this commit

At first I thought that removing array was intentional, but the above referenced commit has WIP for a title, and none of the documentation indicates that array is no longer accepted input.

elstamey commented 1 year ago

I can contribute a fix, but wanted to be sure my expectations matched the project before doing anything.

gsteel commented 1 year ago

Thanks for the report @elstamey - Your problem here is that you're on Psalm 4.x.

The Psalm type InputSpecification defined on InputFilterInterface is an unsealed array shape (Uses ...) - these are not supported in Psalm 4.x

Can I suggest that you baseline the issues until you've upgraded to 5.x?

As we're not going to downgrade to Psalm 4 here, I'll close this issue now.