ExtensionsCheck   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 2
b 0
f 0
dl 0
loc 36
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 17 4
A __construct() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Rule\UploadCheck;
6
7
use Enjoys\Forms\Interfaces\Ruleable;
8
use Psr\Http\Message\UploadedFileInterface;
9
10
final class ExtensionsCheck implements UploadCheckInterface
11
{
12
    private UploadedFileInterface|false $value;
13
    private Ruleable $element;
14
    private array $expectedExtensions;
15
    private ?string $message;
16
17 7
    public function __construct(
18
        false|UploadedFileInterface $value,
19
        Ruleable $element,
20
        string $expectedExtensions,
21
        ?string $message = null
22
    ) {
23 7
        $this->value = $value;
24 7
        $this->element = $element;
25 7
        $this->expectedExtensions = \array_map('trim', \explode(",", \strtolower($expectedExtensions)));
26 7
        $this->message = $message;
27
    }
28
29 7
    public function check(): bool
30
    {
31 7
        if ($this->value === false) {
32 1
            return true;
33
        }
34 6
        $extension = pathinfo($this->value->getClientFilename() ?? '', PATHINFO_EXTENSION);
35
36 6
        if (is_null($this->message)) {
37 4
            $this->message = 'Загрузка файлов с расширением .' . $extension . ' запрещена';
0 ignored issues
show
Bug introduced by
Are you sure $extension of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

37
            $this->message = 'Загрузка файлов с расширением .' . /** @scrutinizer ignore-type */ $extension . ' запрещена';
Loading history...
38
        }
39
40
41 6
        if (!in_array(\strtolower($extension), $this->expectedExtensions, true)) {
0 ignored issues
show
Bug introduced by
It seems like $extension can also be of type array; however, parameter $string of strtolower() does only seem to accept string, 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

41
        if (!in_array(\strtolower(/** @scrutinizer ignore-type */ $extension), $this->expectedExtensions, true)) {
Loading history...
42 2
            $this->element->setRuleError($this->message);
43 2
            return false;
44
        }
45 4
        return true;
46
    }
47
}
48