Test Failed
Push — master ( 3fe005...bfd1fd )
by Antonio Carlos
09:02
created

helpers.php ➔ package_resources_dir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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