• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 15137979703

20 May 2025 12:49PM UTC coverage: 83.18% (-0.02%) from 83.202%
15137979703

push

github

ArnaudLigny
feat: custom useragent to download assets

13 of 14 new or added lines in 2 files covered. (92.86%)

10 existing lines in 1 file now uncovered.

3071 of 3692 relevant lines covered (83.18%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.74
/src/Util/File.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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
     */
28
    public static function getFS(): Filesystem
29
    {
30
        if (!self::$fs instanceof Filesystem) {
1✔
31
            self::$fs = new Filesystem();
1✔
32
        }
33

34
        return self::$fs;
1✔
35
    }
36

37
    /**
38
     * file_get_contents() function with error handler.
39
     *
40
     * @return string|false
41
     */
42
    public static function fileGetContents(string $filename, ?string $userAgent = null)
43
    {
44
        if (empty($filename)) {
1✔
45
            return false;
×
46
        }
47

48
        set_error_handler(
1✔
49
            function ($severity, $message, $file, $line) {
1✔
50
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
1✔
51
            }
1✔
52
        );
1✔
53

54
        try {
55
            $options = [
1✔
56
                'http' => [
1✔
57
                    'method'          => 'GET',
1✔
58
                    'follow_location' => true,
1✔
59
                ],
1✔
60
            ];
1✔
61
            if (!empty($userAgent)) {
1✔
NEW
62
                $options['http']['header'] = "User-Agent: $userAgent";
×
63
            }
64

65
            return file_get_contents($filename, false, stream_context_create($options));
1✔
66
        } catch (\ErrorException) {
1✔
67
            return false;
1✔
68
        } finally {
69
            restore_error_handler();
1✔
70
        }
71
    }
72

73
    /**
74
     * Returns the media type and subtype of a file.
75
     *
76
     * ie: ['text', 'text/plain']
77
     */
78
    public static function getMediaType(string $filename): array
79
    {
80
        try {
81
            if (false !== $subtype = mime_content_type($filename)) {
1✔
82
                return [explode('/', $subtype)[0], $subtype];
1✔
83
            }
84
            $mimeTypes = new MimeTypes();
×
85
            $subtype = $mimeTypes->guessMimeType($filename);
×
86
            if ($subtype === null) {
×
87
                throw new RuntimeException('Can\'t guess the media type.');
×
88
            }
89

90
            return [explode('/', $subtype)[0], $subtype];
×
91
        } catch (\Exception $e) {
×
92
            throw new RuntimeException(\sprintf('Can\'t get media type of "%s" (%s).', $filename, $e->getMessage()));
×
93
        }
94
    }
95

96
    /**
97
     * Returns the extension of a file.
98
     */
99
    public static function getExtension(string $filename): string
100
    {
101
        try {
102
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
1✔
103
            if (!empty($ext)) {
1✔
104
                return $ext;
1✔
105
            }
106
            // guess the extension
107
            $mimeTypes = new MimeTypes();
1✔
108
            $mimeType = $mimeTypes->guessMimeType($filename);
1✔
109
            if ($mimeType === null) {
1✔
110
                throw new RuntimeException('Can\'t guess the media type.');
×
111
            }
112
            $exts = $mimeTypes->getExtensions($mimeType);
1✔
113

114
            return $exts[0];
1✔
115
        } catch (\Exception $e) {
×
116
            throw new RuntimeException(
×
117
                \sprintf('Can\'t get extension of "%s".', $filename),
×
118
                previous: $e,
×
119
            );
×
120
        }
121
    }
122

123
    /**
124
     * exif_read_data() function with error handler.
125
     */
126
    public static function readExif(string $filename): array
127
    {
128
        if (empty($filename)) {
1✔
129
            return [];
×
130
        }
131

132
        set_error_handler(
1✔
133
            function ($severity, $message, $file, $line) {
1✔
134
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
×
135
            }
1✔
136
        );
1✔
137

138
        try {
139
            if (!\function_exists('exif_read_data')) {
1✔
140
                throw new \ErrorException('`exif` extension is not available.');
×
141
            }
142
            $exif = exif_read_data($filename, null, true);
1✔
143
            if ($exif === false) {
1✔
144
                return [];
×
145
            }
146

147
            return $exif;
1✔
148
        } catch (\ErrorException) {
×
149
            return [];
×
150
        } finally {
151
            restore_error_handler();
1✔
152
        }
153
    }
154

155
    /**
156
     * Returns the real path of a relative file path.
157
     */
158
    public static function getRealPath(string $path): string
159
    {
160
        // if file exists
161
        $filePath = realpath(\Cecil\Util::joinFile(__DIR__, '/../', $path));
1✔
162
        if ($filePath !== false) {
1✔
163
            return $filePath;
1✔
164
        }
165
        // if Phar
166
        if (Platform::isPhar()) {
1✔
167
            return \Cecil\Util::joinPath(Platform::getPharPath(), str_replace('../', '/', $path));
×
168
        }
169

170
        throw new RuntimeException(\sprintf('Can\'t get the real path of file "%s".', $path));
1✔
171
    }
172

173
    /**
174
     * Tests if a file path is remote.
175
     */
176
    public static function isRemote(string $path): bool
177
    {
178
        return (bool) preg_match('~^(?:f|ht)tps?://~i', $path);
1✔
179
    }
180

181
    /**
182
     * Tests if a remote file exists.
183
     */
184
    public static function isRemoteExists(string $path): bool
185
    {
186
        if (self::isRemote($path)) {
1✔
187
            $handle = @fopen($path, 'r');
1✔
188
            if (!empty($http_response_header)) {
1✔
189
                if (400 < (int) explode(' ', $http_response_header[0])[1]) {
1✔
190
                    return false;
1✔
191
                }
192
            }
193
            if (\is_resource($handle)) {
1✔
194
                return true;
1✔
195
            }
196
        }
197

198
        return false;
×
199
    }
200
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc