Issues (150)

src/InputFormParam.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
20
/**
21
 * @psalm-import-type Query from Types
22
 * @psalm-import-type RequestQuery from Types
23
 */
24
final class InputFormParam implements ParamInterface
25
{
26
    /** @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...
27
    public function __construct(
28
        private readonly FileUploadFactoryInterface $factory,
29
        private readonly ReflectionParameter $parameter,
30
        private readonly array $inputFileAttributes = [],
31
    ) {
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     *
37
     * Returns form metadata instead of parsing query data
38
     *
39
     * @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...
40
     */
41
    #[Override]
42
    public function __invoke(string $varName, array $query, InjectorInterface $injector): AbstractFileUpload|null
43
    {
44
        // When user sets the FileUpload object, return it directly
45
        if (array_key_exists($varName, $query)) {
46
            $value = $query[$varName];
47
            // Type check: ensure the value is a valid file upload object or null
48
            if ($value !== null && ! ($value instanceof FileUpload) && ! ($value instanceof ErrorFileUpload)) {
49
                throw new InvalidArgumentException($varName);
50
            }
51
52
            return $value;
53
        }
54
55
        unset($injector);
56
        $inputFileAttribute = $this->inputFileAttributes[0] ?? null;
57
58
        return $this->factory->create($this->parameter, $query, $inputFileAttribute);
59
    }
60
}
61