Mimes::check()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.7414

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 22
ccs 4
cts 12
cp 0.3333
crap 8.7414
rs 9.7998
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
use Dimtrovich\Validation\Traits\FileTrait;
15
use Rakit\Validation\Rule;
16
use Rakit\Validation\Rules\Mimes as RulesMimes;
17
18
class Mimes extends AbstractRule
19
{
20
    use FileTrait;
21
22
    protected array $parameters = [];
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function fillParameters(array $params): Rule
28
    {
29 4
        return $this->fillAllowedValuesParameters($params);
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function check($value): bool
36
    {
37 4
        $this->requireParameters(['allowed_values']);
38 4
        $this->setAllowedValues($this->parameters = $this->parameter('allowed_values'));
39
40
        if (! $this->isValidFileInstance($value)) {
41 4
            $rule = new RulesMimes();
42
            $rule->setAttribute($this->getAttribute());
0 ignored issues
show
Bug introduced by
It seems like $this->getAttribute() can also be of type null; however, parameter $attribute of Rakit\Validation\Rule::setAttribute() does only seem to accept Rakit\Validation\Attribute, maybe add an additional type check? ( Ignorable by Annotation )

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

42
            $rule->setAttribute(/** @scrutinizer ignore-type */ $this->getAttribute());
Loading history...
43
            $rule->setValidation($this->validation);
0 ignored issues
show
Bug introduced by
It seems like $this->validation can also be of type null; however, parameter $validation of Rakit\Validation\Rule::setValidation() does only seem to accept Rakit\Validation\Validation, maybe add an additional type check? ( Ignorable by Annotation )

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

43
            $rule->setValidation(/** @scrutinizer ignore-type */ $this->validation);
Loading history...
44
            $rule->setKey($this->getKey());
45
            $rule->setParameters($this->getParameters());
46
            $rule->setMessage($this->getMessage());
47
            $rule->allowTypes($this->parameters);
48
49
            return $rule->check($value);
50
        }
51
52
        if ($this->shouldBlockPhpUpload($value, $this->parameters)) {
53
            return false;
54
        }
55
56 4
        return $value->getPath() !== '' && in_array($value->guessExtension(), $this->parameters, true);
57
    }
58
}
59