Completed
Push — master ( 4c4f45...d696a2 )
by Lars
03:10
created

Stringy::removeXss()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
3
namespace Stringy;
4
5
use voku\helper\AntiXSS;
6
use voku\helper\EmailCheck;
7
use voku\helper\URLify;
8
use voku\helper\UTF8;
9
10
/**
11
 * Class Stringy
12
 *
13
 * @package Stringy
14
 */
15
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
16
{
17
  /**
18
   * An instance's string.
19
   *
20
   * @var string
21
   */
22
  protected $str;
23
24
  /**
25
   * The string's encoding, which should be one of the mbstring module's
26
   * supported encodings.
27
   *
28
   * @var string
29
   */
30
  protected $encoding;
31
32
  /**
33
   * Initializes a Stringy object and assigns both str and encoding properties
34
   * the supplied values. $str is cast to a string prior to assignment, and if
35
   * $encoding is not specified, it defaults to mb_internal_encoding(). Throws
36
   * an InvalidArgumentException if the first argument is an array or object
37
   * without a __toString method.
38
   *
39
   * @param  mixed  $str      Value to modify, after being cast to string
40
   * @param  string $encoding The character encoding
41
   *
42
   * @throws \InvalidArgumentException if an array or object without a
43
   *         __toString method is passed as the first argument
44
   */
45 1061
  public function __construct($str = '', $encoding = null)
46
  {
47 1061
    if (is_array($str)) {
48 1
      throw new \InvalidArgumentException(
49 1
          'Passed value cannot be an array'
50
      );
51 1060
    } elseif (is_object($str) && !method_exists($str, '__toString')) {
52 1
      throw new \InvalidArgumentException(
53 1
          'Passed object must have a __toString method'
54
      );
55
    }
56
57
    // don't throw a notice on PHP 5.3
58 1059
    if (!defined('ENT_SUBSTITUTE')) {
59
      define('ENT_SUBSTITUTE', 8);
60
    }
61
62
    // init
63 1059
    UTF8::checkForSupport();
64
65 1059
    $this->str = (string)$str;
66
67 1059
    if ($encoding) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $encoding of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68 846
      $this->encoding = $encoding;
69
    } else {
70 691
      UTF8::mbstring_loaded();
71 691
      $this->encoding = mb_internal_encoding();
72
    }
73
74 1059
    if ($encoding) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $encoding of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
75 846
      $this->encoding = $encoding;
76
    } else {
77 691
      $this->encoding = mb_internal_encoding();
78
    }
79 1059
  }
80
81
  /**
82
   * Returns the value in $str.
83
   *
84
   * @return string The current value of the $str property
85
   */
86 150
  public function __toString()
87
  {
88 150
    return (string)$this->str;
89
  }
90
91
  /**
92
   * Returns a new string with $string appended.
93
   *
94
   * @param  string $string The string to append
95
   *
96
   * @return Stringy Object with appended $string
97
   */
98 5
  public function append($string)
99
  {
100 5
    return static::create($this->str . $string, $this->encoding);
101
  }
102
103
  /**
104
   * Append an password (limited to chars that are good readable).
105
   *
106
   * @param int $length length of the random string
107
   *
108
   * @return Stringy Object with appended password
109
   */
110 1
  public function appendPassword($length)
111
  {
112 1
    $possibleChars = '2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ';
113
114 1
    return $this->appendRandomString($length, $possibleChars);
115
  }
116
117
  /**
118
   * Append an unique identifier.
119
   *
120
   * @param string|int $extraPrefix
121
   *
122
   * @return Stringy Object with appended unique identifier as md5-hash
123
   */
124 1
  public function appendUniqueIdentifier($extraPrefix = '')
0 ignored issues
show
Coding Style introduced by
appendUniqueIdentifier uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
125
  {
126 1
    $prefix = mt_rand() .
127 1
              session_id() .
128 1
              (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') .
129 1
              (isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '') .
130 1
              $extraPrefix;
131
132 1
    return $this->append(md5(uniqid($prefix, true) . $prefix));
133
  }
134
135
  /**
136
   * Append an random string.
137
   *
138
   * @param int    $length        length of the random string
139
   * @param string $possibleChars characters string for the random selection
140
   *
141
   * @return Stringy Object with appended random string
142
   */
143 2
  public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
144
  {
145
    // init
146 2
    $i = 0;
147 2
    $length = (int)$length;
148 2
    $str = $this->str;
149 2
    $maxlength = UTF8::strlen($possibleChars, $this->encoding);
150
151 2
    if ($maxlength === 0) {
152 1
      return $this;
153
    }
154
155
    // add random chars
156 2
    while ($i < $length) {
157 2
      $char = UTF8::substr($possibleChars, mt_rand(0, $maxlength - 1), 1, $this->encoding);
158 2
      $str .= $char;
159 2
      $i++;
160
    }
161
162 2
    return $this->append($str);
163
  }
164
165
  /**
166
   * Creates a Stringy object and assigns both str and encoding properties
167
   * the supplied values. $str is cast to a string prior to assignment, and if
168
   * $encoding is not specified, it defaults to mb_internal_encoding(). It
169
   * then returns the initialized object. Throws an InvalidArgumentException
170
   * if the first argument is an array or object without a __toString method.
171
   *
172
   * @param  mixed  $str      Value to modify, after being cast to string
173
   * @param  string $encoding The character encoding
174
   *
175
   * @return Stringy A Stringy object
176
   * @throws \InvalidArgumentException if an array or object without a
177
   *         __toString method is passed as the first argument
178
   */
179 1051
  public static function create($str = '', $encoding = null)
180
  {
181 1051
    return new static($str, $encoding);
182
  }
183
184
  /**
185
   * Returns the substring between $start and $end, if found, or an empty
186
   * string. An optional offset may be supplied from which to begin the
187
   * search for the start string.
188
   *
189
   * @param  string $start  Delimiter marking the start of the substring
190
   * @param  string $end    Delimiter marking the end of the substring
191
   * @param  int    $offset Index from which to begin the search
192
   *
193
   * @return Stringy Object whose $str is a substring between $start and $end
194
   */
195 16
  public function between($start, $end, $offset = 0)
196
  {
197 16
    $startIndex = $this->indexOf($start, $offset);
198 16
    if ($startIndex === false) {
199 2
      return static::create('', $this->encoding);
200
    }
201
202 14
    $substrIndex = $startIndex + UTF8::strlen($start, $this->encoding);
203 14
    $endIndex = $this->indexOf($end, $substrIndex);
204 14
    if ($endIndex === false) {
205 2
      return static::create('', $this->encoding);
206
    }
207
208 12
    return $this->substr($substrIndex, $endIndex - $substrIndex);
209
  }
210
211
  /**
212
   * Returns the index of the first occurrence of $needle in the string,
213
   * and false if not found. Accepts an optional offset from which to begin
214
   * the search.
215
   *
216
   * @param  string $needle Substring to look for
217
   * @param  int    $offset Offset from which to search
218
   *
219
   * @return int|bool The occurrence's index if found, otherwise false
220
   */
221 28
  public function indexOf($needle, $offset = 0)
222
  {
223 28
    return UTF8::strpos($this->str, (string)$needle, (int)$offset, $this->encoding);
224
  }
225
226
  /**
227
   * Returns the substring beginning at $start with the specified $length.
228
   * It differs from the UTF8::substr() function in that providing a $length of
229
   * null will return the rest of the string, rather than an empty string.
230
   *
231
   * @param  int $start  Position of the first character to use
232
   * @param  int $length Maximum number of characters used
233
   *
234
   * @return Stringy Object with its $str being the substring
235
   */
236 64
  public function substr($start, $length = null)
237
  {
238 64
    if ($length === null) {
239 19
      $length = $this->length();
240
    }
241
242 64
    $str = UTF8::substr($this->str, $start, $length, $this->encoding);
243
244 64
    return static::create($str, $this->encoding);
245
  }
246
247
  /**
248
   * Returns the length of the string.
249
   *
250
   * @return int The number of characters in $str given the encoding
251
   */
252 248
  public function length()
253
  {
254 248
    return UTF8::strlen($this->str, $this->encoding);
255
  }
256
257
  /**
258
   * Trims the string and replaces consecutive whitespace characters with a
259
   * single space. This includes tabs and newline characters, as well as
260
   * multibyte whitespace such as the thin space and ideographic space.
261
   *
262
   * @return Stringy Object with a trimmed $str and condensed whitespace
263
   */
264 52
  public function collapseWhitespace()
265
  {
266 52
    return $this->regexReplace('[[:space:]]+', ' ')->trim();
267
  }
268
269
  /**
270
   * Returns a string with whitespace removed from the start and end of the
271
   * string. Supports the removal of unicode whitespace. Accepts an optional
272
   * string of characters to strip instead of the defaults.
273
   *
274
   * @param  string $chars Optional string of characters to strip
275
   *
276
   * @return Stringy Object with a trimmed $str
277
   */
278 153 View Code Duplication
  public function trim($chars = null)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
279
  {
280 153
    if (!$chars) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $chars of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
281 152
      $chars = '[:space:]';
282
    } else {
283 1
      $chars = preg_quote($chars, '/');
284
    }
285
286 153
    return $this->regexReplace("^[$chars]+|[$chars]+\$", '');
287
  }
288
289
  /**
290
   * Replaces all occurrences of $pattern in $str by $replacement.
291
   *
292
   * @param  string $pattern     The regular expression pattern
293
   * @param  string $replacement The string to replace with
294
   * @param  string $options     Matching conditions to be used
295
   *
296
   * @return Stringy Object with the result2ing $str after the replacements
297
   */
298 223
  public function regexReplace($pattern, $replacement, $options = '')
299
  {
300 223
    if ($options === 'msr') {
301 8
      $options = 'ms';
302
    }
303
304 223
    $str = preg_replace(
305 223
        '/' . $pattern . '/u' . $options,
306
        $replacement,
307 223
        $this->str
308
    );
309
310 223
    return static::create($str, $this->encoding);
311
  }
312
313
  /**
314
   * Returns true if the string contains all $needles, false otherwise. By
315
   * default the comparison is case-sensitive, but can be made insensitive by
316
   * setting $caseSensitive to false.
317
   *
318
   * @param  array $needles       SubStrings to look for
319
   * @param  bool  $caseSensitive Whether or not to enforce case-sensitivity
320
   *
321
   * @return bool   Whether or not $str contains $needle
322
   */
323 43 View Code Duplication
  public function containsAll($needles, $caseSensitive = true)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
