FInfo   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 38
ccs 11
cts 12
cp 0.9167
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A doDetect() 0 22 4
1
<?php
2
3
namespace ImageMimeTypeGuesser\Detectors;
4
5
class FInfo extends AbstractDetector
6
{
7
8
    /**
9
     * Try to detect mime type of image using *finfo* class.
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 6
    protected function doDetect($filePath)
22
    {
23
24 6
        if (class_exists('finfo')) {
25
            // phpcs:ignore PHPCompatibility.PHP.NewClasses.finfoFound
26 5
            $finfo = new \finfo(FILEINFO_MIME);
27 5
            $result = $finfo->file($filePath);
28 5
            if ($result === false) {
29
                // false means an error occured
30
                return null;
31
            } else {
32 5
                $mime = explode('; ', $result);
33 5
                $result = $mime[0];
34
35 5
                if (strpos($result, 'image/') === 0) {
36 5
                    return $result;
37
                } else {
38 3
                    return false;
39
                }
40
            }
41
        }
42 1
        return null;
43
    }
44
}
45