Passed
Push — configuration ( fedd0e...6e95de )
by Arnaud
09:38 queued 05:27
created

File   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 67
c 4
b 1
f 0
dl 0
loc 163
ccs 48
cts 57
cp 0.8421
rs 10
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isRemote() 0 3 1
A getFS() 0 7 2
A isRemoteExists() 0 10 3
A getMediaType() 0 13 2
A fileGetContents() 0 30 4
A readExif() 0 26 5
A getExtension() 0 11 2
A getRealPath() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Util;
15
16
use Cecil\Exception\RuntimeException;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Mime\MimeTypes;
19
20
class File
21
{
22
    /** @var Filesystem */
23
    protected static $fs;
24
25
    /**
26
     * Returns a Symfony\Component\Filesystem instance.
27 1
     */
28
    public static function getFS(): Filesystem
29 1
    {
30 1
        if (!self::$fs instanceof Filesystem) {
31
            self::$fs = new Filesystem();
32
        }
33 1
34
        return self::$fs;
35
    }
36
37
    /**
38
     * file_get_contents() function with error handler.
39
     *
40
     * @return string|false
41 1
     */
42
    public static function fileGetContents(string $filename, bool $userAgent = false)
43 1
    {
44
        if (empty($filename)) {
45
            return false;
46
        }
47 1
48 1
        set_error_handler(
49 1
            function ($severity, $message, $file, $line) {
50 1
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
51 1
            }
52
        );
53
54 1
        try {
55 1
            if ($userAgent) {
56 1
                $options = [
57 1
                    'http' => [
58 1
                        'method'          => 'GET',
59 1
                        'header'          => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36',
60 1
                        'follow_location' => true,
61 1
                    ],
62
                ];
63 1
64
                return file_get_contents($filename, false, stream_context_create($options));
65
            }
66 1
67 1
            return file_get_contents($filename);
68 1
        } catch (\ErrorException) {
69
            return false;
70 1
        } finally {
71
            restore_error_handler();
72
        }
73
    }
74
75
    /**
76
     * Returns the media type and subtype of a file.
77
     *
78
     * ie: ['text', 'text/plain']
79 1
     */
80
    public static function getMediaType(string $filename): array
81 1
    {
82
        try {
83
            $mimeTypes = new MimeTypes();
84 1
            $mimeType = $mimeTypes->guessMimeType($filename);
85
        } catch (\Exception $e) {
86 1
            throw new RuntimeException(\sprintf('Can\'t get media type of "%s" (%s).', $filename, $e->getMessage()));
87 1
        }
88 1
        $type = explode('/', $mimeType)[0];
0 ignored issues
show
Bug introduced by
It seems like $mimeType can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $type = explode('/', /** @scrutinizer ignore-type */ $mimeType)[0];
Loading history...
89 1
90
        return [
91
            $type,     // type
92
            $mimeType, // subtype
93
        ];
94
    }
95 1
96
    /**
97 1
     * Returns the extension of a file.
98
     */
99
    public static function getExtension(string $filename): string
100
    {
101 1
        try {
102 1
            $mimeTypes = new MimeTypes();
103
            $mimeType = $mimeTypes->guessMimeType($filename);
104 1
            $exts = $mimeTypes->getExtensions($mimeType);
0 ignored issues
show
Bug introduced by
It seems like $mimeType can also be of type null; however, parameter $mimeType of Symfony\Component\Mime\MimeTypes::getExtensions() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
            $exts = $mimeTypes->getExtensions(/** @scrutinizer ignore-type */ $mimeType);
Loading history...
105 1
        } catch (\Exception $e) {
106
            throw new RuntimeException(\sprintf('Can\'t get extension of "%s" (%s).', $filename, $e->getMessage()));
107
        }
108 1
109
        return $exts[0];
110
    }
111 1
112 1
    /**
113
     * exif_read_data() function with error handler.
114
     */
115
    public static function readExif(string $filename): array
116 1
    {
117
        if (empty($filename)) {
118
            return [];
119
        }
120 1
121
        set_error_handler(
122
            function ($severity, $message, $file, $line) {
123
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
124
            }
125
        );
126
127 1
        try {
128
            if (!\function_exists('exif_read_data')) {
129
                throw new \ErrorException('`exif` extension is not available.');
130 1
            }
131 1
            $exif = exif_read_data($filename, null, true);
132 1
            if ($exif === false) {
133
                return [];
134
            }
135 1
136
            return $exif;
137
        } catch (\ErrorException) {
138
            return [];
139 1
        } finally {
140
            restore_error_handler();
141
        }
142
    }
143
144
    /**
145
     * Returns the real path of a relative file path.
146
     */
147
    public static function getRealPath(string $path): string
148
    {
149
        // if file exists
150
        $filePath = realpath(\Cecil\Util::joinFile(__DIR__, '/../', $path));
151
        if ($filePath !== false) {
152
            return $filePath;
153
        }
154
        // if Phar
155
        if (Platform::isPhar()) {
156
            return \Cecil\Util::joinPath(Platform::getPharPath(), str_replace('../', '/', $path));
157
        }
158
159
        throw new RuntimeException(\sprintf('Can\'t get the real path of file "%s".', $path));
160
    }
161
162
    /**
163
     * Tests if a file path is remote.
164
     */
165
    public static function isRemote(string $path): bool
166
    {
167
        return (bool) preg_match('~^(?:f|ht)tps?://~i', $path);
168
    }
169
170
    /**
171
     * Tests if a remote file exists.
172
     */
173
    public static function isRemoteExists(string $path): bool
174
    {
175
        if (self::isRemote($path)) {
176
            $handle = @fopen($path, 'r');
177
            if (\is_resource($handle)) {
178
                return true;
179
            }
180
        }
181
182
        return false;
183
    }
184
}
185