Passed
Push — master ( e32a81...12baf4 )
by Antonio Carlos
02:26
created

src/Support/helpers.php (1 issue)

Upgrade to new PHP Analysis Engine

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 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
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
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 1
            '(?<path>(?:[[:print:]]*))$%';
72
73 1
        $parts = [];
74
75 1
        preg_match($regExp, $path, $parts);
76
77 1
        if ('' !== $parts['root']) {
78
            return true;
79
        }
80
81 1
        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 1
        $str = trim($str);
112
113 1
        $num = (float) $str;
114
115 1
        if (strtoupper(substr($str, -1)) == 'B') {
116 1
            $str = substr($str, 0, -1);
117
        }
118
119 1
        switch (strtoupper(substr($str, -1))) {
120
            case 'P':
121
                $num *= 1024;
122
            case 'T':
123
                $num *= 1024;
124
            case 'G':
125 1
                $num *= 1024;
126
            case 'M':
127 1
                $num *= 1024;
128
            case 'K':
129 1
                $num *= 1024;
130
        }
131
132 1
        return $num;
133
    }
134
}
135
136
if (! function_exists('ip_address_from_hostname')) {
137
    function ip_address_from_hostname($host)
138
    {
139
        if (
140 1
            filter_var(
141 1
                $host,
142 1
                FILTER_VALIDATE_IP,
143 1
                FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
144
            )
145
        ) {
146
            return $host;
147
        }
148
149 1
        if (filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
150 1
            return gethostbyname($host);
151
        }
152
153
        return false;
154
    }
155
}
156