324
  {
325
    /** @noinspection IsEmptyFunctionUsageInspection */
326 43
    if (empty($needles)) {
327 1
      return false;
328
    }
329
330 42
    foreach ($needles as $needle) {
331 42
      if (!$this->contains($needle, $caseSensitive)) {
332 42
        return false;
333
      }
334
    }
335
336 24
    return true;
337
  }
338
339
  /**
340
   * Returns true if the string contains $needle, false otherwise. By default
341
   * the comparison is case-sensitive, but can be made insensitive by setting
342
   * $caseSensitive to false.
343
   *
344
   * @param  string $needle        Substring to look for
345
   * @param  bool   $caseSensitive Whether or not to enforce case-sensitivity
346
   *
347
   * @return bool   Whether or not $str contains $needle
348
   */
349 105
  public function contains($needle, $caseSensitive = true)
350
  {
351 105
    $encoding = $this->encoding;
352
353 105
    if ($caseSensitive) {
354 55
      return (UTF8::strpos($this->str, $needle, 0, $encoding) !== false);
355
    } else {
356 50
      return (UTF8::stripos($this->str, $needle, 0, $encoding) !== false);
357
    }
358
  }
359
360
  /**
361
   * Returns true if the string contains any $needles, false otherwise. By
362
   * default the comparison is case-sensitive, but can be made insensitive by
363
   * setting $caseSensitive to false.
364
   *
365
   * @param  array $needles       SubStrings to look for
366
   * @param  bool  $caseSensitive Whether or not to enforce case-sensitivity
367
   *
368
   * @return bool   Whether or not $str contains $needle
369
   */
370 43 View Code Duplication
  public function containsAny($needles, $caseSensitive = true)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
371
  {
372
    /** @noinspection IsEmptyFunctionUsageInspection */
373 43
    if (empty($needles)) {
374 1
      return false;
375
    }
376
377 42
    foreach ($needles as $needle) {
378 42
      if ($this->contains($needle, $caseSensitive)) {
379 42
        return true;
380
      }
381
    }
382
383 18
    return false;
384
  }
385
386
  /**
387
   * Returns the length of the string, implementing the countable interface.
388
   *
389
   * @return int The number of characters in the string, given the encoding
390
   */
391 1
  public function count()
392
  {
393 1
    return $this->length();
394
  }
395
396
  /**
397
   * Returns the number of occurrences of $substring in the given string.
398
   * By default, the comparison is case-sensitive, but can be made insensitive
399
   * by setting $caseSensitive to false.
400
   *
401
   * @param  string $substring     The substring to search for
402
   * @param  bool   $caseSensitive Whether or not to enforce case-sensitivity
403
   *
404
   * @return int    The number of $substring occurrences
405
   */
406 15
  public function countSubstr($substring, $caseSensitive = true)
407
  {
408 15
    if ($caseSensitive) {
409 9
      return UTF8::substr_count($this->str, $substring, 0, null, $this->encoding);
410
    }
411
412 6
    $str = UTF8::strtoupper($this->str, $this->encoding);
413 6
    $substring = UTF8::strtoupper($substring, $this->encoding);
414
415 6
    return UTF8::substr_count($str, $substring, 0, null, $this->encoding);
416
  }
417
418
  /**
419
   * Returns a lowercase and trimmed string separated by dashes. Dashes are
420
   * inserted before uppercase characters (with the exception of the first
421
   * character of the string), and in place of spaces as well as underscores.
422
   *
423
   * @return Stringy Object with a dasherized $str
424
   */
425 19
  public function dasherize()
426
  {
427 19
    return $this->delimit('-');
428
  }
429
430
  /**
431
   * Returns a lowercase and trimmed string separated by the given delimiter.
432
   * Delimiters are inserted before uppercase characters (with the exception
433
   * of the first character of the string), and in place of spaces, dashes,
434
   * and underscores. Alpha delimiters are not converted to lowercase.
435
   *
436
   * @param  string $delimiter Sequence used to separate parts of the string
437
   *
438
   * @return Stringy Object with a delimited $str
439
   */
440 49
  public function delimit($delimiter)
441
  {
442 49
    $str = $this->trim();
443
444 49
    $str = preg_replace('/\B([A-Z])/u', '-\1', $str);
445
446 49
    $str = UTF8::strtolower($str, $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $str can also be of type array<integer,string>; however, voku\helper\UTF8::strtolower() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
447
448 49
    $str = preg_replace('/[-_\s]+/u', $delimiter, $str);
449
450 49
    return static::create($str, $this->encoding);
451
  }
452
453
  /**
454
   * Ensures that the string begins with $substring. If it doesn't, it's
455
   * prepended.
456
   *
457
   * @param  string $substring The substring to add if not present
458
   *
459
   * @return Stringy Object with its $str prefixed by the $substring
460
   */
461 10 View Code Duplication
  public function ensureLeft($substring)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
462
  {
463 10
    $stringy = static::create($this->str, $this->encoding);
464
465 10
    if (!$stringy->startsWith($substring)) {
466 4
      $stringy->str = $substring . $stringy->str;
467
    }
468
469 10
    return $stringy;
470
  }
471
472
  /**
473
   * Returns true if the string begins with $substring, false otherwise. By
474
   * default, the comparison is case-sensitive, but can be made insensitive
475
   * by setting $caseSensitive to false.
476
   *
477
   * @param  string $substring     The substring to look for
478
   * @param  bool   $caseSensitive Whether or not to enforce case-sensitivity
479
   *
480
   * @return bool   Whether or not $str starts with $substring
481
   */
482 33
  public function startsWith($substring, $caseSensitive = true)
483
  {
484 33
    $str = $this->str;
485
486 33 View Code Duplication
    if (!$caseSensitive) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
487 4
      $substring = UTF8::strtolower($substring, $this->encoding);
488 4
      $str = UTF8::strtolower($this->str, $this->encoding);
489
    }
490
491 33
    return UTF8::strpos($str, $substring, $this->encoding) === 0;
492
  }
493
494
  /**
495
   * Ensures that the string ends with $substring. If it doesn't, it's
496
   * appended.
497
   *
498
   * @param  string $substring The substring to add if not present
499
   *
500
   * @return Stringy Object with its $str suffixed by the $substring
501
   */
502 10 View Code Duplication
  public function ensureRight($substring)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
503
  {
504 10
    $stringy = static::create($this->str, $this->encoding);
505
506 10
    if (!$stringy->endsWith($substring)) {
507 4
      $stringy->str .= $substring;
508
    }
509
510 10
    return $stringy;
511
  }
512
513
  /**
514
   * Returns true if the string ends with $substring, false otherwise. By
515
   * default, the comparison is case-sensitive, but can be made insensitive
516
   * by setting $caseSensitive to false.
517
   *
518
   * @param  string $substring     The substring to look for
519
   * @param  bool   $caseSensitive Whether or not to enforce case-sensitivity
520
   *
521
   * @return bool   Whether or not $str ends with $substring
522
   */
523 33
  public function endsWith($substring, $caseSensitive = true)
524
  {
525 33
    $substringLength = UTF8::strlen($substring, $this->encoding);
526 33
    $strLength = $this->length();
527
528 33
    $endOfStr = UTF8::substr(
529 33
        $this->str,
530 33
        $strLength - $substringLength,
531
        $substringLength,
532 33
        $this->encoding
533
    );
534
535 33 View Code Duplication
    if (!$caseSensitive) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
536 4
      $substring = UTF8::strtolower($substring, $this->encoding);
537 4
      $endOfStr = UTF8::strtolower($endOfStr, $this->encoding);
0 ignored issues
show
Security Bug introduced by
It seems like $endOfStr can also be of type false; however, voku\helper\UTF8::strtolower() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
538
    }
539
540 33
    return (string)$substring === $endOfStr;
541
  }
542
543
  /**
544
   * Returns the first $n characters of the string.
545
   *
546
   * @param  int $n Number of characters to retrieve from the start
547
   *
548
   * @return Stringy Object with its $str being the first $n chars
549
   */
550 12 View Code Duplication
  public function first($n)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
551
  {
552 12
    $stringy = static::create($this->str, $this->encoding);
553
554 12
    if ($n < 0) {
555 2
      $stringy->str = '';
556
    } else {
557 10
      return $stringy->substr(0, $n);
558
    }
559
560 2
    return $stringy;
561
  }
562
563
  /**
564
   * Returns the encoding used by the Stringy object.
565
   *
566
   * @return string The current value of the $encoding property
567
   */
568 3
  public function getEncoding()
569
  {
570 3
    return $this->encoding;
571
  }
572
573
  /**
574
   * Returns a new ArrayIterator, thus implementing the IteratorAggregate
575
   * interface. The ArrayIterator's constructor is passed an array of chars
576
   * in the multibyte string. This enables the use of foreach with instances
577
   * of Stringy\Stringy.
578
   *
579
   * @return \ArrayIterator An iterator for the characters in the string
580
   */
581 1
  public function getIterator()
582
  {
583 1
    return new \ArrayIterator($this->chars());
584
  }
585
586
  /**
587
   * Returns an array consisting of the characters in the string.
588
   *
589
   * @return array An array of string chars
590
   */
591 4
  public function chars()
592
  {
593
    // init
594 4
    $chars = array();
595 4
    $l = $this->length();
596
597 4
    for ($i = 0; $i < $l; $i++) {
598 3
      $chars[] = $this->at($i)->str;
599
    }
600
601 4
    return $chars;
602
  }
603
604
  /**
605
   * Returns the character at $index, with indexes starting at 0.
606
   *
607
   * @param  int $index Position of the character
608
   *
609
   * @return Stringy The character at $index
610
   */
611 11
  public function at($index)
612
  {
613 11
    return $this->substr($index, 1);
614
  }
615
616
  /**
617
   * Returns true if the string contains a lower case char, false
618
   * otherwise.
619
   *
620
   * @return bool Whether or not the string contains a lower case character.
621
   */
622 12
  public function hasLowerCase()
623
  {
624 12
    return $this->matchesPattern('.*[[:lower:]]');
625
  }
626
627
  /**
628
   * Returns true if $str matches the supplied pattern, false otherwise.
629
   *
630
   * @param  string $pattern Regex pattern to match against
631
   *
632
   * @return bool   Whether or not $str matches the pattern
633
   */
634 91
  private function matchesPattern($pattern)
635
  {
636 91
    if (preg_match('/' . $pattern . '/u', $this->str)) {
637 54
      return true;
638
    } else {
639 37
      return false;
640
    }
641
  }
