MimeTypes::getMimeTypes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Devanych\Mime;
6
7
use function array_unique;
8
use function array_merge;
9
use function strtolower;
10
use function trim;
11
12
final class MimeTypes implements MimeTypesInterface, MimeTypesMapsInterface
13
{
14
    use MimeTypesTrait;
15
16
    /**
17
     * @var array<string, string[]>
18
     */
19
    private array $extensions = [];
20
21
    /**
22
     * @var array<string, string[]>
23
     */
24
    private array $mimeTypes = [];
25
26
    /**
27
     * @param array<string, string[]> $map
28
     */
29 24
    public function __construct(array $map = [])
30
    {
31 24
        $this->addMap($map);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 3
    public function getExtensions(string $mimeType): array
38
    {
39 3
        $lowerMime = strtolower(trim($mimeType));
40 3
        $extensions = self::EXTENSIONS[$lowerMime] ?? self::EXTENSIONS[$mimeType] ?? [];
41
42 3
        if ($this->extensions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->extensions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43 2
            $customExtensions = $this->extensions[$lowerMime] ?? $this->extensions[$mimeType] ?? [];
44 2
            $extensions = $customExtensions ? array_unique(array_merge($customExtensions, $extensions)) : $extensions;
45
        }
46
47 3
        return $extensions;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 3
    public function getMimeTypes(string $extension): array
54
    {
55 3
        $lowerExt = strtolower(trim($extension));
56 3
        $mimeTypes = self::MIME_TYPES[$lowerExt] ?? self::MIME_TYPES[$extension] ?? [];
57
58 3
        if ($this->mimeTypes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->mimeTypes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59 2
            $customMimeTypes = $this->mimeTypes[$lowerExt] ?? $this->mimeTypes[$extension] ?? [];
60 2
            $mimeTypes = $customMimeTypes ? array_unique(array_merge($customMimeTypes, $mimeTypes)) : $mimeTypes;
61
        }
62
63 3
        return $mimeTypes;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 24
    public function addMap(array $map): void
70
    {
71 24
        $this->addMapInternal($map);
72
    }
73
}
74