Passed
Pull Request — master (#1676)
by Arnaud
10:58 queued 04:17
created

Util::joinPath()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 1
nop 1
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
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 1
    public static function formatClassName($class, array $options = []): string
28
    {
29 1
        $lowercase = false;
30 1
        extract($options, EXTR_IF_EXISTS);
31
32 1
        $className = substr(strrchr(\get_class($class), '\\'), 1);
33 1
        if ($lowercase) {
34 1
            $className = strtolower($className);
35
        }
36
37 1
        return $className;
38
    }
39
40
    /**
41
     * Converts an array of strings into a path.
42
     */
43 1
    public static function joinPath(string ...$path): string
44
    {
45 1
        $path = array_filter($path, function ($path) {
46 1
            return !empty($path) && !\is_null($path);
47 1
        });
48 1
        array_walk($path, function (&$value, $key) {
49 1
            $value = str_replace('\\', '/', $value);
50 1
            $value = rtrim($value, '/');
51 1
            $value = $key == 0 ? $value : ltrim($value, '/');
52 1
        });
53
54 1
        return Path::canonicalize(implode('/', $path));
55
    }
56
57
    /**
58
     * Converts an array of strings into a system path.
59
     */
60 1
    public static function joinFile(string ...$path): string
61
    {
62 1
        array_walk($path, function (&$value, $key) use (&$path) {
63 1
            $value = str_replace(['\\', '/'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $value);
64 1
            $value = rtrim($value, DIRECTORY_SEPARATOR);
65 1
            $value = $key == 0 ? $value : ltrim($value, DIRECTORY_SEPARATOR);
66
            // unset entry with empty value
67 1
            if (empty($value)) {
68 1
                unset($path[$key]);
69
            }
70 1
        });
71
72 1
        return implode(DIRECTORY_SEPARATOR, $path);
73
    }
74
75
    /**
76
     * Converts memory size for human.
77
     */
78 1
    public static function convertMemory($size): string
79
    {
80 1
        if ($size === 0) {
81
            return '0';
82
        }
83 1
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
84
85 1
        return sprintf('%s %s', round($size / pow(1024, $i = floor(log($size, 1024))), 2), $unit[$i]);
86
    }
87
88
    /**
89
     * Converts microtime interval for human.
90
     */
91 1
    public static function convertMicrotime(float $start): string
92
    {
93 1
        $time = microtime(true) - $start;
94 1
        if ($time < 1) {
95 1
            return sprintf('%s ms', round($time * 1000, 0));
96
        }
97
98 1
        return sprintf('%s s', round($time, 2));
99
    }
100
101
    /**
102
     * Loads class from the source directory, in the given subdirectory $dir.
103
     */
104 1
    public static function autoload(Builder $builder, string $dir): void
105
    {
106 1
        spl_autoload_register(function ($className) use ($builder, $dir) {
107 1
            $classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php");
108 1
            if (file_exists($classFile)) {
109 1
                require $classFile;
110 1
                return;
111
            }
112
            // in themes
113 1
            foreach ($builder->getConfig()->getTheme() ?? [] as $theme) {
114 1
                $classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php");
115 1
                if (file_exists($classFile)) {
116
                    require $classFile;
117
                    return;
118
                }
119
            }
120 1
        });
121
    }
122
}
123