642
643
  /**
644
   * Returns true if the string contains an upper case char, false
645
   * otherwise.
646
   *
647
   * @return bool Whether or not the string contains an upper case character.
648
   */
649 12
  public function hasUpperCase()
650
  {
651 12
    return $this->matchesPattern('.*[[:upper:]]');
652
  }
653
654
  /**
655
   * Convert all HTML entities to their applicable characters.
656
   *
657
   * @param  int|null $flags Optional flags
658
   *
659
   * @return Stringy  Object with the resulting $str after being html decoded.
660
   */
661 5
  public function htmlDecode($flags = ENT_COMPAT)
662
  {
663 5
    $str = UTF8::html_entity_decode($this->str, $flags, $this->encoding);
664
665 5
    return static::create($str, $this->encoding);
666
  }
667
668
  /**
669
   * Convert all applicable characters to HTML entities.
670
   *
671
   * @param  int|null $flags Optional flags
672
   *
673
   * @return Stringy  Object with the resulting $str after being html encoded.
674
   */
675 5
  public function htmlEncode($flags = ENT_COMPAT)
676
  {
677 5
    $str = UTF8::htmlentities($this->str, $flags, $this->encoding);
678
679 5
    return static::create($str, $this->encoding);
680
  }
681
682
  /**
683
   * Capitalizes the first word of the string, replaces underscores with
684
   * spaces, and strips '_id'.
685
   *
686
   * @return Stringy Object with a humanized $str
687
   */
688 3
  public function humanize()
689
  {
690 3
    $str = UTF8::str_replace(array('_id', '_'), array('', ' '), $this->str);
691
692 3
    return static::create($str, $this->encoding)->trim()->upperCaseFirst();
693
  }
694
695
  /**
696
   * Converts the first character of the supplied string to upper case.
697
   *
698
   * @return Stringy Object with the first character of $str being upper case
699
   */
700 27 View Code Duplication
  public function upperCaseFirst()
0 ignored issues
show
Duplication introduced by
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.

Loading history...
701
  {
702 27
    $first = UTF8::substr($this->str, 0, 1, $this->encoding);
703 27
    $rest = UTF8::substr(
704 27
        $this->str,
705 27
        1,
706 27
        $this->length() - 1,
707 27
        $this->encoding
708
    );
709
710 27
    $str = UTF8::strtoupper($first, $this->encoding) . $rest;
0 ignored issues
show
Security Bug introduced by
It seems like $first defined by \voku\helper\UTF8::subst... 0, 1, $this->encoding) on line 702 can also be of type false; however, voku\helper\UTF8::strtoupper() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
711
712 27
    return static::create($str, $this->encoding);
713
  }
714
715
  /**
716
   * Returns the index of the last occurrence of $needle in the string,
717
   * and false if not found. Accepts an optional offset from which to begin
718
   * the search. Offsets may be negative to count from the last character
719
   * in the string.
720
   *
721
   * @param  string $needle Substring to look for
722
   * @param  int    $offset Offset from which to search
723
   *
724
   * @return int|bool The last occurrence's index if found, otherwise false
725
   */
726 12
  public function indexOfLast($needle, $offset = 0)
727
  {
728 12
    return UTF8::strrpos($this->str, (string)$needle, (int)$offset, $this->encoding);
729
  }
730
731
  /**
732
   * Inserts $substring into the string at the $index provided.
733
   *
734
   * @param  string $substring String to be inserted
735
   * @param  int    $index     The index at which to insert the substring
736
   *
737
   * @return Stringy Object with the resulting $str after the insertion
738
   */
739 8 View Code Duplication
  public function insert($substring, $index)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
740
  {
741 8
    $stringy = static::create($this->str, $this->encoding);
742 8
    if ($index > $stringy->length()) {
743 1
      return $stringy;
744
    }
745
746 7
    $start = UTF8::substr($stringy->str, 0, $index, $stringy->encoding);
747 7
    $end = UTF8::substr($stringy->str, $index, $stringy->length(), $stringy->encoding);
748
749 7
    $stringy->str = $start . $substring . $end;
750
751 7
    return $stringy;
752
  }
753
754
  /**
755
   * Returns true if the string contains only alphabetic chars, false
756
   * otherwise.
757
   *
758
   * @return bool Whether or not $str contains only alphabetic chars
759
   */
760 10
  public function isAlpha()
761
  {
762 10
    return $this->matchesPattern('^[[:alpha:]]*$');
763
  }
764
765
  /**
766
   * Determine whether the string is considered to be empty.
767
   *
768
   * A variable is considered empty if it does not exist or if its value equals FALSE.
769
   * empty() does not generate a warning if the variable does not exist.
770
   *
771
   * @return bool
772
   */
773
  public function isEmpty()
774
  {
775
    return empty($this->str);
776
  }
777
778
  /**
779
   * Returns true if the string contains only alphabetic and numeric chars,
780
   * false otherwise.
781
   *
782
   * @return bool Whether or not $str contains only alphanumeric chars
783
   */
784 13
  public function isAlphanumeric()
785
  {
786 13
    return $this->matchesPattern('^[[:alnum:]]*$');
787
  }
788
789
  /**
790
   * Returns true if the string contains only whitespace chars, false
791
   * otherwise.
792
   *
793
   * @return bool Whether or not $str contains only whitespace characters
794
   */
795 15
  public function isBlank()
796
  {
797 15
    return $this->matchesPattern('^[[:space:]]*$');
798
  }
799
800
  /**
801
   * Returns true if the string contains only hexadecimal chars, false
802
   * otherwise.
803
   *
804
   * @return bool Whether or not $str contains only hexadecimal chars
805
   */
806 13
  public function isHexadecimal()
807
  {
808 13
    return $this->matchesPattern('^[[:xdigit:]]*$');
809
  }
810
811
  /**
812
   * Returns true if the string contains HTML-Tags, false otherwise.
813
   *
814
   * @return bool Whether or not $str contains HTML-Tags
815
   */
816 1
  public function isHtml()
817
  {
818 1
    return UTF8::isHtml($this->str);
0 ignored issues
show
Deprecated Code introduced by
The method voku\helper\UTF8::isHtml() has been deprecated.

This method has been deprecated.

Loading history...
819
  }
820
821
  /**
822
   * Returns true if the string contains a valid E-Mail address, false otherwise.
823
   *
824
   * @param bool $useExampleDomainCheck
825
   * @param bool $useTypoInDomainCheck
826
   * @param bool $useTemporaryDomainCheck
827
   * @param bool $useDnsCheck
828
   *
829
   * @return bool Whether or not $str contains a valid E-Mail address
830
   */
831 1
  public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false)
832
  {
833 1
    return EmailCheck::isValid($this->str, $useExampleDomainCheck, $useTypoInDomainCheck, $useTemporaryDomainCheck, $useDnsCheck);
834
  }
835
836
  /**
837
   * Returns true if the string is JSON, false otherwise. Unlike json_decode
838
   * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers,
839
   * in that an empty string is not considered valid JSON.
840
   *
841
   * @return bool Whether or not $str is JSON
842
   */
843 20
  public function isJson()
844
  {
845 20
    if (!isset($this->str[0])) {
846 1
      return false;
847
    }
848
849 19
    json_decode($this->str);
850
851 19
    if (json_last_error() === JSON_ERROR_NONE) {
852 11
      return true;
853
    } else {
854 8
      return false;
855
    }
856
  }
857
858
  /**
859
   * Returns true if the string contains only lower case chars, false
860
   * otherwise.
861
   *
862
   * @return bool Whether or not $str contains only lower case characters
863
   */
864 8
  public function isLowerCase()
865
  {
866 8
    if ($this->matchesPattern('^[[:lower:]]*$')) {
867 3
      return true;
868
    } else {
869 5
      return false;
870
    }
871
  }
872
873
  /**
874
   * Returns true if the string is serialized, false otherwise.
875
   *
876
   * @return bool Whether or not $str is serialized
877
   */
878 7
  public function isSerialized()
879
  {
880 7
    if (!isset($this->str[0])) {
881 1
      return false;
882
    }
883
884
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
885
    if (
886 6
        $this->str === 'b:0;'
887
        ||
888 6
        @unserialize($this->str) !== false
889
    ) {
890 4
      return true;
891
    } else {
892 2
      return false;
893
    }
894
  }
895
896
  /**
897
   * Returns true if the string contains only lower case chars, false
898
   * otherwise.
899
   *
900
   * @return bool Whether or not $str contains only lower case characters
901
   */
902 8
  public function isUpperCase()
903
  {
904 8
    return $this->matchesPattern('^[[:upper:]]*$');
905
  }
906
907
  /**
908
   * Returns the last $n characters of the string.
909
   *
910
   * @param  int $n Number of characters to retrieve from the end
911
   *
912
   * @return Stringy Object with its $str being the last $n chars
913
   */
914 12 View Code Duplication
  public function last($n)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
915
  {
916 12
    $stringy = static::create($this->str, $this->encoding);
917
918 12
    if ($n <= 0) {
919 4
      $stringy->str = '';
920
    } else {
921 8
      return $stringy->substr(-$n);
922
    }
923
924 4
    return $stringy;
925
  }
926
927
  /**
928
   * Splits on newlines and carriage returns, returning an array of Stringy
929
   * objects corresponding to the lines in the string.
930
   *
931
   * @return Stringy[] An array of Stringy objects
932
   */
933 15
  public function lines()
934
  {
935 15
    $array = preg_split('/[\r\n]{1,2}/u', $this->str);
936
    /** @noinspection CallableInLoopTerminationConditionInspection */
937
    /** @noinspection ForeachInvariantsInspection */
938 15 View Code Duplication
    for ($i = 0; $i < count($array); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
939 15
      $array[$i] = static::create($array[$i], $this->encoding);
940
    }
941
942 15
    return $array;
943
  }
944
945
  /**
946
   * Returns the longest common prefix between the string and $otherStr.
947
   *
948
   * @param  string $otherStr Second string for comparison
949
   *
950
   * @return Stringy Object with its $str being the longest common prefix
951
   */
952 10
  public function longestCommonPrefix($otherStr)
953
  {
954 10
    $encoding = $this->encoding;
955 10
    $maxLength = min($this->length(), UTF8::strlen($otherStr, $encoding));
956
957 10
    $longestCommonPrefix = '';
958 10
    for ($i = 0; $i < $maxLength; $i++) {
959 8
      $char = UTF8::substr($this->str, $i, 1, $encoding);
960
961 8
      if ($char == UTF8::substr($otherStr, $i, 1, $encoding)) {
962 6
        $longestCommonPrefix .= $char;
963
      } else {
964 6
        break;
965
      }
966
    }
967
968 10
    return static::create($longestCommonPrefix, $encoding);
969
  }
