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 |
||
| 12 | class Prime extends Combinatorics implements \Iterator { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | protected $max; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $key; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | protected $count = 1; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $current = 2; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Prime constructor. |
||
| 36 | */ |
||
| 37 | 2 | public function __construct() { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritdoc |
||
| 43 | */ |
||
| 44 | 2 | public function current() { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | 2 | public function next() { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | */ |
||
| 66 | 2 | public function key() { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | 2 | public function valid() { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @inheritdoc |
||
| 79 | */ |
||
| 80 | 2 | public function rewind() { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @return int |
||
| 86 | */ |
||
| 87 | 2 | public function count() { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | 2 | public function toArray() { |
|
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $number |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | 2 | protected function isPrimeNumber($number) { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param int $max |
||
| 134 | */ |
||
| 135 | 2 | public function setMaxLimit($max) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | 2 | public function getMaxLimit() { |
|
| 145 | |||
| 146 | } |
||
| 147 |