Passed
Pull Request — master (#1676)
by Arnaud
08:52 queued 03:11
created

Util   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 84
ccs 38
cts 40
cp 0.95
rs 10
c 1
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A formatClassName() 0 11 2
A joinPath() 0 12 3
A joinFile() 0 13 3
A autoload() 0 14 4
A convertMemory() 0 5 1
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\OptimizeHtml" become "OptimizeHtml"
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
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
79
80 1
        return \sprintf('%s %s', round($size / pow(1024, ($i = floor(log($size, 1024)))), 2), $unit[$i]);
81
    }
82
83
    /**
84
     * Loads class from the source directory, in the given subdirectory $dir.
85
     */
86 1
    public static function autoload(Builder $builder, string $dir): void
87
    {
88 1
        spl_autoload_register(function ($className) use ($builder, $dir) {
89 1
            $classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php");
90 1
            if (file_exists($classFile)) {
91 1
                require $classFile;
92 1
                return;
93
            }
94
            // in themes
95 1
            foreach ($builder->getConfig()->getTheme() as $theme) {
96 1
                $classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php");
97 1
                if (file_exists($classFile)) {
98
                    require $classFile;
99
                    return;
100
                }
101
            }
102 1
        });
103
    }
104
}
105