970
971
  /**
972
   * Returns the longest common suffix between the string and $otherStr.
973
   *
974
   * @param  string $otherStr Second string for comparison
975
   *
976
   * @return Stringy Object with its $str being the longest common suffix
977
   */
978 10
  public function longestCommonSuffix($otherStr)
979
  {
980 10
    $encoding = $this->encoding;
981 10
    $maxLength = min($this->length(), UTF8::strlen($otherStr, $encoding));
982
983 10
    $longestCommonSuffix = '';
984 10
    for ($i = 1; $i <= $maxLength; $i++) {
985 8
      $char = UTF8::substr($this->str, -$i, 1, $encoding);
986
987 8
      if ($char == UTF8::substr($otherStr, -$i, 1, $encoding)) {
988 6
        $longestCommonSuffix = $char . $longestCommonSuffix;
989
      } else {
990 6
        break;
991
      }
992
    }
993
994 10
    return static::create($longestCommonSuffix, $encoding);
995
  }
996
997
  /**
998
   * Returns the longest common substring between the string and $otherStr.
999
   * In the case of ties, it returns that which occurs first.
1000
   *
1001
   * @param  string $otherStr Second string for comparison
1002
   *
1003
   * @return Stringy Object with its $str being the longest common substring
1004
   */
1005 10
  public function longestCommonSubstring($otherStr)
1006
  {
1007
    // Uses dynamic programming to solve
1008
    // http://en.wikipedia.org/wiki/Longest_common_substring_problem
1009 10
    $encoding = $this->encoding;
1010 10
    $stringy = static::create($this->str, $encoding);
1011 10
    $strLength = $stringy->length();
1012 10
    $otherLength = UTF8::strlen($otherStr, $encoding);
1013
1014
    // Return if either string is empty
1015 10
    if ($strLength == 0 || $otherLength == 0) {
1016 2
      $stringy->str = '';
1017
1018 2
      return $stringy;
1019
    }
1020
1021 8
    $len = 0;
1022 8
    $end = 0;
1023 8
    $table = array_fill(
1024 8
        0, $strLength + 1,
1025 8
        array_fill(0, $otherLength + 1, 0)
1026
    );
1027
1028 8
    for ($i = 1; $i <= $strLength; $i++) {
1029 8
      for ($j = 1; $j <= $otherLength; $j++) {
1030 8
        $strChar = UTF8::substr($stringy->str, $i - 1, 1, $encoding);
1031 8
        $otherChar = UTF8::substr($otherStr, $j - 1, 1, $encoding);
1032
1033 8
        if ($strChar == $otherChar) {
1034 8
          $table[$i][$j] = $table[$i - 1][$j - 1] + 1;
1035 8
          if ($table[$i][$j] > $len) {
1036 8
            $len = $table[$i][$j];
1037 8
            $end = $i;
1038
          }
1039
        } else {
1040 8
          $table[$i][$j] = 0;
1041
        }
1042
      }
1043
    }
1044
1045 8
    $stringy->str = UTF8::substr($stringy->str, $end - $len, $len, $encoding);
0 ignored issues
show
Documentation Bug introduced by
It seems like \voku\helper\UTF8::subst... $len, $len, $encoding) can also be of type false. However, the property $str is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
1046
1047 8
    return $stringy;
1048
  }
1049
1050
  /**
1051
   * Returns whether or not a character exists at an index. Offsets may be
1052
   * negative to count from the last character in the string. Implements
1053
   * part of the ArrayAccess interface.
1054
   *
1055
   * @param  mixed $offset The index to check
1056
   *
1057
   * @return boolean Whether or not the index exists
1058
   */
1059 6
  public function offsetExists($offset)
1060
  {
1061
    // init
1062 6
    $length = $this->length();
1063 6
    $offset = (int)$offset;
1064
1065 6
    if ($offset >= 0) {
1066 3
      return ($length > $offset);
1067
    }
1068
1069 3
    return ($length >= abs($offset));
1070
  }
1071
1072
  /**
1073
   * Returns the character at the given index. Offsets may be negative to
1074
   * count from the last character in the string. Implements part of the
1075
   * ArrayAccess interface, and throws an OutOfBoundsException if the index
1076
   * does not exist.
1077
   *
1078
   * @param  mixed $offset The index from which to retrieve the char
1079
   *
1080
   * @return string                 The character at the specified index
1081
   * @throws \OutOfBoundsException If the positive or negative offset does
1082
   *                               not exist
1083
   */
1084 2
  public function offsetGet($offset)
1085
  {
1086
    // init
1087 2
    $offset = (int)$offset;
1088 2
    $length = $this->length();
1089
1090
    if (
1091 2
        ($offset >= 0 && $length <= $offset)
1092
        ||
1093 2
        $length < abs($offset)
1094
    ) {
1095 1
      throw new \OutOfBoundsException('No character exists at the index');
1096
    }
1097
1098 1
    return UTF8::substr($this->str, $offset, 1, $this->encoding);
1099
  }
1100
1101
  /**
1102
   * Implements part of the ArrayAccess interface, but throws an exception
1103
   * when called. This maintains the immutability of Stringy objects.
1104
   *
1105
   * @param  mixed $offset The index of the character
1106
   * @param  mixed $value  Value to set
1107
   *
1108
   * @throws \Exception When called
1109
   */
1110 1
  public function offsetSet($offset, $value)
1111
  {
1112
    // Stringy is immutable, cannot directly set char
1113 1
    throw new \Exception('Stringy object is immutable, cannot modify char');
1114
  }
1115
1116
  /**
1117
   * Implements part of the ArrayAccess interface, but throws an exception
1118
   * when called. This maintains the immutability of Stringy objects.
1119
   *
1120
   * @param  mixed $offset The index of the character
1121
   *
1122
   * @throws \Exception When called
1123
   */
1124 1
  public function offsetUnset($offset)
1125
  {
1126
    // Don't allow directly modifying the string
1127 1
    throw new \Exception('Stringy object is immutable, cannot unset char');
1128
  }
1129
1130
  /**
1131
   * Pads the string to a given length with $padStr. If length is less than
1132
   * or equal to the length of the string, no padding takes places. The
1133
   * default string used for padding is a space, and the default type (one of
1134
   * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
1135
   * if $padType isn't one of those 3 values.
1136
   *
1137
   * @param  int    $length  Desired string length after padding
1138
   * @param  string $padStr  String used to pad, defaults to space
1139
   * @param  string $padType One of 'left', 'right', 'both'
1140
   *
1141
   * @return Stringy Object with a padded $str
1142
   * @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both'
1143
   */
1144 13
  public function pad($length, $padStr = ' ', $padType = 'right')
1145
  {
1146 13
    if (!in_array($padType, array('left', 'right', 'both'), true)) {
1147 1
      throw new \InvalidArgumentException(
1148 1
          'Pad expects $padType ' . "to be one of 'left', 'right' or 'both'"
1149
      );
1150
    }
1151
1152
    switch ($padType) {
1153 12
      case 'left':
1154 3
        return $this->padLeft($length, $padStr);
1155 9
      case 'right':
1156 6
        return $this->padRight($length, $padStr);
1157
      default:
1158 3
        return $this->padBoth($length, $padStr);
1159
    }
1160
  }
1161
1162
  /**
1163
   * Returns a new string of a given length such that the beginning of the
1164
   * string is padded. Alias for pad() with a $padType of 'left'.
1165
   *
1166
   * @param  int    $length Desired string length after padding
1167
   * @param  string $padStr String used to pad, defaults to space
1168
   *
1169
   * @return Stringy String with left padding
1170
   */
1171 10
  public function padLeft($length, $padStr = ' ')
1172
  {
1173 10
    return $this->applyPadding($length - $this->length(), 0, $padStr);
1174
  }
1175
1176
  /**
1177
   * Adds the specified amount of left and right padding to the given string.
1178
   * The default character used is a space.
1179
   *
1180
   * @param  int    $left   Length of left padding
1181
   * @param  int    $right  Length of right padding
1182
   * @param  string $padStr String used to pad
1183
   *
1184
   * @return Stringy String with padding applied
1185
   */
1186 37
  private function applyPadding($left = 0, $right = 0, $padStr = ' ')
1187
  {
1188 37
    $stringy = static::create($this->str, $this->encoding);
1189
1190 37
    $length = UTF8::strlen($padStr, $stringy->encoding);
1191
1192 37
    $strLength = $stringy->length();
1193 37
    $paddedLength = $strLength + $left + $right;
1194
1195 37
    if (!$length || $paddedLength <= $strLength) {
1196 3
      return $stringy;
1197
    }
1198
1199 34
    $leftPadding = UTF8::substr(
1200 34
        UTF8::str_repeat(
1201
            $padStr,
1202 34
            ceil($left / $length)
1203
        ),
1204 34
        0,
1205
        $left,
1206 34
        $stringy->encoding
1207
    );
1208
1209 34
    $rightPadding = UTF8::substr(
1210 34
        UTF8::str_repeat(
1211
            $padStr,
1212 34
            ceil($right / $length)
1213
        ),
1214 34
        0,
1215
        $right,
1216 34
        $stringy->encoding
1217
    );
1218
1219 34
    $stringy->str = $leftPadding . $stringy->str . $rightPadding;
1220
1221 34
    return $stringy;
1222
  }
1223
1224
  /**
1225
   * Returns a new string of a given length such that the end of the string
1226
   * is padded. Alias for pad() with a $padType of 'right'.
1227
   *
1228
   * @param  int    $length Desired string length after padding
1229
   * @param  string $padStr String used to pad, defaults to space
1230
   *
1231
   * @return Stringy String with right padding
1232
   */
1233 13
  public function padRight($length, $padStr = ' ')
1234
  {
1235 13
    return $this->applyPadding(0, $length - $this->length(), $padStr);
1236
  }
1237
1238
  /**
1239
   * Returns a new string of a given length such that both sides of the
1240
   * string are padded. Alias for pad() with a $padType of 'both'.
1241
   *
1242
   * @param  int    $length Desired string length after padding
1243
   * @param  string $padStr String used to pad, defaults to space
1244
   *
1245
   * @return Stringy String with padding applied
1246
   */
1247 14
  public function padBoth($length, $padStr = ' ')
1248
  {
1249 14
    $padding = $length - $this->length();
1250
1251 14
    return $this->applyPadding(floor($padding / 2), ceil($padding / 2), $padStr);
1252
  }
