Passed
Push — memory ( c07ba3...694cdf )
by Arnaud
09:11 queued 05:11
created

Util::convertMemory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
        });
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
        });
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
        });
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