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

Cecilapp / Cecil / 23440895806

23 Mar 2026 01:54PM UTC coverage: 82.477%. First build
23440895806

Pull #2333

github

web-flow
Merge 21184bf01 into e78ea37a0
Pull Request #2333: feat: add raw timings, diffs and save metrics

9 of 10 new or added lines in 2 files covered. (90.0%)

3323 of 4029 relevant lines covered (82.48%)

0.82 hits per line

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

91.78
/src/Util.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Cecil;
15

16
use Symfony\Component\Filesystem\Path;
17

18
/**
19
 * Utility class.
20
 *
21
 * Provides various utility methods for formatting class names, method names,
22
 * joining paths, converting memory sizes, and more.
23
 */
24
class Util
25
{
26
    /**
27
     * Formats a class name.
28
     *
29
     * ie: "Cecil\Step\OptimizeHtml" become "OptimizeHtml"
30
     *
31
     * @param object $class
32
     */
33
    public static function formatClassName($class, array $options = []): string
34
    {
35
        $lowercase = false;
1✔
36
        extract($options, EXTR_IF_EXISTS);
1✔
37

38
        $className = substr(strrchr(\get_class($class), '\\'), 1);
1✔
39
        if ($lowercase) {
1✔
40
            $className = strtolower($className);
1✔
41
        }
42

43
        return $className;
1✔
44
    }
45

46
    /**
47
     * Formats a method name.
48
     *
49
     * ie: "Cecil\Renderer\Extension\Core::asset()" become "asset()"
50
     *
51
     * @param string $method
52
     */
53
    public static function formatMethodName(string $method): string
54
    {
55
        $methodName = explode('::', $method)[1];
×
56

57
        return $methodName;
×
58
    }
59

60
    /**
61
     * Converts an array of strings into a path.
62
     */
63
    public static function joinPath(string ...$path): string
64
    {
65
        $path = array_filter($path, function ($path) {
1✔
66
            return !empty($path) && !\is_null($path);
1✔
67
        });
1✔
68
        array_walk($path, function (&$value, $key) {
1✔
69
            $value = str_replace('\\', '/', $value);
1✔
70
            $value = rtrim($value, '/');
1✔
71
            $value = $key == 0 ? $value : ltrim($value, '/');
1✔
72
        });
1✔
73

74
        return Path::canonicalize(implode('/', $path));
1✔
75
    }
76

77
    /**
78
     * Converts an array of strings into a system path.
79
     */
80
    public static function joinFile(string ...$path): string
81
    {
82
        array_walk($path, function (&$value, $key) use (&$path) {
1✔
83
            $value = str_replace(['\\', '/'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $value);
1✔
84
            $value = rtrim($value, DIRECTORY_SEPARATOR);
1✔
85
            $value = $key == 0 ? $value : ltrim($value, DIRECTORY_SEPARATOR);
1✔
86
            // unset entry with empty value
87
            if (empty($value)) {
1✔
88
                unset($path[$key]);
1✔
89
            }
90
        });
1✔
91

92
        return implode(DIRECTORY_SEPARATOR, $path);
1✔
93
    }
94

95
    /**
96
     * Converts memory size for human.
97
     */
98
    public static function convertMemory($size): string
99
    {
100
        if ($size === 0) {
1✔
101
            return '0';
×
102
        }
103
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
1✔
104

105
        return \sprintf('%s %s', round($size / pow(1024, $i = floor(log($size, 1024))), 2), $unit[$i]);
1✔
106
    }
107

108
    /**
109
     * Converts a duration (in seconds) to a human-readable string.
110
     */
111
    public static function convertDuration(float $duration): string
112
    {
113
        if ($duration < 1) {
1✔
114
            return \sprintf('%s ms', round($duration * 1000, 0));
1✔
115
        }
116

117
        return \sprintf('%s s', round($duration, 2));
1✔
118
    }
119

120
    /**
121
     * Converts microtime interval for human.
122
     */
123
    public static function convertMicrotime(float $start): string
124
    {
NEW
125
        return self::convertDuration(microtime(true) - $start);
×
126
    }
127

128
    /**
129
     * Loads class from the source directory, in the given subdirectory $dir.
130
     */
131
    public static function autoload(Builder $builder, string $dir): void
132
    {
133
        spl_autoload_register(function ($className) use ($builder, $dir) {
1✔
134
            $classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php");
1✔
135
            if (is_readable($classFile)) {
1✔
136
                require $classFile;
1✔
137
                return;
1✔
138
            }
139
            // in themes
140
            foreach ($builder->getConfig()->getTheme() ?? [] as $theme) {
1✔
141
                $classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php");
1✔
142
                if (is_readable($classFile)) {
1✔
143
                    require $classFile;
×
144
                    return;
×
145
                }
146
            }
147
        });
1✔
148
    }
149

150
    /**
151
     * Matches a URL against known embedded content patterns.
152
     * Supports YouTube, Vimeo, Dailymotion, and GitHub Gists.
153
     *
154
     * @param string $url The URL to check
155
     *
156
     * @return array|false An associative array with 'type' and 'url' keys if a match is found, or false otherwise
157
     */
158
    public static function matchesUrlPattern(string $url): array|false
159
    {
160
        $services = [
1✔
161
            'youtube' => [
1✔
162
                // https://regex101.com/r/gznM1j/1
163
                'pattern' => '(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})',
1✔
164
                'baseurl' => 'https://www.youtube-nocookie.com/embed/',
1✔
165
                'type' => 'video',
1✔
166
            ],
1✔
167
            'vimeo' => [
1✔
168
                // https://regex101.com/r/wCEFhd/1
169
                'pattern' => 'https:\/\/vimeo\.com\/([0-9]+)',
1✔
170
                'baseurl' => 'https://player.vimeo.com/video/',
1✔
171
                'type' => 'video',
1✔
172
            ],
1✔
173
            'dailymotion' => [
1✔
174
                // https://regex101.com/r/YKnLPm/1
175
                'pattern' => '(?:https?:\/\/)?(?:www\.)?dailymotion\.com\/video\/([a-z0-9]+)',
1✔
176
                'baseurl' => 'https://geo.dailymotion.com/player.html?video=',
1✔
177
                'type' => 'video',
1✔
178
            ],
1✔
179
            'github_gist' => [
1✔
180
                // https://regex101.com/r/y3bm2M/1
181
                'pattern' => 'https:\/\/gist\.github\.com\/([-a-zA-Z0-9_]+\/[-a-zA-Z0-9_]+)',
1✔
182
                'baseurl' => 'https://gist.github.com/',
1✔
183
                'type' => 'script',
1✔
184
            ],
1✔
185
        ];
1✔
186

187
        foreach ($services as $service) {
1✔
188
            if (preg_match('/' . $service['pattern'] . '/is', $url, $matches)) {
1✔
189
                return [
1✔
190
                    'type' => $service['type'],
1✔
191
                    'url' => $service['baseurl'] . $matches[1],
1✔
192
                ];
1✔
193
            }
194
        }
195

196
        return false;
1✔
197
    }
198
}
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