1253
1254
  /**
1255
   * Returns a new string starting with $string.
1256
   *
1257
   * @param  string $string The string to append
1258
   *
1259
   * @return Stringy Object with appended $string
1260
   */
1261 2
  public function prepend($string)
1262
  {
1263 2
    return static::create($string . $this->str, $this->encoding);
1264
  }
1265
1266
  /**
1267
   * Returns a new string with the prefix $substring removed, if present.
1268
   *
1269
   * @param  string $substring The prefix to remove
1270
   *
1271
   * @return Stringy Object having a $str without the prefix $substring
1272
   */
1273 12 View Code Duplication
  public function removeLeft($substring)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
1274
  {
1275 12
    $stringy = static::create($this->str, $this->encoding);
1276
1277 12
    if ($stringy->startsWith($substring)) {
1278 6
      $substringLength = UTF8::strlen($substring, $stringy->encoding);
1279
1280 6
      return $stringy->substr($substringLength);
1281
    }
1282
1283 6
    return $stringy;
1284
  }
1285
1286
  /**
1287
   * Returns a new string with the suffix $substring removed, if present.
1288
   *
1289
   * @param  string $substring The suffix to remove
1290
   *
1291
   * @return Stringy Object having a $str without the suffix $substring
1292
   */
1293 12 View Code Duplication
  public function removeRight($substring)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
1294
  {
1295 12
    $stringy = static::create($this->str, $this->encoding);
1296
1297 12
    if ($stringy->endsWith($substring)) {
1298 8
      $substringLength = UTF8::strlen($substring, $stringy->encoding);
1299
1300 8
      return $stringy->substr(0, $stringy->length() - $substringLength);
1301
    }
1302
1303 4
    return $stringy;
1304
  }
1305
1306
  /**
1307
   * Returns a repeated string given a multiplier.
1308
   *
1309
   * @param  int $multiplier The number of times to repeat the string
1310
   *
1311
   * @return Stringy Object with a repeated str
1312
   */
1313 7
  public function repeat($multiplier)
1314
  {
1315 7
    $repeated = UTF8::str_repeat($this->str, $multiplier);
1316
1317 7
    return static::create($repeated, $this->encoding);
1318
  }
1319
1320
  /**
1321
   * Replaces all occurrences of $search in $str by $replacement.
1322
   *
1323
   * @param string $search        The needle to search for
1324
   * @param string $replacement   The string to replace with
1325
   * @param bool   $caseSensitive Whether or not to enforce case-sensitivity
1326
   *
1327
   * @return Stringy Object with the resulting $str after the replacements
1328
   */
1329 28 View Code Duplication
  public function replace($search, $replacement, $caseSensitive = true)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
1330
  {
1331 28
    if ($caseSensitive) {
1332 21
      $return = UTF8::str_replace($search, $replacement, $this->str);
1333
    } else {
1334 7
      $return = UTF8::str_ireplace($search, $replacement, $this->str);
1335
    }
1336
1337 28
    return static::create($return);
1338
  }
1339
1340
  /**
1341
   * Replaces all occurrences of $search in $str by $replacement.
1342
   *
1343
   * @param array        $search        The elements to search for
1344
   * @param string|array $replacement   The string to replace with
1345
   * @param bool         $caseSensitive Whether or not to enforce case-sensitivity
1346
   *
1347
   * @return Stringy Object with the resulting $str after the replacements
1348
   */
1349 30 View Code Duplication
  public function replaceAll(array $search, $replacement, $caseSensitive = true)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
1350
  {
1351 30
    if ($caseSensitive) {
1352 23
      $return = UTF8::str_replace($search, $replacement, $this->str);
1353
    } else {
1354 7
      $return = UTF8::str_ireplace($search, $replacement, $this->str);
1355
    }
1356
1357 30
    return static::create($return);
1358
  }
1359
1360
  /**
1361
   * Replaces all occurrences of $search from the beginning of string with $replacement
1362
   *
1363
   * @param string $search
1364
   * @param string $replacement
1365
   *
1366
   * @return Stringy Object with the resulting $str after the replacements
1367
   */
1368 16
  public function replaceBeginning($search, $replacement)
1369
  {
1370 16
    $str = $this->regexReplace('^' . preg_quote($search, '/'), UTF8::str_replace('\\', '\\\\', $replacement));
1371
1372 16
    return static::create($str, $this->encoding);
1373
  }
1374
1375
  /**
1376
   * Replaces all occurrences of $search from the ending of string with $replacement
1377
   *
1378
   * @param string $search
1379
   * @param string $replacement
1380
   *
1381
   * @return Stringy Object with the resulting $str after the replacements
1382
   */
1383 16
  public function replaceEnding($search, $replacement)
1384
  {
1385 16
    $str = $this->regexReplace(preg_quote($search, '/') . '$', UTF8::str_replace('\\', '\\\\', $replacement));
1386
1387 16
    return static::create($str, $this->encoding);
1388
  }
1389
1390
  /**
1391
   * Returns a reversed string. A multibyte version of strrev().
1392
   *
1393
   * @return Stringy Object with a reversed $str
1394
   */
1395 5
  public function reverse()
1396
  {
1397 5
    $reversed = UTF8::strrev($this->str);
1398
1399 5
    return static::create($reversed, $this->encoding);
1400
  }
1401
1402
  /**
1403
   * Truncates the string to a given length, while ensuring that it does not
1404
   * split words. If $substring is provided, and truncating occurs, the
1405
   * string is further truncated so that the substring may be appended without
1406
   * exceeding the desired length.
1407
   *
1408
   * @param  int    $length    Desired length of the truncated string
1409
   * @param  string $substring The substring to append if it can fit
1410
   *
1411
   * @return Stringy Object with the resulting $str after truncating
1412
   */
1413 23
  public function safeTruncate($length, $substring = '')
1414
  {
1415 23
    $stringy = static::create($this->str, $this->encoding);
1416 23
    if ($length >= $stringy->length()) {
1417 4
      return $stringy;
1418
    }
1419
1420
    // Need to further trim the string so we can append the substring
1421 19
    $encoding = $stringy->encoding;
1422 19
    $substringLength = UTF8::strlen($substring, $encoding);
1423 19
    $length -= $substringLength;
1424
1425 19
    $truncated = UTF8::substr($stringy->str, 0, $length, $encoding);
1426
1427
    // If the last word was truncated
1428 19
    $strPosSpace = UTF8::strpos($stringy->str, ' ', $length - 1, $encoding);
1429 19
    if ($strPosSpace != $length) {
1430
      // Find pos of the last occurrence of a space, get up to that
1431 12
      $lastPos = UTF8::strrpos($truncated, ' ', 0, $encoding);
0 ignored issues
show
Security Bug introduced by
It seems like $truncated defined by \voku\helper\UTF8::subst... 0, $length, $encoding) on line 1425 can also be of type false; however, voku\helper\UTF8::strrpos() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1432
1433 12
      if ($lastPos !== false || $strPosSpace !== false) {
1434 11
        $truncated = UTF8::substr($truncated, 0, $lastPos, $encoding);
0 ignored issues
show
Security Bug introduced by
It seems like $truncated defined by \voku\helper\UTF8::subst...0, $lastPos, $encoding) on line 1434 can also be of type false; however, voku\helper\UTF8::substr() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Bug introduced by
It seems like $lastPos defined by \voku\helper\UTF8::strrp...ted, ' ', 0, $encoding) on line 1431 can also be of type double or false; however, voku\helper\UTF8::substr() does only seem to accept integer|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1435
      }
1436
    }
1437
1438 19
    $stringy->str = $truncated . $substring;
1439
1440 19
    return $stringy;
1441
  }
1442
1443
  /**
1444
   * A multibyte string shuffle function. It returns a string with its
1445
   * characters in random order.
1446
   *
1447
   * @return Stringy Object with a shuffled $str
1448
   */
1449 3
  public function shuffle()
1450
  {
1451 3
    $shuffledStr = UTF8::str_shuffle($this->str);
1452
1453 3
    return static::create($shuffledStr, $this->encoding);
1454
  }
1455
1456
  /**
1457
   * Converts the string into an URL slug. This includes replacing non-ASCII
1458
   * characters with their closest ASCII equivalents, removing remaining
1459
   * non-ASCII and non-alphanumeric characters, and replacing whitespace with
1460
   * $replacement. The replacement defaults to a single dash, and the string
1461
   * is also converted to lowercase.
1462
   *
1463
   * @param string $replacement The string used to replace whitespace
1464
   * @param string $language    The language for the url
1465
   * @param bool   $strToLower  string to lower
1466
   *
1467
   * @return Stringy Object whose $str has been converted to an URL slug
1468
   */
1469 15
  public function slugify($replacement = '-', $language = 'de', $strToLower = true)
1470
  {
1471 15
    $slug = URLify::slug($this->str, $language, $replacement, $strToLower);
1472
1473 15
    return static::create($slug, $this->encoding);
1474
  }
1475
1476
  /**
1477
   * Remove css media-queries.
1478
   *
1479
   * @return Stringy
1480
   */
1481 1
  public function stripeCssMediaQueries()
1482
  {
1483 1
    $pattern = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU';
1484
1485 1
    return static::create(preg_replace($pattern, '', $this->str));
1486
  }
1487
1488
  /**
1489
   * Remove empty html-tag.
1490
   *
1491
   * e.g.: <tag></tag>
1492
   *
1493
   * @return Stringy
1494
   */
1495 1
  public function stripeEmptyHtmlTags()
1496
  {
1497 1
    $pattern = "/<[^\/>]*>(([\s]?)*|)<\/[^>]*>/i";
1498
1499 1
    return static::create(preg_replace($pattern, '', $this->str));
1500
  }
1501
1502
  /**
1503
   * Converts the string into an valid UTF-8 string.
1504
   *
1505
   * @return Stringy
1506
   */
1507 1
  public function utf8ify()
1508
  {
1509 1
    return static::create(UTF8::cleanup($this->str));
1510
  }
1511
1512
  /**
1513
   * escape html
1514
   *
1515
   * @return Stringy
1516
   */
1517 6
  public function escape()
1518
  {
1519 6
    $str = UTF8::htmlspecialchars(
1520 6
        $this->str,
1521 6
        ENT_QUOTES | ENT_SUBSTITUTE,
1522 6
        $this->encoding
1523
    );
1524
1525 6
    return static::create($str, $this->encoding);
1526
  }
