Code Duplication    Length = 14-16 lines in 2 locations

src/Stringy.php 2 locations

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