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

Cecilapp / Cecil / 7135108204

07 Dec 2023 11:10PM UTC coverage: 82.976% (+0.4%) from 82.534%
7135108204

Pull #1676

github

web-flow
Merge ce0d85209 into 814daa587
Pull Request #1676: 8.x dev

184 of 222 new or added lines in 31 files covered. (82.88%)

15 existing lines in 5 files now uncovered.

2861 of 3448 relevant lines covered (82.98%)

0.83 hits per line

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

92.86
/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
     * Converts an array of strings into a path.
42
     */
43
    public static function joinPath(string ...$path): string
44
    {
45
        $path = array_filter($path, function ($path) {
1✔
46
            return !empty($path) && !\is_null($path);
1✔
47
        });
1✔
48
        array_walk($path, function (&$value, $key) {
1✔
49
            $value = str_replace('\\', '/', $value);
1✔
50
            $value = rtrim($value, '/');
1✔
51
            $value = $key == 0 ? $value : ltrim($value, '/');
1✔
52
        });
1✔
53

54
        return Path::canonicalize(implode('/', $path));
1✔
55
    }
56

57
    /**
58
     * Converts an array of strings into a system path.
59
     */
60
    public static function joinFile(string ...$path): string
61
    {
62
        array_walk($path, function (&$value, $key) use (&$path) {
1✔
63
            $value = str_replace(['\\', '/'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $value);
1✔
64
            $value = rtrim($value, DIRECTORY_SEPARATOR);
1✔
65
            $value = $key == 0 ? $value : ltrim($value, DIRECTORY_SEPARATOR);
1✔
66
            // unset entry with empty value
67
            if (empty($value)) {
1✔
68
                unset($path[$key]);
1✔
69
            }
70
        });
1✔
71

72
        return implode(DIRECTORY_SEPARATOR, $path);
1✔
73
    }
74

75
    /**
76
     * Converts memory size for human.
77
     */
78
    public static function convertMemory($size): string
79
    {
80
        if ($size === 0) {
1✔
81
            return '0';
×
82
        }
83
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
1✔
84

85
        return sprintf('%s %s', round($size / pow(1024, $i = floor(log($size, 1024))), 2), $unit[$i]);
1✔
86
    }
87

88
    /**
89
     * Converts microtime interval for human.
90
     */
91
    public static function convertMicrotime(float $start): string
92
    {
93
        $time = microtime(true) - $start;
1✔
94
        if ($time < 1) {
1✔
95
            return sprintf('%s ms', round($time * 1000, 0));
1✔
96
        }
97

98
        return sprintf('%s s', round($time, 2));
1✔
99
    }
100

101
    /**
102
     * Loads class from the source directory, in the given subdirectory $dir.
103
     */
104
    public static function autoload(Builder $builder, string $dir): void
105
    {
106
        spl_autoload_register(function ($className) use ($builder, $dir) {
1✔
107
            $classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php");
1✔
108
            if (file_exists($classFile)) {
1✔
109
                require $classFile;
1✔
110
                return;
1✔
111
            }
112
            // in themes
113
            foreach ($builder->getConfig()->getTheme() ?? [] as $theme) {
1✔
114
                $classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php");
1✔
115
                if (file_exists($classFile)) {
1✔
NEW
116
                    require $classFile;
×
NEW
117
                    return;
×
118
                }
119
            }
120
        });
1✔
121
    }
122
}
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

© 2025 Coveralls, Inc