| Conditions | 12 |
| Paths | 10 |
| Total Lines | 33 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 80 | public function firstDifference($old, $new) |
||
| 81 | { |
||
| 82 | // loop through old and new character by character and compare |
||
| 83 | $oldLen = mb_strlen($old); |
||
| 84 | $newLen = mb_strlen($new); |
||
| 85 | |||
| 86 | if ($oldLen === 0) { |
||
| 87 | return 0; |
||
| 88 | } |
||
| 89 | |||
| 90 | $oldStripped = $this->cursor->filter($old, static::REPLACEMENT_CHAR); |
||
| 91 | $newStripped = $this->cursor->filter($new, static::REPLACEMENT_CHAR); |
||
| 92 | $lastReal = 0; |
||
| 93 | |||
| 94 | for ($i = 0; $i < $oldLen && $i < $newLen; $i++) { |
||
| 95 | if (mb_substr($old, $i, 1) !== mb_substr($new, $i, 1)) { |
||
| 96 | if (($i > 0) |
||
| 97 | && ((mb_substr($oldStripped, $i - 1, 1) === static::REPLACEMENT_CHAR) |
||
| 98 | || (mb_substr($newStripped, $i - 1, 1) === static::REPLACEMENT_CHAR) |
||
| 99 | ) |
||
| 100 | ) { |
||
| 101 | return $lastReal > 0 ? $lastReal + 1 : 0; |
||
| 102 | } |
||
| 103 | return $i; |
||
| 104 | } elseif (mb_substr($oldStripped, $i, 1) !== static::REPLACEMENT_CHAR) { |
||
| 105 | $lastReal = $i; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | if ($i < $oldLen || $i < $newLen) { |
||
| 109 | return $i; |
||
| 110 | } |
||
| 111 | return -1; |
||
| 112 | } |
||
| 113 | } |
||
| 114 |