MimeContentType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 39
ccs 9
cts 9
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A doDetect() 0 23 5
1
<?php
2
3
namespace ImageMimeTypeGuesser\Detectors;
4
5
class MimeContentType extends AbstractDetector
6
{
7
8
    /**
9
     * Try to detect mime type of image using *mime_content_type()*.
10
     *
11
     * Returns:
12
     * - mime type (string) (if it is in fact an image, and type could be determined)
13
     * - false (if it is not an image type that the server knowns about)
14
     * - null  (if nothing can be determined)
15
     *
16
     * @param  string  $filePath  The path to the file
17
     * @return string|false|null  mimetype (if it is an image, and type could be determined),
18
     *    false (if it is not an image type that the server knowns about)
19
     *    or null (if nothing can be determined)
20
     */
21 3
    protected function doDetect($filePath)
22
    {
23
        // mime_content_type supposedly used to be deprecated, but it seems it isn't anymore
24
        // it may return false on failure.
25 3
        if (function_exists('mime_content_type')) {
26
            try {
27 2
                $result = mime_content_type($filePath);
28 1
                if ($result !== false) {
29 1
                    if (strpos($result, 'image/') === 0) {
30 1
                        return $result;
31
                    } else {
32 1
                        return false;
33
                    }
34
                }
35 1
            } catch (\Exception $e) {
36
                // we are unstoppable!
37
38
                // TODO:
39
                // We should probably throw... - we will do in version 1.0.0
40
                //throw $e;
41
            }
42
        }
43 2
        return null;
44
    }
45
}
46