Test Failed
Pull Request — master (#313)
by Jean-Christophe
28:33
created

UString   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 89.39%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 45
eloc 48
dl 0
loc 150
ccs 59
cts 66
cp 0.8939
rs 8.8
c 1
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A contains() 0 2 1
A startswith() 0 2 1
A isJson() 0 2 1
A toString() 0 5 2
A doubleBackSlashes() 0 4 2
A isBooleanStr() 0 2 1
A cleanAttribute() 0 5 2
A explode() 0 2 1
A isBooleanTrue() 0 2 1
A mask() 0 2 1
A replaceFirstOccurrence() 0 3 1
A isNotNull() 0 2 3
A isBoolean() 0 2 1
A isNull() 0 2 3
A isValid() 0 2 3
A pluralize() 0 8 3
A isBooleanFalse() 0 2 1
A getBooleanStr() 0 2 4
A endswith() 0 2 1
A containsValues() 0 7 3
A isExpression() 0 4 5
A replaceArray() 0 6 2
A firstReplace() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like UString often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UString, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
/**
6
 * String utilities
7
 *
8
 * Ubiquity\utils\base$UString
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.6
13
 *
14
 */
15
class UString {
16
17 110
	public static function startswith($hay, $needle) {
18 110
		return \substr($hay, 0, strlen($needle)) === $needle;
19
	}
20
21 6
	public static function contains($needle, $haystack) {
22 6
		return \strpos($haystack, $needle) !== false;
23
	}
24
25
	public static function containsValues(array $values, string $haystack): bool {
26
		foreach ($values as $v) {
27
			if (\strpos($haystack, $v) !== false) {
28
				return true;
29
			}
30
		}
31
		return false;
32
	}
33
34 67
	public static function endswith($hay, $needle) {
35 67
		return \substr($hay, -strlen($needle)) === $needle;
36
	}
37
38 61
	public static function getBooleanStr($value) {
39 61
		return ($value === true || $value === 'true' || $value == 1) ? 'true' : 'false';
40
	}
41
42 20
	public static function isNull($s) {
43 20
		return (!isset ($s) || null === $s || '' === $s);
44
	}
45
46 33
	public static function isNotNull($s) {
47 33
		return (isset ($s) && null !== $s && '' !== $s);
48
	}
49
50 13
	public static function isBooleanTrue($s) {
51 13
		return filter_var($s, FILTER_VALIDATE_BOOLEAN) === true;
52
	}
53
54 1
	public static function isBooleanFalse($s) {
55 1
		return \filter_var($s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === false;
56
	}
57
58 1
	public static function isBoolean($value) {
59 1
		return \is_bool($value);
60
	}
61
62 73
	public static function isBooleanStr($value) {
63 73
		return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null;
64
	}
65
66
	/**
67
	 * Pluralize an expression
68
	 *
69
	 * @param int $count the count of elements
70
	 * @param string $zero value to return if count==0, can contains {count} mask
71
	 * @param string $one value to return if count==1, can contains {count} mask
72
	 * @param string $other value to return if count>1, can contains {count} mask
73
	 * @return string the pluralized expression
74
	 */
75 2
	public static function pluralize($count, $zero, $one, $other) {
76 2
		$result = $other;
77 2
		if ($count === 0) {
78 1
			$result = $zero;
79 2
		} elseif ($count === 1) {
80 2
			$result = $one;
81
		}
82 2
		return \str_replace('{count}', $count, $result);
83
	}
84
85 2
	public static function firstReplace($haystack, $needle, $replace) {
86 2
		$newstring = $haystack;
87 2
		$pos = \strpos($haystack, $needle);
88 2
		if ($pos !== false) {
89 2
			$newstring = \substr_replace($haystack, $replace, $pos, \strlen($needle));
90
		}
91 2
		return $newstring;
92
	}
93
94 1
	public static function replaceFirstOccurrence($pattern, $replacement, $subject) {
95 1
		$pattern = '/' . \preg_quote($pattern, '/') . '/';
96 1
		return \preg_replace($pattern, $replacement, $subject, 1);
97
	}
98
99 1
	public static function replaceArray($haystack, $needleArray, $replace) {
100 1
		$result = $haystack;
101 1
		foreach ($needleArray as $needle) {
102 1
			$result = self::firstReplace($result, $needle, $replace);
103
		}
104 1
		return $result;
105
	}
106
107 66
	public static function doubleBackSlashes($value) {
108 66
		if (\is_string($value))
109 66
			return \str_replace("\\", "\\\\", $value);
110 1
		return $value;
111
	}
112
113 5
	public static function cleanAttribute($attr, $replacement = "-") {
114 5
		$attr = \preg_replace('/[^a-zA-Z0-9\-]/s', $replacement, $attr);
115 5
		while ($attr !== ($attr = \str_replace($replacement . $replacement, $replacement, $attr)))
116
			;
117 5
		return $attr;
118
	}
119
120 6
	public static function mask($secretString, $maskChar = "*") {
121 6
		return \str_repeat($maskChar, \strlen($secretString));
122
	}
123
124 5
	public static function isValid($value) {
125 5
		return \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'));
126
	}
127
128 1
	public static function isJson($value) {
129 1
		return \is_object(\json_decode($value));
130
	}
131
132
	/**
133
	 * Converts a value to a string
134
	 *
135
	 * @param mixed $value
136
	 * @return string
137
	 */
138 1
	public static function toString($value) {
139 1
		if (self::isValid($value)) {
140 1
			return $value . '';
141
		}
142 1
		return '';
143
	}
144
145
	/**
146
	 * Explodes a string with an array of delimiters
147
	 *
148
	 * @param array $delimiters
149
	 * @param string $string
150
	 * @return array
151
	 */
152
	public static function explode($delimiters, $string) {
153
		return \explode($delimiters [0], \str_replace($delimiters, $delimiters [0], $string));
154
	}
155
156
	/**
157
	 * Returns true is value is a php expression.
158
	 * @param string $v
159
	 * @return bool
160
	 */
161
	public static function isExpression(string $v): bool {
162
		$v = \trim($v);
163
		return $v == 'nonce' || self::startswith($v, '$') || self::startswith($v, 'function')
164
			|| self::startswith($v, 'array(') || self::startswith($v, 'getenv(');
165
	}
166
}
167
168