Issues (150)

src/InputFormsParam.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use InvalidArgumentException;
8
use Koriym\FileUpload\AbstractFileUpload;
9
use Koriym\FileUpload\ErrorFileUpload;
10
use Koriym\FileUpload\FileUpload;
11
use Override;
12
use Ray\Di\InjectorInterface;
13
use Ray\InputQuery\Attribute\InputFile;
14
use Ray\InputQuery\FileUploadFactoryInterface;
15
use ReflectionAttribute;
16
use ReflectionParameter;
17
18
use function array_key_exists;
19
use function assert;
20
use function count;
21
use function is_array;
22
23
/**
24
 * @psalm-import-type Query from Types
25
 * @psalm-import-type RequestQuery from Types
26
 */
27
final class InputFormsParam implements ParamInterface
28
{
29
    /** @param array<ReflectionAttribute<InputFile>> $inputFileAttributes */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<ReflectionAttribute<InputFile>> at position 2 could not be parsed: Expected '>' at position 2, but found 'ReflectionAttribute'.
Loading history...
30
    public function __construct(
31
        private readonly FileUploadFactoryInterface $factory,
32
        private readonly ReflectionParameter $parameter,
33
        private readonly array $inputFileAttributes = [],
34
    ) {
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * Returns array of FileUpload objects using createMultiple()
41
     *
42
     * @param RequestQuery $query
0 ignored issues
show
The type BEAR\Resource\RequestQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     *
44
     * @return array<AbstractFileUpload>
45
     */
46
    #[Override]
47
    public function __invoke(string $varName, array $query, InjectorInterface $injector): array
48
    {
49
        if (array_key_exists($varName, $query)) {
50
            return $this->getUserFileUploads($varName, $query);
51
        }
52
53
        $inputFileAttribute = $this->inputFileAttributes[0] ?? null;
54
55
        return $this->factory->createMultiple($this->parameter, $query, $inputFileAttribute);
56
    }
57
58
    /**
59
     * @param RequestQuery $query
60
     *
61
     * @return array<AbstractFileUpload>
62
     *
63
     * @throws InvalidArgumentException When array contains invalid file objects.
64
     */
65
    private function getUserFileUploads(string $varName, array $query): array
66
    {
67
        $fileUploads = $query[$varName];
68
        if (! is_array($fileUploads)) {
69
            throw new InvalidArgumentException($varName); // Invalid type of array
70
        }
71
72
        /** @var array<FileUpload|ErrorFileUpload> $fileUploads */
73
74
        assert(count($fileUploads) > 0);
75
        foreach ($fileUploads as $fileUpload) {
76
            if (! ($fileUpload instanceof FileUpload) && ! ($fileUpload instanceof ErrorFileUpload)) { // @phpstan-ignore-line
77
                throw new InvalidArgumentException($varName); // Invalid item of array tye of FileUpload or ErrorFileUpload
78
            }
79
        }
80
81
        return $fileUploads;
82
    }
83
}
84