Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php | ||
| 17 | class SuggestionIterator implements \ArrayAccess, \Iterator | ||
| 18 | { | ||
| 19 | /** | ||
| 20 | * @var array | ||
| 21 | */ | ||
| 22 | private $rawData = []; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @var SuggestionEntry[] | ||
| 26 | */ | ||
| 27 | private $convertedData = []; | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Constructor. | ||
| 31 | * | ||
| 32 | * @param array $rawData | ||
| 33 | */ | ||
| 34 | public function __construct($rawData) | ||
| 38 | |||
| 39 | /** | ||
| 40 |      * {@inheritdoc} | ||
| 41 | */ | ||
| 42 | public function offsetExists($offset) | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Offset to retrieve. | ||
| 49 | * | ||
| 50 | * @param string|int $offset | ||
| 51 | * | ||
| 52 | * @return SuggestionEntry[]|null | ||
| 53 | */ | ||
| 54 | View Code Duplication | public function offsetGet($offset) | |
| 67 | |||
| 68 | /** | ||
| 69 |      * {@inheritdoc} | ||
| 70 | */ | ||
| 71 | public function offsetSet($offset, $value) | ||
| 75 | |||
| 76 | /** | ||
| 77 |      * {@inheritdoc} | ||
| 78 | */ | ||
| 79 | public function offsetUnset($offset) | ||
| 83 | |||
| 84 | /** | ||
| 85 |      * {@inheritdoc} | ||
| 86 | */ | ||
| 87 | public function current() | ||
| 91 | |||
| 92 | /** | ||
| 93 |      * {@inheritdoc} | ||
| 94 | */ | ||
| 95 | public function next() | ||
| 99 | |||
| 100 | /** | ||
| 101 |      * {@inheritdoc} | ||
| 102 | */ | ||
| 103 | public function key() | ||
| 107 | |||
| 108 | /** | ||
| 109 |      * {@inheritdoc} | ||
| 110 | */ | ||
| 111 | public function valid() | ||
| 115 | |||
| 116 | /** | ||
| 117 |      * {@inheritdoc} | ||
| 118 | */ | ||
| 119 | public function rewind() | ||
| 123 | |||
| 124 | /** | ||
| 125 | * Converts array to a array of suggestion objects. | ||
| 126 | * | ||
| 127 | * @param array $data | ||
| 128 | * | ||
| 129 | * @return SuggestionEntry[] | ||
| 130 | */ | ||
| 131 | private function convert($data) | ||
| 145 | } | ||
| 146 | 
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.