Total Complexity | 45 |
Total Lines | 319 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Strings 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 Strings, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | final class Strings |
||
12 | { |
||
13 | /** |
||
14 | * Filter a string. |
||
15 | * |
||
16 | * Verify that the passed in value is a string. By default, nulls are not allowed, and the length is restricted |
||
17 | * between 1 and PHP_INT_MAX. These parameters can be overwritten for custom behavior. |
||
18 | * |
||
19 | * The return value is the string, as expected by the \TraderInteractive\Filterer class. |
||
20 | * |
||
21 | * @param mixed $value The value to filter. |
||
22 | * @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed. |
||
23 | * @param int $minLength Minimum length to allow for $value. |
||
24 | * @param int $maxLength Maximum length to allow for $value. |
||
25 | * @return string|null The passed in $value. |
||
26 | * |
||
27 | * @throws FilterException if the value did not pass validation. |
||
28 | * @throws \InvalidArgumentException if one of the parameters was not correctly typed. |
||
29 | */ |
||
30 | public static function filter( |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Explodes a string into an array using the given delimiter. |
||
52 | * |
||
53 | * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz']. |
||
54 | * |
||
55 | * @param string $value The string to explode. |
||
56 | * @param string $delimiter The non-empty delimiter to explode on. |
||
57 | * @return array The exploded values. |
||
58 | * |
||
59 | * @throws \InvalidArgumentException if the delimiter does not pass validation. |
||
60 | */ |
||
61 | public static function explode($value, string $delimiter = ',') |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * This filter takes the given string and translates it using the given value map. |
||
76 | * |
||
77 | * @param string $value The string value to translate |
||
78 | * @param array $valueMap Array of key value pairs where a key will match the given $value. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public static function translate(string $value, array $valueMap) : string |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * This filter prepends $prefix and appends $suffix to the string value. |
||
93 | * |
||
94 | * @param mixed $value The string value to which $prefix and $suffix will be added. |
||
95 | * @param string $prefix The value to prepend to the string. |
||
96 | * @param string $suffix The value to append to the string. |
||
97 | * |
||
98 | * @return string |
||
99 | * |
||
100 | * @throws FilterException Thrown if $value cannot be casted to a string. |
||
101 | */ |
||
102 | public static function concat($value, string $prefix = '', string $suffix = '') : string |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * This filter trims and removes superfluous whitespace characters from the given string. |
||
110 | * |
||
111 | * @param string|null $value The string to compress. |
||
112 | * @param bool $replaceVerticalWhitespace Flag to replace vertical whitespace such as newlines with |
||
113 | * single space. |
||
114 | * |
||
115 | * @return string|null |
||
116 | */ |
||
117 | public static function compress(string $value = null, bool $replaceVerticalWhitespace = false) |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * This filter replaces the given words with a replacement character. |
||
130 | * |
||
131 | * @param mixed $value The raw input to run the filter against. |
||
132 | * @param array|callable $words The words to filter out. |
||
133 | * @param string $replacement The character to replace the words with. |
||
134 | * |
||
135 | * @return string|null |
||
136 | * |
||
137 | * @throws FilterException Thrown when a bad value is encountered. |
||
138 | */ |
||
139 | public static function redact( |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Strip HTML and PHP tags from a string and, optionally, replace the tags with a string. |
||
162 | * Unlike the strip_tags function, this method will return null if a null value is given. |
||
163 | * The native php function will return an empty string. |
||
164 | * |
||
165 | * @param string|null $value The input string. |
||
166 | * @param string $replacement The string to replace the tags with. Defaults to an empty string. |
||
167 | * |
||
168 | * @return string|null |
||
169 | */ |
||
170 | public static function stripTags(string $value = null, string $replacement = '') |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * This filter ensures the given string $input matches the given regular expression pattern. |
||
187 | * |
||
188 | * @param string|null $input The string value to filter. |
||
189 | * @param string $pattern The PCRE pattern to match. |
||
190 | * |
||
191 | * @return string |
||
192 | * |
||
193 | * @throws FilterException |
||
194 | */ |
||
195 | public static function match(string $input, string $pattern) : string |
||
196 | { |
||
197 | $matched = preg_match($pattern, $input); |
||
198 | if ($matched === 1) { |
||
199 | return $input; |
||
200 | } |
||
201 | |||
202 | throw new FilterException("The given string '$input' did not match the expression {$pattern}"); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * This filter ensures the given string $input matches the given regular expression pattern and returns the found |
||
207 | * named subpattern. |
||
208 | * |
||
209 | * @param string|null $input The string value to filter. |
||
210 | * @param string $pattern The PCRE pattern to match. |
||
211 | * @param string $name The name of the sub pattern to extract. |
||
212 | * |
||
213 | * @return string |
||
214 | * |
||
215 | * @throws FilterException |
||
216 | */ |
||
217 | public static function extract(string $input, string $pattern, string $name) : string |
||
233 | } |
||
234 | |||
235 | private static function validateMinimumLength(int $minLength) |
||
236 | { |
||
237 | if ($minLength < 0) { |
||
238 | throw new \InvalidArgumentException('$minLength was not a positive integer value'); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | private static function validateMaximumLength(int $maxLength) |
||
243 | { |
||
244 | if ($maxLength < 0) { |
||
245 | throw new \InvalidArgumentException('$maxLength was not a positive integer value'); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | private static function validateStringLength(string $value = null, int $minLength, int $maxLength) |
||
250 | { |
||
251 | $valueLength = strlen($value); |
||
252 | if ($valueLength < $minLength || $valueLength > $maxLength) { |
||
253 | $format = "Value '%s' with length '%d' is less than '%d' or greater than '%d'"; |
||
254 | throw new FilterException( |
||
255 | sprintf($format, $value, $valueLength, $minLength, $maxLength) |
||
256 | ); |
||
257 | } |
||
258 | } |
||
259 | |||
260 | private static function valueIsNullAndValid(bool $allowNull, $value = null) : bool |
||
267 | } |
||
268 | |||
269 | private static function validateIfObjectIsAString($value) |
||
270 | { |
||
271 | if (!is_string($value)) { |
||
272 | throw new FilterException("Value '" . var_export($value, true) . "' is not a string"); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | private static function enforceValueCanBeCastAsString($value) |
||
277 | { |
||
278 | try { |
||
279 | $value = ( |
||
280 | function (string $str) : string { |
||
281 | return $str; |
||
282 | } |
||
283 | )($value); |
||
284 | } catch (TypeError $te) { |
||
285 | throw new FilterException(sprintf("Value '%s' is not a string", var_export($value, true))); |
||
286 | } |
||
287 | |||
288 | return $value; |
||
289 | } |
||
290 | |||
291 | private static function replaceWordsWithReplacementString(string $value, array $words, string $replacement) : string |
||
301 | } |
||
302 | |||
303 | private static function getMatchingWords(array $words, string $value) : array |
||
304 | { |
||
305 | $matchingWords = []; |
||
306 | foreach ($words as $word) { |
||
307 | $escapedWord = preg_quote($word, '/'); |
||
308 | $caseInsensitiveWordPattern = "/\b{$escapedWord}\b/i"; |
||
309 | if (preg_match($caseInsensitiveWordPattern, $value)) { |
||
310 | $matchingWords[] = $word; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | return $matchingWords; |
||
315 | } |
||
316 | |||
317 | private static function generateReplacementsMap(array $words, string $replacement) : array |
||
330 | ); |
||
331 | } |
||
332 | } |
||
333 |