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 |
||
| 10 | class Product extends Combinatorics implements \Iterator, \Countable |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The key. |
||
| 14 | * |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | protected $key; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The iterators. |
||
| 21 | * |
||
| 22 | * @var \Iterator[] |
||
| 23 | */ |
||
| 24 | protected $iterators = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Product constructor. |
||
| 28 | * |
||
| 29 | * @param array $datasetArray |
||
| 30 | * The array of dataset |
||
| 31 | */ |
||
| 32 | 4 | public function __construct(array $datasetArray) |
|
| 33 | { |
||
| 34 | 4 | parent::__construct($datasetArray); |
|
| 35 | |||
| 36 | 4 | foreach ($datasetArray as $dataset) { |
|
| 37 | 4 | $this->iterators[] = new \ArrayIterator($dataset); |
|
| 38 | } |
||
| 39 | |||
| 40 | 4 | $this->key = 0; |
|
| 41 | 4 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | 4 | public function current() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | 4 | public function next() |
|
|
|
|||
| 61 | { |
||
| 62 | 4 | foreach (array_reverse($this->iterators) as $key => $iterator) { |
|
| 63 | 4 | $iterator->next(); |
|
| 64 | 4 | if ($iterator->valid()) { |
|
| 65 | 4 | $count_iterators = count($this->iterators); |
|
| 66 | 4 | foreach ($this->iterators as $key2 => $iterator2) { |
|
| 67 | 4 | if ($key >= $count_iterators - $key2) { |
|
| 68 | 4 | $iterator2->rewind(); |
|
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | 4 | break; |
|
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | 4 | ++$this->key; |
|
| 77 | 4 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function key() |
||
| 83 | { |
||
| 84 | return $this->key; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 4 | public function valid() |
|
| 91 | { |
||
| 92 | 4 | $isUnlessOneValid = false; |
|
| 93 | |||
| 94 | 4 | foreach ($this->iterators as $iterator) { |
|
| 95 | 4 | if ($iterator->valid()) { |
|
| 96 | 4 | $isUnlessOneValid = true; |
|
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | 4 | return $isUnlessOneValid; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 4 | public function rewind() |
|
| 107 | { |
||
| 108 | 4 | foreach ($this->iterators as $iterator) { |
|
| 109 | 4 | $iterator->rewind(); |
|
| 110 | } |
||
| 111 | |||
| 112 | 4 | $this->key = 0; |
|
| 113 | 4 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | 4 | public function count() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Convert the iterator into an array. |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | * The elements |
||
| 134 | */ |
||
| 135 | 4 | View Code Duplication | public function toArray() |
| 145 | } |
||
| 146 |
This check marks variable names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.