Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

Upload::checkMaxsize()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 13
c 5
b 1
f 1
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 9.8333
cc 4
nc 5
nop 3
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Interfaces\Ruleable;
9
use Enjoys\Forms\Rule\UploadCheck\UploadCheckInterface;
10
use Enjoys\Forms\Traits\Request;
11
use Psr\Http\Message\UploadedFileInterface;
12
use Webmozart\Assert\Assert;
13
14
class Upload implements RuleInterface
15
{
16
    use Request;
17
18
    public const REQUIRED = 'required';
19
    public const EXTENSIONS = 'extensions';
20
21
    private array $params;
22
23 29
    public function __construct(array $params)
24
    {
25 29
        $this->params = $params;
26
    }
27
28
    /**
29
     * @psalm-suppress PossiblyNullReference
30
     * @param Ruleable&Element $element
31
     * @return bool
32
     */
33 3
    public function validate(Ruleable $element): bool
34
    {
35
        /** @var UploadedFileInterface|false $value */
36 3
        $value = \getValueByIndexPath($element->getName(), $this->getRequest()->getFilesData()->toArray());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Enjoys\Forms\Interfaces\Ruleable. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Forms\Interfaces\Ruleable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $this->getRequest()->getFilesData()->toArray());
Loading history...
Bug introduced by
The method toArray() does not exist on Psr\Http\Message\UploadedFileInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        $value = \getValueByIndexPath($element->getName(), $this->getRequest()->getFilesData()->/** @scrutinizer ignore-call */ toArray());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38 3
        if (false === $this->check($value, $element)) {
39 1
            return false;
40
        }
41 2
        return true;
42
    }
43
44 17
    private function check(UploadedFileInterface|false $value, Ruleable $element): bool
45
    {
46
        /** @var array|scalar $options */
47 17
        foreach ($this->params as $rule => $options) {
48 16
            if (is_int($rule) && is_string($options)) {
49 10
                $rule = $options;
50 10
                $options = [];
51
            }
52
53
54
            /** @var string $rule */
55 16
            $className = sprintf('\Enjoys\Forms\Rule\UploadCheck\%sCheck', ucfirst($rule));
56
57
            /** @var class-string<UploadCheckInterface> $className */
58 16
            Assert::classExists($className, sprintf('Unknown Check Upload: [%s]', $className));
59
60 15
            if ((new $className($value, $element, ...(array)$options))->check() === false) {
61 6
                return false;
62
            }
63
        }
64 10
        return true;
65
    }
66
}
67