Completed
Push — master ( c79d57...cdca2b )
by Bjørn
02:46
created

src/Helpers/MimeType.php (1 issue)

1
<?php
2
3
namespace WebPConvert\Helpers;
4
5
use ImageMimeTypeGuesser\ImageMimeTypeGuesser;
6
7
use WebPConvert\Exceptions\InvalidInputException;
8
use WebPConvert\Exceptions\InvalidInput\TargetNotFoundException;
9
10
/**
11
 * Get MimeType, results cached by path.
12
 *
13
 * @package    WebPConvert
14
 * @author     Bjørn Rosell <[email protected]>
15
 * @since      Class available since Release 2.0.6
16
 */
17
class MimeType
18
{
19
    private static $cachedDetections = [];
20
21
    /**
22
     * Get mime type for image (best guess).
23
     *
24
     * It falls back to using file extension. If that fails too, false is returned
25
     *
26
     * @return  string|false|null mimetype (if it is an image, and type could be determined / guessed),
27
     *    false (if it is not an image type that the server knowns about)
28
     *    or null (if nothing can be determined)
29
     */
30 35
    public static function getMimeTypeDetectionResult($absFilePath)
31
    {
32 35
        PathChecker::checkAbsolutePathAndExists($absFilePath);
33
34 35
        if (isset(self::$cachedDetections[$absFilePath])) {
35
            return self::$cachedDetections[$absFilePath];
36
        }
37 35
        $cachedDetections[$absFilePath] = ImageMimeTypeGuesser::lenientGuess($absFilePath);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$cachedDetections was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cachedDetections = array(); before regardless.
Loading history...
38 35
        return $cachedDetections[$absFilePath];
39
    }
40
}
41