GuessFromExtension::guess()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 51
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 6
b 0
f 0
nc 10
nop 1
dl 0
loc 51
ccs 22
cts 22
cp 1
crap 10
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * ImageMimeTypeGuesser - Detect / guess mime type of an image
5
 *
6
 * @link https://github.com/rosell-dk/image-mime-type-guesser
7
 * @license MIT
8
 */
9
10
namespace ImageMimeTypeGuesser;
11
12
class GuessFromExtension
13
{
14
15
16
    /**
17
     *  Make a wild guess based on file extension.
18
     *
19
     *  - and I mean wild!
20
     *
21
     *  Only most popular image types are recognized.
22
     *  Many are not. See this list: https://www.iana.org/assignments/media-types/media-types.xhtml
23
     *                - and the constants here: https://secure.php.net/manual/en/function.exif-imagetype.php
24
     *
25
     *  If no mapping found, nothing is returned
26
     *
27
     *  TODO: jp2, jpx, ...
28
     * Returns:
29
     * - mimetype (if file extension could be mapped to an image type),
30
     * - false (if file extension could be mapped to a type known not to be an image type)
31
     * - null (if file extension could not be mapped to any mime type, using our little list)
32
     *
33
     * @param  string  $filePath  The path to the file
34
     * @return string|false|null  mimetype (if file extension could be mapped to an image type),
35
     *    false (if file extension could be mapped to a type known not to be an image type)
36
     *    or null (if file extension could not be mapped to any mime type, using our little list)
37
     */
38 1
    public static function guess($filePath)
39
    {
40 1
        if (!@file_exists($filePath)) {
41 1
            return false;
42
        }
43
        /*
44
        Not using pathinfo, as it is locale aware, and I'm not sure if that could lead to problems
45
46
        if (!function_exists('pathinfo')) {
47
            // This is really a just in case! - We do not expect this to happen.
48
            // - in fact we have a test case asserting that this does not happen.
49
            return null;
50
            //
51
            $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
52
            $fileExtension = strtolower($fileExtension);
53
        }*/
54
55 1
        $result = preg_match('#\\.([^.]*)$#', $filePath, $matches);
56 1
        if ($result !== 1) {
57 1
            return null;
58
        }
59 1
        $fileExtension = $matches[1];
60
61
        // Trivial image mime types
62 1
        if (in_array($fileExtension, ['apng', 'avif', 'bmp', 'gif', 'jpeg', 'png', 'tiff', 'webp'])) {
63 1
            return 'image/' . $fileExtension;
64
        }
65
66
        // Common extensions that are definitely not images
67 1
        if (in_array($fileExtension, ['txt', 'doc', 'zip', 'gz', 'exe'])) {
68 1
            return false;
69
        }
70
71
        // Non-trivial image mime types
72 1
        switch ($fileExtension) {
73 1
            case 'ico':
74 1
            case 'cur':
75 1
                return 'image/x-icon';      // or perhaps 'vnd.microsoft.icon' ?
76
77 1
            case 'jpg':
78 1
                return 'image/jpeg';
79
80 1
            case 'svg':
81 1
                return 'image/svg+xml';
82
83 1
            case 'tif':
84 1
                return 'image/tiff';
85
        }
86
87
        // We do not know this extension, return null
88 1
        return null;
89
    }
90
}
91