Passed
Push — 5.x ( fdc2ab...368a22 )
by Enjoys
02:42
created

Upload::getSystemMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 3
c 2
b 0
f 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule;
6
7
use Enjoys\Forms\Element;
8
use Enjoys\Forms\Exception\ExceptionRule;
9
use Enjoys\Forms\Interfaces\Ruleable;
10
use Enjoys\Forms\Rule\UploadCheck\UploadCheckInterface;
11
use Enjoys\Forms\Rules;
12
use Psr\Http\Message\UploadedFileInterface;
13
use Webmozart\Assert\Assert;
14
15
class Upload extends Rules implements RuleInterface
16
{
17
18
    /**
19
     * @psalm-suppress PossiblyNullReference
20
     * @param Ruleable&Element $element
21
     * @return bool
22
     * @throws ExceptionRule
23
     */
24 2
    public function validate(Ruleable $element): bool
25
    {
26
        /** @var UploadedFileInterface|false $value */
27 2
        $value = \getValueByIndexPath($element->getName(), $this->getRequest()->getFilesData()->toArray());
0 ignored issues
show
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

27
        $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...
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

27
        $value = \getValueByIndexPath($element->/** @scrutinizer ignore-call */ getName(), $this->getRequest()->getFilesData()->toArray());
Loading history...
28
29 2
        if (false === $this->check($value, $element)) {
30 1
            return false;
31
        }
32 1
        return true;
33
    }
34
35 16
    private function check(UploadedFileInterface|false $value, Ruleable $element): bool
36
    {
37 16
        foreach ($this->getParams() as $rule => $options) {
38
39 15
            if (is_int($rule) && is_string($options)) {
40 9
                $rule = $options;
41 9
                $ruleOpts = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $ruleOpts is dead and can be removed.
Loading history...
42
            }
43
44
            /** @var class-string<UploadCheckInterface> $className */
45 15
            $className = sprintf('\Enjoys\Forms\Rule\UploadCheck\%sCheck', ucfirst($rule));
46 15
            Assert::classExists($className, sprintf('Unknown Check Upload: [%s]', $className));
47 14
            return (new $className($value, $element, $options))->check();
48
        }
49 1
        return true;
50
    }
51
}
52