| Conditions | 3 |
| Paths | 3 |
| Total Lines | 27 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 3 |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 27 | 2 | public function getCommonPrefix(AbstractCG $group): string |
|
| 28 | { |
||
| 29 | 2 | $prefix = ''; |
|
| 30 | |||
| 31 | // Convert strings to arrays |
||
| 32 | 2 | $initialArray = str_split($this->string); |
|
| 33 | 2 | $comparedArray = str_split($group->string); |
|
| 34 | |||
| 35 | // Get shortest array |
||
| 36 | 2 | $minSize = min(count($initialArray), count($comparedArray)); |
|
| 37 | |||
| 38 | // Iterate longest array |
||
| 39 | 2 | for ($i = 0; $i < $minSize; $i++) { |
|
| 40 | // Get existing character or empty string |
||
| 41 | 2 | $initialChar = $initialArray[$i] ?? ''; |
|
| 42 | 2 | $comparedChar = $comparedArray[$i] ?? ''; |
|
| 43 | |||
| 44 | // On first mismatch - break |
||
| 45 | 2 | if ($initialChar !== $comparedChar) { |
|
| 46 | 2 | break; |
|
| 47 | } |
||
| 48 | |||
| 49 | 2 | $prefix .= $initialChar; |
|
| 50 | } |
||
| 51 | |||
| 52 | 2 | return $prefix; |
|
| 53 | } |
||
| 54 | |||
| 66 |