Test Failed
Push — master ( bfe357...41d8e8 )
by Antonio Carlos
25:24
created

helpers.php ➔ package_dir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 2
cp 0
crap 2
rs 10
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
        if (is_array($parameters) && count($parameters)) {
16
            $reflection = new ReflectionClass($abstract);
17
18
            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
        $reflector = new ReflectionClass(Service::class);
34
35
        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
            package_dir() .
49
            DIRECTORY_SEPARATOR .
50
            'config' .
51
            DIRECTORY_SEPARATOR .
52
            '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
            '(?<path>(?:[[:print:]]*))$%';
73
74
        $parts = [];
75
76
        preg_match($regExp, $path, $parts);
77
78
        if ('' !== $parts['root']) {
79
            return true;
80
        }
81
82
        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
        $str = trim($str);
113
114
        $num = (float) $str;
115
116
        if (strtoupper(substr($str, -1)) == 'B') {
117
            $str = substr($str, 0, -1);
118
        }
119
120
        switch (strtoupper(substr($str, -1))) {
121
            case 'P':
122
                $num *= 1024;
123
            case 'T':
124
                $num *= 1024;
125
            case 'G':
126
                $num *= 1024;
127
            case 'M':
128
                $num *= 1024;
129
            case 'K':
130
                $num *= 1024;
131
        }
132
133
        return $num;
134
    }
135
}
136
137
if (!function_exists('ip_address_from_hostname')) {
138
    function ip_address_from_hostname($host)
139
    {
140
        if (
141
            filter_var(
142
                $host,
143
                FILTER_VALIDATE_IP,
144
                FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
145
            )
146
        ) {
147
            return $host;
148
        }
149
150
        if (filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
151
            return gethostbyname($host);
152
        }
153
154
        return false;
155
    }
156
}
157