Code Duplication    Length = 14-16 lines in 2 locations

src/Stringy.php 2 locations

@@ 918-931 (lines=14) @@
915
   *
916
   * @return static <p>Object with the resulting $str after the insertion.</p>
917
   */
918
  public function insert(string $substring, int $index): self
919
  {
920
    $stringy = static::create($this->str, $this->encoding);
921
    if ($index > $stringy->length()) {
922
      return $stringy;
923
    }
924
925
    $start = UTF8::substr($stringy->str, 0, $index, $stringy->encoding);
926
    $end = UTF8::substr($stringy->str, $index, $stringy->length(), $stringy->encoding);
927
928
    $stringy->str = $start . $substring . $end;
929
930
    return $stringy;
931
  }
932
933
  /**
934
   * Returns true if the string contains the $pattern, otherwise false.
@@ 2485-2500 (lines=16) @@
2482
   *
2483
   * @return static <p>Object with the resulting $str after truncating.</p>
2484
   */
2485
  public function truncate(int $length, string $substring = ''): self
2486
  {
2487
    $stringy = static::create($this->str, $this->encoding);
2488
    if ($length >= $stringy->length()) {
2489
      return $stringy;
2490
    }
2491
2492
    // Need to further trim the string so we can append the substring
2493
    $substringLength = UTF8::strlen($substring, $stringy->encoding);
2494
    $length -= $substringLength;
2495
2496
    $truncated = UTF8::substr($stringy->str, 0, $length, $stringy->encoding);
2497
    $stringy->str = $truncated . $substring;
2498
2499
    return $stringy;
2500
  }
2501
2502
  /**
2503
   * Returns a lowercase and trimmed string separated by underscores.