These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 | |||
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 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.