MimeTypesTrait::addMapInternal()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 28
ccs 20
cts 20
cp 1
crap 6
rs 9.1111
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