Passed
Push — master ( de702b...3e9331 )
by Pauli
02:18
created

Util   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 19.15%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 164
ccs 9
cts 47
cp 0.1915
rs 10
c 10
b 0
f 0
wmc 22

13 Methods

Rating   Name   Duplication   Size   Complexity  
A extractIds() 0 4 1
A stringCaseCompare() 0 2 1
A swap() 0 4 1
A endsWith() 0 2 1
A arrayDiff() 0 9 3
A truncate() 0 5 2
A extractUserIds() 0 4 1
A formatFileSize() 0 4 1
A startsWith() 0 2 1
A createIdLookupTable() 0 6 2
A getFolderFromRelativePath() 0 5 4
A arrayGetOrDefault() 0 2 2
A arrayMultiGet() 0 6 2
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2018 - 2020
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * Miscellaneous static utility functions
17
 */
18
class Util {
19
20
	/**
21
	 * Extract ID of each array element by calling getId and return
22
	 * the IDs as an array
23
	 * @param array $arr
24
	 * @return array
25
	 */
26 4
	public static function extractIds(array $arr) {
27
		return \array_map(function ($i) {
28 4
			return $i->getId();
29 4
		}, $arr);
30
	}
31
32
	/**
33
	 * Extract User ID of each array element by calling getUserId and return
34
	 * the IDs as an array
35
	 * @param array $arr
36
	 * @return array
37
	 */
38
	public static function extractUserIds(array $arr) {
39
		return \array_map(function ($i) {
40
			return $i->getUserId();
41
		}, $arr);
42
	}
43
44
	/**
45
	 * Create look-up table from given array of items which have a `getId` function.
46
	 * @param array $array
47
	 * @return array where keys are the values returned by `getId` of each item
48
	 */
49
	public static function createIdLookupTable($array) {
50
		$lut = [];
51
		foreach ($array as $item) {
52
			$lut[$item->getId()] = $item;
53
		}
54
		return $lut;
55
	}
56
57
	/**
58
	 * Get difference of two arrays, i.e. elements belonging to $b but not $a.
59
	 * This function is faster than the built-in array_diff for large arrays but
60
	 * at the expense of higher RAM usage and can be used only for arrays of
61
	 * integers or strings.
62
	 * From https://stackoverflow.com/a/8827033
63
	 * @param array $b
64
	 * @param array $a
65
	 * @return array
66
	 */
67
	public static function arrayDiff($b, $a) {
68
		$at = \array_flip($a);
69
		$d = [];
70
		foreach ($b as $i) {
71
			if (!isset($at[$i])) {
72
				$d[] = $i;
73
			}
74
		}
75
		return $d;
76
	}
77
78
	/**
79
	 * Get multiple items from @a $array, as indicated by a second array @a $indices.
80
	 * @param array $array
81
	 * @param array $indices
82
	 * @return array
83
	 */
84
	public static function arrayMultiGet($array, $indices) {
85
		$result = [];
86
		foreach ($indices as $index) {
87
			$result[] = $array[$index];
88
		}
89
		return $result;
90
	}
91
92
	/**
93
	 * Get array value if exists, otherwise return a default value or null
94
	 * @param array $array
95
	 * @param int|string $key
96
	 * @param mixed|null $default
97
	 * @return mixed|null
98
	 */
99 3
	public static function arrayGetOrDefault($array, $key, $default=null) {
100 3
		return isset($array[$key]) ? $array[$key] : $default;
101
	}
102
103
	/**
104
	 * Truncate the given string to maximum length, appendig ellipsis character
105
	 * if the truncation happened. Also null argument may be safely passed and
106
	 * it remains unaltered.
107
	 * @param string|null $string
108
	 * @param int $maxLength
109
	 * @return string|null
110
	 */
111 3
	public static function truncate($string, $maxLength) {
112 3
		if ($string === null) {
113 1
			return null;
114
		} else {
115 2
			return \mb_strimwidth($string, 0, $maxLength, "\u{2026}");
116
		}
117
	}
118
119
	/**
120
	 * Test if given string starts with another given string
121
	 * @param string $string
122
	 * @param string $potentialStart
123
	 * @return boolean
124
	 */
125
	public static function startsWith($string, $potentialStart) {
126
		return \substr($string, 0, \strlen($potentialStart)) === $potentialStart;
127
	}
128
129
	/**
130
	 * Test if given string ends with another given string
131
	 * @param string $string
132
	 * @param string $potentialEnd
133
	 * @return boolean
134
	 */
135
	public static function endsWith($string, $potentialEnd) {
136
		return \substr($string, -\strlen($potentialEnd)) === $potentialEnd;
137
	}
138
139
	/**
140
	 * Multi-byte safe case-insensitive string comparison
141
	 * @param string $a
142
	 * @param string $b
143
	 */
144
	public static function stringCaseCompare($a, $b) {
145
		return \strcmp(\mb_strtolower($a), \mb_strtolower($b));
146
	}
147
148
	/**
149
	 * Convert file size given in bytes to human-readable format
150
	 * @param int $bytes
151
	 * @param int $decimals
152
	 * @return string
153
	 */
154
	public static function formatFileSize($bytes, $decimals = 1) {
155
		$units = 'BKMGTP';
156
		$factor = \floor((\strlen($bytes) - 1) / 3);
157
		return \sprintf("%.{$decimals}f", $bytes / \pow(1024, $factor)) . @$units[(int)$factor];
158
	}
159
160
	/**
161
	 * @param Folder $parentFolder
0 ignored issues
show
Bug introduced by
The type OCA\Music\Utility\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
162
	 * @param string $relativePath
163
	 * @return Folder
164
	 */
165
	public static function getFolderFromRelativePath($parentFolder, $relativePath) {
166
		if ($relativePath !== null && $relativePath !== '/' && $relativePath !== '') {
167
			return $parentFolder->get($relativePath);
168
		} else {
169
			return $parentFolder;
170
		}
171
	}
172
173
	/**
174
	 * Swap values of two variables in place
175
	 * @param mixed $a
176
	 * @param mixed $b
177
	 */
178
	public static function swap(&$a, &$b) {
179
		$temp = $a;
180
		$a = $b;
181
		$b = $temp;
182
	}
183
}
184