1527
1528
  /**
1529
   * Create an extract from a text, so if the search-string was found, it will be centered in the output.
1530
   *
1531
   * @param string   $search
1532
   * @param int|null $length
1533
   * @param string   $ellipsis
1534
   *
1535
   * @return Stringy
1536
   */
1537 1
  public function extractText($search = '', $length = null, $ellipsis = '...')
1538
  {
1539
    // init
1540 1
    $text = $this->str;
1541
1542 1
    if (empty($text)) {
1543 1
      return static::create('', $this->encoding);
1544
    }
1545
1546 1
    $trimChars = "\t\r\n -_()!~?=+/*\\,.:;\"'[]{}`&";
1547
1548 1
    if ($length === null) {
1549 1
      $length = $this->length() / 2;
1550
    }
1551
1552 1
    if (empty($search)) {
1553
1554
      $stringLength = UTF8::strlen($text, $this->encoding);
1555
      $end = ($length - 1) > $stringLength ? $stringLength : ($length - 1);
1556
      $pos = min(UTF8::strpos($text, ' ', $end, 0, $this->encoding), UTF8::strpos($text, '.', $end));
0 ignored issues
show
Documentation introduced by
$this->encoding is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1557
1558
      if ($pos) {
1559
        return static::create(
1560
            rtrim(
1561
                UTF8::substr($text, 0, $pos, $this->encoding),
1562
                $trimChars
1563
            ) . $ellipsis,
1564
            $this->encoding
1565
        );
1566
      } else {
1567
        return static::create($text, $this->encoding);
1568
      }
1569
1570
    }
1571
1572 1
    $wordPos = UTF8::strpos(
1573 1
        UTF8::strtolower($text),
1574 1
        UTF8::strtolower($search, $this->encoding),
1575 1
        null,
1576 1
        $this->encoding
1577
    );
1578 1
    $halfSide = (int)($wordPos - $length / 2 + UTF8::strlen($search, $this->encoding) / 2);
1579
1580 1
    if ($halfSide > 0) {
1581
1582 1
      $halfText = UTF8::substr($text, 0, $halfSide, $this->encoding);
1583 1
      $pos_start = max(UTF8::strrpos($halfText, ' ', 0), UTF8::strrpos($halfText, '.', 0));
0 ignored issues
show
Security Bug introduced by
It seems like $halfText defined by \voku\helper\UTF8::subst...fSide, $this->encoding) on line 1582 can also be of type false; however, voku\helper\UTF8::strrpos() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1584
1585 1
      if (!$pos_start) {
1586 1
        $pos_start = 0;
1587
      }
1588
1589
    } else {
1590 1
      $pos_start = 0;
1591
    }
1592
1593 1
    if ($wordPos && $halfSide > 0) {
1594 1
      $l = $pos_start + $length - 1;
1595 1
      $realLength = UTF8::strlen($text, $this->encoding);
1596
1597 1
      if ($l > $realLength) {
1598
        $l = $realLength;
1599
      }
1600
1601 1
      $pos_end = min(
1602 1
                     UTF8::strpos($text, ' ', $l, $this->encoding),
1603 1
                     UTF8::strpos($text, '.', $l, $this->encoding)
1604 1
                 ) - $pos_start;
1605
1606 1
      if (!$pos_end || $pos_end <= 0) {
1607 1
        $extract = $ellipsis . ltrim(
1608 1
                UTF8::substr(
1609
                    $text,
1610
                    $pos_start,
1611 1
                    UTF8::strlen($text),
1612 1
                    $this->encoding
1613
                ),
1614
                $trimChars
1615
            );
1616 View Code Duplication
      } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
1617 1
        $extract = $ellipsis . trim(
1618 1
                UTF8::substr(
1619
                    $text,
1620
                    $pos_start,
1621
                    $pos_end,
1622 1
                    $this->encoding
1623
                ),
1624
                $trimChars
1625 1
            ) . $ellipsis;
1626
      }
1627
1628
    } else {
1629
1630 1
      $l = $length - 1;
1631 1
      $trueLength = UTF8::strlen($text, $this->encoding);
1632
1633 1
      if ($l > $trueLength) {
1634
        $l = $trueLength;
1635
      }
1636
1637 1
      $pos_end = min(
1638 1
          UTF8::strpos($text, ' ', $l, $this->encoding),
1639 1
          UTF8::strpos($text, '.', $l, $this->encoding)
1640
      );
1641
1642 1 View Code Duplication
      if ($pos_end) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
1643 1
        $extract = rtrim(
1644 1
                       UTF8::substr($text, 0, $pos_end, $this->encoding),
1645
                       $trimChars
1646 1
                   ) . $ellipsis;
1647
      } else {
1648 1
        $extract = $text;
1649
      }
1650
    }
1651
1652 1
    return static::create($extract, $this->encoding);
1653
  }
1654
1655
1656
  /**
1657
   * remove xss from html
1658
   *
1659
   * @return Stringy
1660
   */
1661 6
  public function removeXss()
1662
  {
1663 6
    static $antiXss = null;
1664
1665 6
    if ($antiXss === null) {
1666 1
      $antiXss = new AntiXSS();
1667
    }
1668
1669 6
    $str = $antiXss->xss_clean($this->str);
1670
1671 6
    return static::create($str, $this->encoding);
1672
  }
1673
1674
  /**
1675
   * remove html-break [br | \r\n | \r | \n | ...]
1676
   *
1677
   * @param string $replacement
1678
   *
1679
   * @return Stringy
1680
   */
1681 6
  public function removeHtmlBreak($replacement = '')
1682
  {
1683 6
    $str = preg_replace('#/\r\n|\r|\n|<br.*/?>#isU', $replacement, $this->str);
1684
1685 6
    return static::create($str, $this->encoding);
1686
  }
1687
1688
  /**
1689
   * remove html
1690
   *
1691
   * @param $allowableTags
1692
   *
1693
   * @return Stringy
1694
   */
1695 6
  public function removeHtml($allowableTags = null)
1696
  {
1697 6
    $str = strip_tags($this->str, $allowableTags);
1698
1699 6
    return static::create($str, $this->encoding);
1700
  }
1701
1702
  /**
1703
   * Returns the substring beginning at $start, and up to, but not including
1704
   * the index specified by $end. If $end is omitted, the function extracts
1705
   * the remaining string. If $end is negative, it is computed from the end
1706
   * of the string.
1707
   *
1708
   * @param  int $start Initial index from which to begin extraction
1709
   * @param  int $end   Optional index at which to end extraction
1710
   *
1711
   * @return Stringy Object with its $str being the extracted substring
1712
   */
1713 18
  public function slice($start, $end = null)
1714
  {
1715 18
    if ($end === null) {
1716 4
      $length = $this->length();
1717 14
    } elseif ($end >= 0 && $end <= $start) {
1718 4
      return static::create('', $this->encoding);
1719 10
    } elseif ($end < 0) {
1720 2
      $length = $this->length() + $end - $start;
1721
    } else {
1722 8
      $length = $end - $start;
1723
    }
1724
1725 14
    $str = UTF8::substr($this->str, $start, $length, $this->encoding);
1726
1727 14
    return static::create($str, $this->encoding);
1728
  }
1729
1730
  /**
1731
   * Splits the string with the provided regular expression, returning an
1732
   * array of Stringy objects. An optional integer $limit will truncate the
1733
   * results.
1734
   *
1735
   * @param  string $pattern The regex with which to split the string
1736
   * @param  int    $limit   Optional maximum number of results to return
1737
   *
1738
   * @return Stringy[] An array of Stringy objects
1739
   */
1740 19
  public function split($pattern, $limit = null)
1741
  {
1742 19
    if ($limit === 0) {
1743 2
      return array();
1744
    }
1745
1746
    // UTF8::split errors when supplied an empty pattern in < PHP 5.4.13
1747
    // and current versions of HHVM (3.8 and below)
1748 17
    if ($pattern === '') {
1749 1
      return array(static::create($this->str, $this->encoding));
1750
    }
1751
1752
    // UTF8::split returns the remaining unsplit string in the last index when
1753
    // supplying a limit
1754 16
    if ($limit > 0) {
1755 8
      $limit += 1;
1756
    } else {
1757 8
      $limit = -1;
1758
    }
1759
1760 16
    $array = preg_split('/' . preg_quote($pattern, '/') . '/u', $this->str, $limit);
1761
1762 16
    if ($limit > 0 && count($array) === $limit) {
1763 4
      array_pop($array);
1764
    }
1765
1766
    /** @noinspection CallableInLoopTerminationConditionInspection */
1767
    /** @noinspection ForeachInvariantsInspection */
1768 16 View Code Duplication
    for ($i = 0; $i < count($array); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
1769 16
      $array[$i] = static::create($array[$i], $this->encoding);
1770
    }
1771
1772 16
    return $array;
1773
  }
1774
1775
  /**
1776
   * Surrounds $str with the given substring.
1777
   *
1778
   * @param  string $substring The substring to add to both sides
1779
   *
1780
   * @return Stringy Object whose $str had the substring both prepended and
1781
   *                 appended
1782
   */
1783 5
  public function surround($substring)
1784
  {
1785 5
    $str = implode('', array($substring, $this->str, $substring));
1786
1787 5
    return static::create($str, $this->encoding);
1788
  }
1789
1790
  /**
1791
   * Returns a case swapped version of the string.
1792
   *
1793
   * @return Stringy Object whose $str has each character's case swapped
1794
   */
1795 5
  public function swapCase()
1796
  {
1797 5
    $stringy = static::create($this->str, $this->encoding);
1798
1799 5
    $stringy->str = UTF8::swapCase($stringy->str, $stringy->encoding);
1800
1801 5
    return $stringy;
1802
  }
1803
1804
  /**
1805
   * Returns a string with smart quotes, ellipsis characters, and dashes from
1806
   * Windows-1252 (commonly used in Word documents) replaced by their ASCII
1807
   * equivalents.
1808
   *
1809
   * @return Stringy Object whose $str has those characters removed
1810
   */
1811 4
  public function tidy()
1812
  {
1813 4
    $str = UTF8::normalize_msword($this->str);
1814
1815 4
    return static::create($str, $this->encoding);
1816
  }
1817
1818
  /**
1819
   * Returns a trimmed string with the first letter of each word capitalized.
1820
   * Also accepts an array, $ignore, allowing you to list words not to be
1821
   * capitalized.
1822
   *
1823
   * @param  array $ignore An array of words not to capitalize
1824
   *
1825
   * @return Stringy Object with a titleized $str
1826
   */
1827 5
  public function titleize($ignore = null)
