FInfo::doDetect()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.8333
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