MimeTypesTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 35
ccs 20
cts 20
cp 1
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A addMapInternal() 0 28 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Devanych\Mime;
6
7
use InvalidArgumentException;
8
9
use function is_string;
10
use function is_array;
11
use function sprintf;
12
use function gettype;
13
14
trait MimeTypesTrait
15
{
16
    /**
17
     * @param array<string, string[]> $map
18
     * @see MimeTypesInterface::addMap()
19
     * @psalm-suppress DocblockTypeContradiction
20
     */
21 36
    private function addMapInternal(array $map): void
22
    {
23 36
        foreach ($map as $mimeType => $extensions) {
24 34
            if (!is_string($mimeType)) {
25 6
                throw new InvalidArgumentException(sprintf(
26 6
                    'MIME type MUST be string, received `%s`',
27 6
                    gettype($mimeType)
28 6
                ));
29
            }
30
31 28
            if (!is_array($extensions)) {
32 12
                throw new InvalidArgumentException(sprintf(
33 12
                    'Extensions MUST be array, received `%s`',
34 12
                    gettype($extensions)
35 12
                ));
36
            }
37
38 16
            $this->extensions[$mimeType] = $extensions;
0 ignored issues
show
Bug Best Practice introduced by
The property extensions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
40 16
            foreach ($extensions as $extension) {
41 16
                if (!is_string($extension)) {
42 12
                    throw new InvalidArgumentException(sprintf(
43 12
                        'Extension MUST be string, received `%s`',
44 12
                        gettype($extension)
45 12
                    ));
46
                }
47
48 16
                $this->mimeTypes[$extension][] = $mimeType;
0 ignored issues
show
Bug Best Practice introduced by
The property mimeTypes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
            }
50
        }
51
    }
52
}
53