Passed
Pull Request — 1.x (#325)
by Akihito
02:19
created

InputFormsParam::getUserFileUploads()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 17
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use InvalidArgumentException;
8
use Koriym\FileUpload\ErrorFileUpload;
9
use Koriym\FileUpload\FileUpload;
10
use Override;
11
use Ray\Di\InjectorInterface;
12
use Ray\InputQuery\Attribute\InputFile;
13
use Ray\InputQuery\FileUploadFactoryInterface;
14
use ReflectionAttribute;
15
use ReflectionParameter;
16
17
use function array_key_exists;
18
use function assert;
19
use function count;
20
use function is_array;
21
22
/**
23
 * @psalm-import-type Query from Types
24
 * @psalm-import-type RequestQuery from Types
25
 */
26
final class InputFormsParam implements ParamInterface
27
{
28
    /** @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...
29
    public function __construct(
30
        private readonly FileUploadFactoryInterface $factory,
31
        private readonly ReflectionParameter $parameter,
32
        private readonly array $inputFileAttributes = [],
33
    ) {
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     *
39
     * Returns array of FileUpload objects using createMultiple()
40
     *
41
     * @param RequestQuery $query
0 ignored issues
show
Bug introduced by
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...
42
     *
43
     * @return array<FileUpload|ErrorFileUpload>
44
     */
45
    #[Override]
46
    public function __invoke(string $varName, array $query, InjectorInterface $injector): array
47
    {
48
        if (array_key_exists($varName, $query)) {
49
            return $this->getUserFileUploads($varName, $query);
50
        }
51
52
        $inputFileAttribute = $this->inputFileAttributes[0] ?? null;
53
54
        return $this->factory->createMultiple($this->parameter, $query, $inputFileAttribute);
55
    }
56
57
    /**
58
     * @param RequestQuery $query
59
     *
60
     * @return array<FileUpload|ErrorFileUpload>
61
     *
62
     * @throws InvalidArgumentException When array contains invalid file objects
63
     */
64
    private function getUserFileUploads(string $varName, array $query): array
65
    {
66
        $fileUploads = $query[$varName];
67
        if (! is_array($fileUploads)) {
68
            throw new InvalidArgumentException($varName); // Invalid type of array
69
        }
70
71
        /** @var array<FileUpload|ErrorFileUpload> $fileUploads */
72
73
        assert(count($fileUploads) > 0);
74
        foreach ($fileUploads as $fileUpload) {
75
            if (! ($fileUpload instanceof FileUpload) && ! ($fileUpload instanceof ErrorFileUpload)) { // @phpstan-ignore-line
76
                throw new InvalidArgumentException($varName); // Invalid item of array tye of FileUpload or ErrorFileUpload
77
            }
78
        }
79
80
        return $fileUploads;
81
    }
82
}
83