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

Cecilapp / Cecil / 15478712271

05 Jun 2025 10:39PM UTC coverage: 82.789%. Remained the same
15478712271

Pull #2184

github

web-flow
Merge 096c24b5d into e1877839a
Pull Request #2184: fix: server router

3117 of 3765 relevant lines covered (82.79%)

0.83 hits per line

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

88.64
/src/Util.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;
15

16
use Symfony\Component\Filesystem\Path;
17

18
class Util
19
{
20
    /**
21
     * Formats a class name.
22
     *
23
     * ie: "Cecil\Step\OptimizeHtml" become "OptimizeHtml"
24
     *
25
     * @param object $class
26
     */
27
    public static function formatClassName($class, array $options = []): string
28
    {
29
        $lowercase = false;
1✔
30
        extract($options, EXTR_IF_EXISTS);
1✔
31

32
        $className = substr(strrchr(\get_class($class), '\\'), 1);
1✔
33
        if ($lowercase) {
1✔
34
            $className = strtolower($className);
1✔
35
        }
36

37
        return $className;
1✔
38
    }
39

40
    /**
41
     * Formats a method name.
42
     *
43
     * ie: "Cecil\Renderer\Extension\Core::asset()" become "asset()"
44
     *
45
     * @param string $method
46
     */
47
    public static function formatMethodName(string $method): string
48
    {
49
        $methodName = explode('::', $method)[1];
×
50

51
        return $methodName;
×
52
    }
53

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

68
        return Path::canonicalize(implode('/', $path));
1✔
69
    }
70

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

86
        //return implode(DIRECTORY_SEPARATOR, $path);
87
        return Path::canonicalize(implode(DIRECTORY_SEPARATOR, $path));
1✔
88
    }
89

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

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

103
    /**
104
     * Converts microtime interval for human.
105
     */
106
    public static function convertMicrotime(float $start): string
107
    {
108
        $time = microtime(true) - $start;
1✔
109
        if ($time < 1) {
1✔
110
            return \sprintf('%s ms', round($time * 1000, 0));
1✔
111
        }
112

113
        return \sprintf('%s s', round($time, 2));
1✔
114
    }
115

116
    /**
117
     * Loads class from the source directory, in the given subdirectory $dir.
118
     */
119
    public static function autoload(Builder $builder, string $dir): void
120
    {
121
        spl_autoload_register(function ($className) use ($builder, $dir) {
1✔
122
            $classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php");
1✔
123
            if (is_readable($classFile)) {
1✔
124
                require $classFile;
1✔
125
                return;
1✔
126
            }
127
            // in themes
128
            foreach ($builder->getConfig()->getTheme() ?? [] as $theme) {
1✔
129
                $classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php");
1✔
130
                if (is_readable($classFile)) {
1✔
131
                    require $classFile;
×
132
                    return;
×
133
                }
134
            }
135
        });
1✔
136
    }
137
}
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