1828
  {
1829 5
    $stringy = static::create($this->trim(), $this->encoding);
1830 5
    $encoding = $this->encoding;
1831
1832 5
    $stringy->str = preg_replace_callback(
1833 5
        '/([\S]+)/u',
1834
        function ($match) use ($encoding, $ignore) {
1835 5
          if ($ignore && in_array($match[0], $ignore, true)) {
1836 2
            return $match[0];
1837
          } else {
1838 5
            $stringy = new Stringy($match[0], $encoding);
1839
1840 5
            return (string)$stringy->toLowerCase()->upperCaseFirst();
1841
          }
1842 5
        },
1843 5
        $stringy->str
1844
    );
1845
1846 5
    return $stringy;
1847
  }
1848
1849
  /**
1850
   * Converts all characters in the string to lowercase. An alias for PHP's
1851
   * UTF8::strtolower().
1852
   *
1853
   * @return Stringy Object with all characters of $str being lowercase
1854
   */
1855 27
  public function toLowerCase()
1856
  {
1857 27
    $str = UTF8::strtolower($this->str, $this->encoding);
1858
1859 27
    return static::create($str, $this->encoding);
1860
  }
1861
1862
  /**
1863
   * Returns true if the string is base64 encoded, false otherwise.
1864
   *
1865
   * @return bool Whether or not $str is base64 encoded
1866
   */
1867 7
  public function isBase64()
1868
  {
1869 7
    return UTF8::is_base64($this->str);
1870
  }
1871
1872
  /**
1873
   * Returns an ASCII version of the string. A set of non-ASCII characters are
1874
   * replaced with their closest ASCII counterparts, and the rest are removed
1875
   * unless instructed otherwise.
1876
   *
1877
   * @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p>
1878
   *
1879
   * @return Stringy Object whose $str contains only ASCII characters
1880
   */
1881 16
  public function toAscii($strict = false)
1882
  {
1883 16
    $str = UTF8::to_ascii($this->str, '?', $strict);
1884
1885 16
    return static::create($str, $this->encoding);
1886
  }
1887
1888
  /**
1889
   * Returns a boolean representation of the given logical string value.
1890
   * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0',
1891
   * 'off', and 'no' will return false. In all instances, case is ignored.
1892
   * For other numeric strings, their sign will determine the return value.
1893
   * In addition, blank strings consisting of only whitespace will return
1894
   * false. For all other strings, the return value is a result of a
1895
   * boolean cast.
1896
   *
1897
   * @return bool A boolean value for the string
1898
   */
1899 15
  public function toBoolean()
1900
  {
1901 15
    $key = $this->toLowerCase()->str;
1902
    $map = array(
1903 15
        'true'  => true,
1904
        '1'     => true,
1905
        'on'    => true,
1906
        'yes'   => true,
1907
        'false' => false,
1908
        '0'     => false,
1909
        'off'   => false,
1910
        'no'    => false,
1911
    );
1912
1913 15
    if (array_key_exists($key, $map)) {
1914 10
      return $map[$key];
1915 5
    } elseif (is_numeric($this->str)) {
1916 2
      return ((int)$this->str > 0);
1917
    } else {
1918 3
      return (bool)$this->regexReplace('[[:space:]]', '')->str;
1919
    }
1920
  }
1921
1922
  /**
1923
   * Return Stringy object as string, but you can also use (string) for automatically casting the object into a string.
1924
   *
1925
   * @return string
1926
   */
1927 968
  public function toString()
1928
  {
1929 968
    return (string)$this->str;
1930
  }
1931
1932
  /**
1933
   * Converts each tab in the string to some number of spaces, as defined by
1934
   * $tabLength. By default, each tab is converted to 4 consecutive spaces.
1935
   *
1936
   * @param  int $tabLength Number of spaces to replace each tab with
1937
   *
1938
   * @return Stringy Object whose $str has had tabs switched to spaces
1939
   */
1940 6 View Code Duplication
  public function toSpaces($tabLength = 4)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
1941
  {
1942 6
    $spaces = UTF8::str_repeat(' ', $tabLength);
1943 6
    $str = UTF8::str_replace("\t", $spaces, $this->str);
1944
1945 6
    return static::create($str, $this->encoding);
1946
  }
1947
1948
  /**
1949
   * Converts each occurrence of some consecutive number of spaces, as
1950
   * defined by $tabLength, to a tab. By default, each 4 consecutive spaces
1951
   * are converted to a tab.
1952
   *
1953
   * @param  int $tabLength Number of spaces to replace with a tab
1954
   *
1955
   * @return Stringy Object whose $str has had spaces switched to tabs
1956
   */
1957 5 View Code Duplication
  public function toTabs($tabLength = 4)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
1958
  {
1959 5
    $spaces = UTF8::str_repeat(' ', $tabLength);
1960 5
    $str = UTF8::str_replace($spaces, "\t", $this->str);
1961
1962 5
    return static::create($str, $this->encoding);
1963
  }
1964
1965
  /**
1966
   * Converts the first character of each word in the string to uppercase.
1967
   *
1968
   * @return Stringy Object with all characters of $str being title-cased
1969
   */
1970 5
  public function toTitleCase()
1971
  {
1972
    // "mb_convert_case()" used a polyfill from the "UTF8"-Class
1973 5
    $str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding);
1974
1975 5
    return static::create($str, $this->encoding);
1976
  }
1977
1978
  /**
1979
   * Converts all characters in the string to uppercase. An alias for PHP's
1980
   * UTF8::strtoupper().
1981
   *
1982
   * @return Stringy Object with all characters of $str being uppercase
1983
   */
1984 5
  public function toUpperCase()
1985
  {
1986 5
    $str = UTF8::strtoupper($this->str, $this->encoding);
1987
1988 5
    return static::create($str, $this->encoding);
1989
  }
1990
1991
  /**
1992
   * Returns a string with whitespace removed from the start of the string.
1993
   * Supports the removal of unicode whitespace. Accepts an optional
1994
   * string of characters to strip instead of the defaults.
1995
   *
1996
   * @param  string $chars Optional string of characters to strip
1997
   *
1998
   * @return Stringy Object with a trimmed $str
1999
   */
2000 13 View Code Duplication
  public function trimLeft($chars = null)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2001
  {
2002 13
    if (!$chars) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $chars of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2003 11
      $chars = '[:space:]';
2004
    } else {
2005 2
      $chars = preg_quote($chars, '/');
2006
    }
2007
2008 13
    return $this->regexReplace("^[$chars]+", '');
2009
  }
2010
2011
  /**
2012
   * Returns a string with whitespace removed from the end of the string.
2013
   * Supports the removal of unicode whitespace. Accepts an optional
2014
   * string of characters to strip instead of the defaults.
2015
   *
2016
   * @param  string $chars Optional string of characters to strip
2017
   *
2018
   * @return Stringy Object with a trimmed $str
2019
   */
2020 13 View Code Duplication
  public function trimRight($chars = null)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2021
  {
2022 13
    if (!$chars) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $chars of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2023 11
      $chars = '[:space:]';
2024
    } else {
2025 2
      $chars = preg_quote($chars, '/');
2026
    }
2027
2028 13
    return $this->regexReplace("[$chars]+\$", '');
2029
  }
2030
2031
  /**
2032
   * Truncates the string to a given length. If $substring is provided, and
2033
   * truncating occurs, the string is further truncated so that the substring
2034
   * may be appended without exceeding the desired length.
2035
   *
2036
   * @param  int    $length    Desired length of the truncated string
2037
   * @param  string $substring The substring to append if it can fit
2038
   *
2039
   * @return Stringy Object with the resulting $str after truncating
2040
   */
2041 22 View Code Duplication
  public function truncate($length, $substring = '')
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2042
  {
2043 22
    $stringy = static::create($this->str, $this->encoding);
2044 22
    if ($length >= $stringy->length()) {
2045 4
      return $stringy;
2046
    }
2047
2048
    // Need to further trim the string so we can append the substring
2049 18
    $substringLength = UTF8::strlen($substring, $stringy->encoding);
2050 18
    $length -= $substringLength;
2051
2052 18
    $truncated = UTF8::substr($stringy->str, 0, $length, $stringy->encoding);
2053 18
    $stringy->str = $truncated . $substring;
2054
2055 18
    return $stringy;
2056
  }
2057
2058
  /**
2059
   * Returns a lowercase and trimmed string separated by underscores.
2060
   * Underscores are inserted before uppercase characters (with the exception
2061
   * of the first character of the string), and in place of spaces as well as
2062
   * dashes.
2063
   *
2064
   * @return Stringy Object with an underscored $str
2065
   */
2066 16
  public function underscored()
2067
  {
2068 16
    return $this->delimit('_');
2069
  }
2070
2071
  /**
2072
   * Returns an UpperCamelCase version of the supplied string. It trims
2073
   * surrounding spaces, capitalizes letters following digits, spaces, dashes
2074
   * and underscores, and removes spaces, dashes, underscores.
2075
   *
2076
   * @return Stringy Object with $str in UpperCamelCase
2077
   */
2078 13
  public function upperCamelize()
2079
  {
2080 13
    return $this->camelize()->upperCaseFirst();
2081
  }
2082
2083
  /**
2084
   * Returns a camelCase version of the string. Trims surrounding spaces,
2085
   * capitalizes letters following digits, spaces, dashes and underscores,
2086
   * and removes spaces, dashes, as well as underscores.
2087
   *
2088
   * @return Stringy Object with $str in camelCase
2089
   */
2090 32
  public function camelize()
2091
  {
2092 32
    $encoding = $this->encoding;
2093 32
    $stringy = $this->trim()->lowerCaseFirst();
2094 32
    $stringy->str = preg_replace('/^[-_]+/', '', $stringy->str);
2095
2096 32
    $stringy->str = preg_replace_callback(
2097 32
        '/[-_\s]+(.)?/u',
2098
        function ($match) use ($encoding) {
2099 27
          if (isset($match[1])) {
2100 27
            return UTF8::strtoupper($match[1], $encoding);
2101
          } else {
2102 1
            return '';
2103
          }
2104 32
        },
2105 32
        $stringy->str
2106
    );
2107
2108 32
    $stringy->str = preg_replace_callback(
2109 32
        '/[\d]+(.)?/u',
2110
        function ($match) use ($encoding) {
2111 6
          return UTF8::strtoupper($match[0], $encoding);
2112 32
        },
2113 32
        $stringy->str
2114
    );
2115
2116 32
    return $stringy;
2117
  }
2118
2119
  /**
2120
   * Convert a string to e.g.: "snake_case"
2121
   *
2122
   * @return Stringy Object with $str in snake_case
2123
   */
