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

Cecilapp / Cecil / 5046064611

pending completion
5046064611

push

github

GitHub
perf: native_function_invocation (#1697)

322 of 322 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

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

68.29
/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

19
class File
20
{
21
    /** @var Filesystem */
22
    protected static $fs;
23

24
    /**
25
     * Returns a Symfony\Component\Filesystem instance.
26
     */
27
    public static function getFS(): Filesystem
28
    {
29
        if (!self::$fs instanceof Filesystem) {
1✔
30
            self::$fs = new Filesystem();
1✔
31
        }
32

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

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

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

53
        try {
54
            if ($userAgent) {
1✔
55
                $options = [
1✔
56
                    'http' => [
1✔
57
                        'method'          => 'GET',
1✔
58
                        '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',
1✔
59
                        'follow_location' => true,
1✔
60
                    ],
1✔
61
                ];
1✔
62

63
                return file_get_contents($filename, false, stream_context_create($options));
1✔
64
            }
65

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

74
    /**
75
     * Returns MIME content type and subtype of a file.
76
     *
77
     * ie: ['text', 'text/plain']
78
     */
79
    public static function getMimeType(string $filename): array
80
    {
81
        if (false === $subtype = mime_content_type($filename)) {
1✔
82
            throw new RuntimeException(sprintf('Can\'t get MIME content type of "%s"', $filename));
×
83
        }
84
        $type = explode('/', $subtype)[0];
1✔
85

86
        return [
1✔
87
            $type,
1✔
88
            $subtype,
1✔
89
        ];
1✔
90
    }
91

92
    /**
93
     * exif_read_data() function with error handler.
94
     */
95
    public static function readExif(string $filename): array
96
    {
97
        if (empty($filename)) {
×
98
            return [];
×
99
        }
100

101
        set_error_handler(
×
102
            function ($severity, $message, $file, $line) {
×
103
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
×
104
            }
×
105
        );
×
106

107
        try {
108
            return exif_read_data($filename, null, true);
×
109
        } catch (\ErrorException $e) {
×
110
            return [];
×
111
        } finally {
112
            restore_error_handler();
×
113
        }
114
    }
115
}
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