Passed
Push — master ( ad05ee...3635fb )
by Arnaud
05:31
created

Util::autoload()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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