Test Failed
Push — master ( 892598...558941 )
by Antonio Carlos
11:17
created

helpers.php ➔ instantiate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
use PragmaRX\Health\Service;
4
5
if (!function_exists('instantiate')) {
6
    /**
7
     * Instantiate a class.
8
     *
9
     * @param $abstract
10
     * @param array $parameters
11
     * @return object
12
     */
13
    function instantiate($abstract, $parameters = [])
14
    {
15 10
        if (is_array($parameters) && count($parameters)) {
16 10
            $reflection = new ReflectionClass($abstract);
17
18 10
            return $reflection->newInstanceArgs((array) $parameters);
19
        }
20
21 8
        return app($abstract);
22
    }
23
}
24
25
if (!function_exists('package_dir')) {
26
    /**
27
     * Get the package root directory.
28
     *
29
     * @return string
30
     */
31
    function package_dir()
32
    {
33 10
        $reflector = new ReflectionClass(Service::class);
34
35 10
        return dirname($reflector->getFileName());
36
    }
37
}
38
39
if (!function_exists('package_resources_dir')) {
40
    /**
41
     * Get package resources directory.
42
     *
43
     * @return string
44
     */
45
    function package_resources_dir()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46
    {
47
        return (
48 10
            package_dir() .
49 10
            DIRECTORY_SEPARATOR .
50 10
            'config' .
51 10
            DIRECTORY_SEPARATOR .
52 10
            'resources'
53
        );
54
    }
55
}
56
57
if (!function_exists('is_absolute_path')) {
58
    /**
59
     * Check if string is absolute path.
60
     *
61
     * @param $path
62
     * @return string
63
     */
64
    function is_absolute_path($path)
65
    {
66
        // Optional wrapper(s).
67
        $regExp =
68
            // Optional root prefix.
69
            '%^(?<wrappers>(?:[[:print:]]{2,}://)*)' .
70
            '(?<root>(?:[[:alpha:]]:/|/)?)' .
71
            // Actual path.
72 1
            '(?<path>(?:[[:print:]]*))$%';
73
74 1
        $parts = [];
75
76 1
        preg_match($regExp, $path, $parts);
77
78 1
        if ('' !== $parts['root']) {
79
            return true;
80
        }
81
82 1
        return false;
83
    }
84
}
85
86
if (!function_exists('bytes_to_human')) {
87
    /**
88
     * Convert bytes to human readable.
89
     *
90
     * @return string
91
     */
92
    function bytes_to_human($bytes)
93
    {
94
        $base = log($bytes) / log(1024);
95
96
        $suffix = ['', 'KB', 'MB', 'GB', 'TB'];
97
98
        $f_base = floor($base);
99
100
        return round(pow(1024, $base - floor($base)), 1) . $suffix[$f_base];
101
    }
102
}
103
104
if (!function_exists('human_to_bytes')) {
105
    /**
106
     * Convert bytes to human readable.
107
     *
108
     * @return string
109
     */
110
    function human_to_bytes($str)
111
    {
112 1
        $str = trim($str);
113
114 1
        $num = (float) $str;
115
116 1
        if (strtoupper(substr($str, -1)) == 'B') {
117 1
            $str = substr($str, 0, -1);
118
        }
119
120 1
        switch (strtoupper(substr($str, -1))) {
121
            case 'P':
122
                $num *= 1024;
123
            case 'T':
124
                $num *= 1024;
125
            case 'G':
126 1
                $num *= 1024;
127
            case 'M':
128 1
                $num *= 1024;
129
            case 'K':
130 1
                $num *= 1024;
131
        }
132
133 1
        return $num;
134
    }
135
}
136
137
if (!function_exists('ip_address_from_hostname')) {
138
    function ip_address_from_hostname($host)
139
    {
140
        if (
141 1
            filter_var(
142 1
                $host,
143 1
                FILTER_VALIDATE_IP,
144 1
                FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
145
            )
146
        ) {
147
            return $host;
148
        }
149
150 1
        if (filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
151 1
            return gethostbyname($host);
152
        }
153
154
        return false;
155
    }
156
}
157