Completed
Push — master ( 3a9f70...45da6a )
by C
05:33
created

Util::cleanHostName()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 8.7624
cc 5
eloc 11
nc 16
nop 1
crap 5
1
<?php
2
namespace Tartana;
3
use Pdp\Parser;
4
use Pdp\PublicSuffixListManager;
5
6
final class Util
7
{
8
9
	/**
10
	 * Returns if there is a process with the given id running.
11
	 *
12
	 * @param integer $pid
13
	 * @return boolean
14
	 */
15 7
	public static function isPidRunning ($pid)
16
	{
17 7
		return posix_getpgid((int) $pid) > 0;
18
	}
19
20
	/**
21
	 * Convertes the given size to a human readable format.
22
	 *
23
	 * @param integer $size
24
	 * @param array $strings
25
	 * @return string
26
	 */
27 8
	public static function readableSize ($size, $strings = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'])
28
	{
29 8
		for ($i = 0; ($size / 1024) > 0.9; $i ++, $size /= 1024)
0 ignored issues
show
Unused Code introduced by
This for loop is empty and can be removed.

This check looks for for loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
30
		{
31
		}
32 8
		return round($size, 2) . (isset($strings[$i]) ? ' ' . $strings[$i] : '');
33
	}
34
35
	/**
36
	 * Returns the real path for the given path.
37
	 * If the path is relative and the file exists, the real fill path is
38
	 * returned.
39
	 *
40
	 * @param string $path
41
	 * @return NULL|string
42
	 */
43 65
	public static function realPath ($path)
44
	{
45 65
		if (! $path)
46
		{
47 12
			return null;
48
		}
49
50
		// If it is a relative path add the root path to it
51 59
		if ($path[0] !== DIRECTORY_SEPARATOR && preg_match('~\A[A-Z]:(?![^/\\\\])~i', $path) < 1)
52
		{
53 11
			$path = TARTANA_PATH_ROOT . '/' . $path;
54
		}
55
56 59
		if (! file_exists($path))
57
		{
58 10
			return null;
59
		}
60
61 53
		return $path;
62
	}
63
64
	/**
65
	 * Returns a deep cloned array of the given objects.
66
	 *
67
	 * @param array $objects
68
	 * @return array
69
	 */
70
	public static function cloneObjects (array $objects)
71
	{
72 102
		return array_map(function  ($obj) {
73 96
			return clone $obj;
74 102
		}, $objects);
75
	}
76
77
	/**
78
	 * Checks if the haystack starts with the needle.
79
	 *
80
	 * @param string $haystack
81
	 * @param string $needle
82
	 * @return boolean
83
	 */
84 12
	public static function startsWith ($haystack, $needle)
85
	{
86 12
		return (substr($haystack, 0, strlen($needle)) === $needle);
87
	}
88
89
	/**
90
	 * Checks if the haystack ends with the needle.
91
	 *
92
	 * @param string $haystack
93
	 * @param string $needle
94
	 * @return boolean
95
	 */
96 37
	public static function endsWith ($haystack, $needle)
97
	{
98 37
		$length = strlen($needle);
99 37
		if ($length == 0)
100
		{
101 1
			return true;
102
		}
103
104 37
		return (substr($haystack, - $length) === $needle);
105
	}
106
107
	/**
108
	 * Shortens the given string by replacing the middle part with three dots.
109
	 *
110
	 * @param string $string
111
	 * @param integer $length
112
	 *
113
	 * @return string
114
	 */
115 5
	public static function shorten ($string, $length = 20)
116
	{
117 5
		if (strlen($string) > $length)
118
		{
119 1
			$characters = floor($length / 2);
120 1
			return substr($string, 0, $characters) . '...' . substr($string, - 1 * $characters);
121
		}
122 5
		return $string;
123
	}
124
125
	/**
126
	 * Parses the given url and returns an array with the following properties
127
	 * for that url:
128
	 * http://user:[email protected]:8000/link/test.html?hello=foo#bar
129
	 *
130
	 * [scheme] => http
131
	 * [user] => user
132
	 * [pass] => pass
133
	 * [host] => mirrors.kernel.org
134
	 * [subdomain] => mirrors
135
	 * [registerableDomain] => kernel.org
136
	 * [publicSuffix] => org
137
	 * [port] => 8000
138
	 * [path] => /link/test.html
139
	 * [query] => hello=foo
140
	 * [fragment] => bar
141
	 *
142
	 * @param string $url
143
	 * @return array
144
	 */
145 34
	public static function parseUrl ($url)
146
	{
147
		try
148
		{
149 34
			$pslManager = new PublicSuffixListManager();
150 34
			$parser = new Parser($pslManager->getList());
151
152 34
			return $parser->parseUrl($url)->toArray();
153
		}
154 5
		catch (\Exception $e)
155
		{
156
			return [
157 5
					'scheme' => '',
158
					'user' => '',
159
					'pass' => '',
160
					'host' => '',
161
					'subdomain' => '',
162
					'registerableDomain' => '',
163
					'publicSuffix' => '',
164
					'port' => '',
165
					'path' => '',
166
					'query' => '',
167
					'fragment' => ''
168
			];
169
		}
170
	}
171
172
	/**
173
	 * Cleans special characters from the uri which can be used as identifier in
174
	 * YAMl files.
175
	 *
176
	 * @param array $uri
177
	 * @return string
178
	 * @see Util::parseUrl()
179
	 */
180 17
	public static function cleanHostName ($uri)
181
	{
182 17
		if (is_string($uri))
183
		{
184
			$uri = [
185 7
					'host' => $uri
186
			];
187
		}
188 17
		if (! isset($uri['registerableDomain']))
189
		{
190 10
			$uri['registerableDomain'] = '';
191
		}
192 17
		if (! isset($uri['host']))
193
		{
194 3
			$uri['host'] = '';
195
		}
196 17
		$hostName = $uri['registerableDomain'] ? $uri['registerableDomain'] : $uri['host'];
197 17
		$hostName = preg_replace("/[^A-Za-z0-9 ]/", '', $hostName);
198
199 17
		return $hostName;
200
	}
201
}