2124 20
  public function snakeize()
2125
  {
2126 20
    $str = $this->str;
2127
2128 20
    $encoding = $this->encoding;
2129 20
    $str = UTF8::normalize_whitespace($str);
2130 20
    $str = str_replace('-', '_', $str);
2131
2132 20
    $str = preg_replace_callback(
2133 20
        '/([\d|A-Z])/u',
2134 20
        function ($matches) use ($encoding) {
2135 8
          $match = $matches[1];
2136 8
          $matchInt = (int)$match;
2137
2138 8
          if ("$matchInt" == $match) {
2139 4
            return '_' . $match . '_';
2140
          } else {
2141 4
            return '_' . UTF8::strtolower($match, $encoding);
2142
          }
2143 20
        },
2144
        $str
2145
    );
2146
2147 20
    $str = preg_replace(
2148
        array(
2149
2150 20
            '/\s+/',      // convert spaces to "_"
2151
            '/^\s+|\s+$/',  // trim leading & trailing spaces
2152
            '/_+/',         // remove double "_"
2153
        ),
2154
        array(
2155 20
            '_',
2156
            '',
2157
            '_',
2158
        ),
2159
        $str
2160
    );
2161
2162 20
    $str = UTF8::trim($str, '_'); // trim leading & trailing "_"
2163 20
    $str = UTF8::trim($str); // trim leading & trailing whitespace
2164
2165 20
    return static::create($str, $this->encoding);
2166
  }
2167
2168
  /**
2169
   * Converts the first character of the string to lower case.
2170
   *
2171
   * @return Stringy Object with the first character of $str being lower case
2172
   */
2173 37 View Code Duplication
  public function lowerCaseFirst()
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2174
  {
2175 37
    $first = UTF8::substr($this->str, 0, 1, $this->encoding);
2176 37
    $rest = UTF8::substr($this->str, 1, $this->length() - 1, $this->encoding);
2177
2178 37
    $str = UTF8::strtolower($first, $this->encoding) . $rest;
0 ignored issues
show
Security Bug introduced by
It seems like $first defined by \voku\helper\UTF8::subst... 0, 1, $this->encoding) on line 2175 can also be of type false; however, voku\helper\UTF8::strtolower() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
2179
2180 37
    return static::create($str, $this->encoding);
2181
  }
2182
2183
  /**
2184
   * Shorten the string after $length, but also after the next word.
2185
   *
2186
   * @param int    $length
2187
   * @param string $strAddOn
2188
   *
2189
   * @return Stringy
2190
   */
2191 4
  public function shortenAfterWord($length, $strAddOn = '...')
2192
  {
2193 4
    $string = UTF8::str_limit_after_word($this->str, $length, $strAddOn);
2194
2195 4
    return static::create($string);
2196
  }
2197
2198
  /**
2199
   * Line-Wrap the string after $limit, but also after the next word.
2200
   *
2201
   * @param int $limit
2202
   *
2203
   * @return Stringy
2204
   */
2205 1
  public function lineWrapAfterWord($limit)
2206
  {
2207 1
    $strings = preg_split('/\\r\\n|\\r|\\n/', $this->str);
2208
2209 1
    $string = '';
2210 1
    foreach ($strings as $value) {
2211 1
      $string .= wordwrap($value, $limit);
2212 1
      $string .= "\n";
2213
    }
2214
2215 1
    return static::create($string);
2216
  }
2217
2218
  /**
2219
   * Gets the substring after the first occurrence of a separator.
2220
   * If no match is found returns new empty Stringy object.
2221
   *
2222
   * @param string $separator
2223
   *
2224
   * @return Stringy
2225
   */
2226 1 View Code Duplication
  public function afterFirst($separator)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2227
  {
2228 1
    if (($offset = $this->indexOf($separator)) === false) {
2229 1
      return static::create('');
2230
    }
2231
2232 1
    return static::create(
2233 1
        UTF8::substr(
2234 1
            $this->str,
2235 1
            $offset + UTF8::strlen($separator, $this->encoding),
2236 1
            null,
2237 1
            $this->encoding
2238
        ),
2239 1
        $this->encoding
2240
    );
2241
  }
2242
2243
  /**
2244
   * Gets the substring after the last occurrence of a separator.
2245
   * If no match is found returns new empty Stringy object.
2246
   *
2247
   * @param string $separator
2248
   *
2249
   * @return Stringy
2250
   */
2251 1
  public function afterLast($separator)
2252
  {
2253 1
    $offset = $this->indexOfLast($separator);
2254 1
    if ($offset === false) {
2255 1
      return static::create('', $this->encoding);
2256
    }
2257
2258 1
    return static::create(
2259 1
        UTF8::substr(
2260 1
            $this->str,
2261 1
            $offset + UTF8::strlen($separator, $this->encoding),
2262 1
            null,
2263 1
            $this->encoding
2264
        ),
2265 1
        $this->encoding
2266
    );
2267
  }
2268
2269
  /**
2270
   * Gets the substring before the first occurrence of a separator.
2271
   * If no match is found returns new empty Stringy object.
2272
   *
2273
   * @param string $separator
2274
   *
2275
   * @return Stringy
2276
   */
2277 1 View Code Duplication
  public function beforeFirst($separator)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2278
  {
2279 1
    $offset = $this->indexOf($separator);
2280 1
    if ($offset === false) {
2281 1
      return static::create('', $this->encoding);
2282
    }
2283
2284 1
    return static::create(
2285 1
        UTF8::substr(
2286 1
            $this->str,
2287 1
            0,
2288
            $offset,
2289 1
            $this->encoding
2290
        ),
2291 1
        $this->encoding
2292
    );
2293
  }
2294
2295
  /**
2296
   * Gets the substring before the last occurrence of a separator.
2297
   * If no match is found returns new empty Stringy object.
2298
   *
2299
   * @param string $separator
2300
   *
2301
   * @return Stringy
2302
   */
2303 1 View Code Duplication
  public function beforeLast($separator)
0 ignored issues
show
Duplication introduced by
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.

Loading history...
2304
  {
2305 1
    $offset = $this->indexOfLast($separator);
2306 1
    if ($offset === false) {
2307 1
      return static::create('', $this->encoding);
2308
    }
2309
2310 1
    return static::create(
2311 1
        UTF8::substr(
2312 1
            $this->str,
2313 1
            0,
2314
            $offset,
2315 1
            $this->encoding
2316
        ),
2317 1
        $this->encoding
2318
    );
2319
  }
2320
2321
  /**
2322
   * Returns the string with the first letter of each word capitalized,
2323
   * except for when the word is a name which shouldn't be capitalized.
2324
   *
2325
   * @return Stringy Object with $str capitalized
2326
   */
2327 39
  public function capitalizePersonalName()
2328
  {
2329 39
    $stringy = $this->collapseWhitespace();
2330 39
    $stringy->str = $this->capitalizePersonalNameByDelimiter($stringy->str, ' ');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->capitalizePersona...ter($stringy->str, ' ') of type this<Stringy\Stringy> is incompatible with the declared type string of property $str.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
2331 39
    $stringy->str = $this->capitalizePersonalNameByDelimiter($stringy->str, '-');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->capitalizePersona...ter($stringy->str, '-') of type this<Stringy\Stringy> is incompatible with the declared type string of property $str.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
2332
2333 39
    return static::create($stringy, $this->encoding);
2334
  }
2335
2336
  /**
2337
   * @param string $word
2338
   *
2339
   * @return string
2340
   */
2341 7
  private function capitalizeWord($word)
2342
  {
2343 7
    $encoding = $this->encoding;
2344
2345 7
    $firstCharacter = UTF8::substr($word, 0, 1, $encoding);
2346 7
    $restOfWord = UTF8::substr($word, 1, null, $encoding);
2347 7
    $firstCharacterUppercased = UTF8::strtoupper($firstCharacter, $encoding);
0 ignored issues
show
Security Bug introduced by
It seems like $firstCharacter defined by \voku\helper\UTF8::substr($word, 0, 1, $encoding) on line 2345 can also be of type false; however, voku\helper\UTF8::strtoupper() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
2348
2349 7
    return new static($firstCharacterUppercased . $restOfWord, $encoding);
2350
  }
2351
2352
  /**
2353
   * Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius").
2354
   *
2355
   * @param string $names
2356
   * @param string $delimiter
2357
   *
2358
   * @return string
2359
   */
2360 39
  private function capitalizePersonalNameByDelimiter($names, $delimiter)
2361
  {
2362
    // init
2363 39
    $names = explode($delimiter, $names);
2364 39
    $encoding = $this->encoding;
2365
2366
    $specialCases = array(
2367
        'names'    => array(
2368
            'ab',
2369
            'af',
2370
            'al',
2371
            'and',
2372
            'ap',
2373
            'bint',
2374
            'binte',
2375
            'da',
2376
            'de',
2377
            'del',
2378
            'den',
2379
            'der',
2380
            'di',
2381
            'dit',
2382
            'ibn',
2383
            'la',
2384
            'mac',
2385
            'nic',
2386
            'of',
2387
            'ter',
2388
            'the',
2389
            'und',
2390
            'van',
2391
            'von',
2392
            'y',
2393
            'zu',
2394 39
        ),
2395
        'prefixes' => array(
2396
            'al-',
2397
            "d'",
2398
            'ff',
2399
            "l'",
2400
            'mac',
2401
            'mc',
2402
            'nic',
2403
        ),
2404
    );
2405
2406 39
    foreach ($names as &$name) {
2407 39
      if (in_array($name, $specialCases['names'], true)) {
2408 27
        continue;
2409
      }
2410
2411 13
      $continue = false;
2412
2413 13
      if ($delimiter == '-') {
2414 13 View Code Duplication
        foreach ($specialCases['names'] as $beginning) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
2415 13
          if (UTF8::strpos($name, $beginning, null, $encoding) === 0) {
2416 13
            $continue = true;
2417
          }
2418
        }
2419
      }
2420
2421 13 View Code Duplication
      foreach ($specialCases['prefixes'] as $beginning) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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.

Loading history...
2422 13
        if (UTF8::strpos($name, $beginning, null, $encoding) === 0) {
2423 13
          $continue = true;
2424
        }
2425
      }
2426
2427 13
      if ($continue) {
2428 7
        continue;
2429
      }
2430
2431 7
      $name = $this->capitalizeWord($name);
2432
    }
2433
2434 39
    return new static(implode($delimiter, $names), $encoding);
2435
  }
2436
}
2437