These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Innmind\Immutable; |
||
4 | |||
5 | use Innmind\Immutable\Exception\TypeException; |
||
6 | use Innmind\Immutable\Exception\RegexException; |
||
7 | |||
8 | class StringPrimitive implements PrimitiveInterface, StringableInterface |
||
9 | { |
||
10 | private $value; |
||
11 | |||
12 | 2 | public function __construct($value) |
|
13 | { |
||
14 | 2 | if (!is_string($value)) { |
|
15 | 1 | throw new TypeException('Value must be a string'); |
|
16 | } |
||
17 | |||
18 | 1 | $this->value = (string) $value; |
|
19 | 1 | } |
|
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | 1 | public function toPrimitive() |
|
25 | { |
||
26 | 1 | return $this->value; |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 1 | public function __toString() |
|
33 | { |
||
34 | 1 | return $this->value; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Split the string into a collection of ones |
||
39 | * |
||
40 | * @param string $delimiter |
||
41 | * |
||
42 | * @return TypedCollection |
||
43 | */ |
||
44 | public function split($delimiter) |
||
45 | { |
||
46 | $delimiter = (string) $delimiter; |
||
47 | |||
48 | return new TypedCollection( |
||
49 | self::class, |
||
50 | empty($delimiter) ? |
||
51 | str_split($this->value) : explode($delimiter, $this->value) |
||
52 | ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Returns a collection of the string splitted by the given chunk size |
||
57 | * |
||
58 | * @param int $size |
||
59 | * |
||
60 | * @return TypedCollection |
||
61 | */ |
||
62 | public function chunk($size = 1) |
||
63 | { |
||
64 | $size = (int) $size; |
||
65 | |||
66 | return new TypedCollection( |
||
67 | self::class, |
||
68 | str_split($this->value, $size) |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns the position of the first occurence of the string |
||
74 | * |
||
75 | * @param string $needle |
||
76 | * @param int $offset |
||
77 | * |
||
78 | * @throws Exception If the string is not found |
||
79 | * |
||
80 | * @return IntegerPrimitive |
||
81 | */ |
||
82 | public function pos($needle, $offset = 0) |
||
83 | { |
||
84 | $position = mb_strpos($this->value, (string) $needle, (int) $offset); |
||
85 | |||
86 | if ($position === false) { |
||
87 | throw new \Exception('String not found'); |
||
88 | } |
||
89 | |||
90 | return new IntegerPrimitive($position); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Replace all occurences of the search string with the replacement one |
||
95 | * |
||
96 | * @param string $search |
||
97 | * @param string $replacement |
||
98 | * |
||
99 | * @return StringPrimitive |
||
100 | */ |
||
101 | public function replace($search, $replacement) |
||
102 | { |
||
103 | return new self(str_replace( |
||
104 | (string) $search, |
||
105 | (string) $replacement, |
||
106 | $this->value |
||
107 | )); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns the string following the given delimiter |
||
112 | * |
||
113 | * @param string $delimiter |
||
114 | * |
||
115 | * @throws Exception If the string is not found |
||
116 | * |
||
117 | * @return StringInterface |
||
118 | */ |
||
119 | public function str($delimiter) |
||
120 | { |
||
121 | $sub = strstr($this->value, (string) $delimiter); |
||
122 | |||
123 | if ($sub === false) { |
||
124 | throw new \Exception('String not found'); |
||
125 | } |
||
126 | |||
127 | return new self($sub); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Return the string in upper case |
||
132 | * |
||
133 | * @return StringPrimitive |
||
134 | */ |
||
135 | public function toUpper() |
||
136 | { |
||
137 | return new self(mb_strtoupper($this->value)); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Return the string in lower case |
||
142 | * |
||
143 | * @return StringPrimitive |
||
144 | */ |
||
145 | public function toLower() |
||
146 | { |
||
147 | return new self(mb_strtolower($this->value)); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Return the string length |
||
152 | * |
||
153 | * @return IntegerPrimitive |
||
154 | */ |
||
155 | public function length() |
||
156 | { |
||
157 | return new IntegerPrimitive(strlen($this->value)); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Reverse the string |
||
162 | * |
||
163 | * @return StringPrimitive |
||
164 | */ |
||
165 | public function reverse() |
||
166 | { |
||
167 | return new self(strrev($this->value)); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Pad the string |
||
172 | * |
||
173 | * @param int $length |
||
174 | * @param string $character |
||
175 | * @param int $direction |
||
176 | * |
||
177 | * @return StringPrimitive |
||
178 | */ |
||
179 | public function pad($length, $character = ' ', $direction = STR_PAD_RIGHT) |
||
180 | { |
||
181 | return new self(str_pad( |
||
182 | $this->value, |
||
183 | (int) $length, |
||
184 | (string) $character, |
||
185 | $direction |
||
186 | )); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Pad to the right |
||
191 | * |
||
192 | * @param int $length |
||
193 | * @param string $charater |
||
0 ignored issues
–
show
|
|||
194 | * |
||
195 | * @return StringPrimitive |
||
196 | */ |
||
197 | public function rightPad($length, $character = ' ') |
||
198 | { |
||
199 | return $this->pad($length, $character, STR_PAD_RIGHT); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Pad to the left |
||
204 | * |
||
205 | * @param int $length |
||
206 | * @param string $charater |
||
0 ignored issues
–
show
There is no parameter named
$charater . Did you maybe mean $character ?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit. Consider the following example. The parameter /**
* @param array $germany
* @param array $ireland
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was changed, but the annotation was not. ![]() |
|||
207 | * |
||
208 | * @return StringPrimitive |
||
209 | */ |
||
210 | public function leftPad($length, $character = ' ') |
||
211 | { |
||
212 | return $this->pad($length, $character, STR_PAD_LEFT); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Pad both sides |
||
217 | * |
||
218 | * @param int $length |
||
219 | * @param string $charater |
||
0 ignored issues
–
show
There is no parameter named
$charater . Did you maybe mean $character ?
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit. Consider the following example. The parameter /**
* @param array $germany
* @param array $ireland
*/
function finale($germany, $island) {
return "2:1";
}
The most likely cause is that the parameter was changed, but the annotation was not. ![]() |
|||
220 | * |
||
221 | * @return StringPrimitive |
||
222 | */ |
||
223 | public function uniPad($length, $character = ' ') |
||
224 | { |
||
225 | return $this->pad($length, $character, STR_PAD_BOTH); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Find length of initial segment not matching mask |
||
230 | * |
||
231 | * @param string $mask |
||
232 | * @param int $start |
||
233 | * @param int $length |
||
234 | * |
||
235 | * @return IntegerPrimitive |
||
236 | */ |
||
237 | public function cspn($mask, $start = 0, $length = null) |
||
238 | { |
||
239 | $start = (int) $start; |
||
240 | |||
241 | if ($length === null) { |
||
242 | $value = strcspn($this->value, (string) $mask, $start); |
||
243 | } else { |
||
244 | $value = strcspn( |
||
245 | $this->value, |
||
246 | (string) $mask, |
||
247 | $start, |
||
248 | (int) $length |
||
249 | ); |
||
250 | } |
||
251 | |||
252 | return new IntegerPrimitive($value); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Repeat the string n times |
||
257 | * |
||
258 | * @param int $repeat |
||
259 | * |
||
260 | * @return StringPrimitive |
||
261 | */ |
||
262 | public function repeat($repeat) |
||
263 | { |
||
264 | return new self(str_repeat($this->value, (int) $repeat)); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Shuffle the string |
||
269 | * |
||
270 | * @return StringPrimitive |
||
271 | */ |
||
272 | public function shuffle() |
||
273 | { |
||
274 | return new self(str_shuffle($this->value)); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Strip slashes |
||
279 | * |
||
280 | * @return StringPrimitive |
||
281 | */ |
||
282 | public function stripSlashes() |
||
283 | { |
||
284 | return new self(stripslashes($this->value)); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Strip C-like slashes |
||
289 | * |
||
290 | * @return StringPrimitive |
||
291 | */ |
||
292 | public function stripCSlashes() |
||
293 | { |
||
294 | return new self(stripcslashes($this->value)); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Return the word count |
||
299 | * |
||
300 | * @param string $charlist |
||
301 | * |
||
302 | * @return IntegerPrimitive |
||
303 | */ |
||
304 | public function wordCount($charlist = '') |
||
305 | { |
||
306 | return new IntegerPrimitive(str_word_count( |
||
307 | $this->value, |
||
308 | 0, |
||
309 | (string) $charlist |
||
310 | )); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Return the collection of words |
||
315 | * |
||
316 | * @param string $charlist |
||
317 | * |
||
318 | * @return TypedCollection |
||
319 | */ |
||
320 | public function words($charlist = '') |
||
321 | { |
||
322 | return new TypedCollection( |
||
323 | self::class, |
||
324 | str_word_count($this->value, 2, (string) $charlist) |
||
325 | ); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Split the string using a regular expression |
||
330 | * |
||
331 | * @param string $regex |
||
332 | * @param int $limit |
||
333 | * @param int $flags |
||
334 | * |
||
335 | * @return TypedCollection |
||
336 | */ |
||
337 | public function pregSplit($regex, $limit = -1, $flags = 0) |
||
338 | { |
||
339 | return new TypedCollection( |
||
340 | self::class, |
||
341 | preg_split((string) $regex, $this->value, $limit, $flags) |
||
342 | ); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Check if the string match the given regular expression |
||
347 | * |
||
348 | * @param string $regex |
||
349 | * @param int $flags |
||
350 | * @param int $offset |
||
351 | * |
||
352 | * @throws Exception If the regex failed |
||
353 | * |
||
354 | * @return BooleanPrimitive |
||
355 | */ |
||
356 | View Code Duplication | public function match($regex, $flags = 0, $offset = 0) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
357 | { |
||
358 | $value = preg_match((string) $regex, $this->value, null, $flags, $offset); |
||
0 ignored issues
–
show
|
|||
359 | |||
360 | if ($value === false) { |
||
361 | throw new RegexException('', preg_last_error()); |
||
362 | } |
||
363 | |||
364 | return new BooleanPrimitive((bool) $value); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Return a collection of the elements matching the regex |
||
369 | * |
||
370 | * @param string $regex |
||
371 | * @param int $flags |
||
372 | * @param int $offset |
||
373 | * |
||
374 | * @throws Exception If the regex failed |
||
375 | * |
||
376 | * @return TypedCollection |
||
377 | */ |
||
378 | View Code Duplication | public function getMatches($regex, $flags = 0, $offset = 0) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
379 | { |
||
380 | $matches = []; |
||
381 | $value = preg_match( |
||
382 | (string) $regex, |
||
383 | $this->value, |
||
384 | $matches, |
||
385 | $flags, |
||
386 | $offset |
||
387 | ); |
||
388 | |||
389 | if ($value === false) { |
||
390 | throw new RegexException('', preg_last_error()); |
||
391 | } |
||
392 | |||
393 | return new TypedCollection(self::class, $matches); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Replace part of the string by using a regular expression |
||
398 | * |
||
399 | * @param string $regex |
||
400 | * @param string $replacement |
||
401 | * @param int $limit |
||
402 | * |
||
403 | * @throws Exception If the regex failed |
||
404 | * |
||
405 | * @return StringPrimitive |
||
406 | */ |
||
407 | public function pregReplace($regex, $replacement, $limit = -1) |
||
408 | { |
||
409 | $value = preg_replace( |
||
410 | (string) $regex, |
||
411 | (string) $replacement, |
||
412 | $this->value, |
||
413 | $limit |
||
414 | ); |
||
415 | |||
416 | if ($value === null) { |
||
417 | throw new RegexException('', preg_last_error()); |
||
418 | } |
||
419 | |||
420 | return new self($value); |
||
421 | } |
||
422 | } |
||
423 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.