Passed
Push — master ( 2d60b9...33913d )
by Bjørn
01:46
created

src/Detectors/AbstractDetector.php (1 issue)

1
<?php
2
3
namespace ImageMimeTypeGuesser\Detectors;
4
5
abstract class AbstractDetector
6
{
7
    /**
8
     * Try to detect mime type of image
9
     *
10
     * Returns:
11
     * - mime type (string) (if it is in fact an image, and type could be determined)
12
     * - false (if it can be determined that this is not an image)
13
     * - void  (if nothing can be determined)
14
     *
15
     * @param  string  $filePath  The path to the file
16
     * @return string|void|false  mimetype (if it is an image, and type could be determined),
17
     *    false (if it can be determined that this is not an image)
18
     *    or void (if nothing can be determined)
19
     */
20
    abstract protected function doDetect($filePath);
21
22
    /**
23
     * Create an instance of this class
24
     *
25
     * @param  string  $filePath  The path to the file
26
     * @return \ImageMimeTypeGuesser\AbstractDetector
27
     */
28
    public static function createInstance()
29
    {
30
        return new static();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static() returns the type ImageMimeTypeGuesser\Detectors\AbstractDetector which is incompatible with the documented return type ImageMimeTypeGuesser\AbstractDetector.
Loading history...
31
    }
32
33
    /**
34
     * Detect mime type of file (for images only)
35
     *
36
     * Returns:
37
     * - void  (if it cannot be determined)
38
     * - false (if it can be determined that this is not an image)
39
     * - mime type (string) (if it is in fact an image, and type could be determined)
40
     *
41
     * @param  string  $filePath  The path to the file
42
     * @return string|void|false
43
     */
44
    public static function detect($filePath)
45
    {
46
        if (!@file_exists($filePath)) {
47
            return false;
48
        }
49
        return self::createInstance()->doDetect($filePath);
50
    }
51
}
52