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 Product extends Combinatorics implements \Iterator, \Countable |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The key. |
||
| 17 | * |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected $key; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The iterators. |
||
| 24 | * |
||
| 25 | * @var \Iterator[] |
||
| 26 | */ |
||
| 27 | protected $iterators = array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Product constructor. |
||
| 31 | * |
||
| 32 | * @param array $datasetArray |
||
| 33 | * The array of dataset. |
||
| 34 | */ |
||
| 35 | 4 | public function __construct(array $datasetArray) |
|
| 36 | { |
||
| 37 | 4 | parent::__construct($datasetArray); |
|
| 38 | |||
| 39 | 4 | foreach ($datasetArray as $dataset) { |
|
| 40 | 4 | $this->iterators[] = new \ArrayIterator($dataset); |
|
| 41 | } |
||
| 42 | |||
| 43 | 4 | $this->key = 0; |
|
| 44 | 4 | } |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 4 | public function current() |
|
| 50 | { |
||
| 51 | 4 | $tuple = array(); |
|
| 52 | |||
| 53 | 4 | foreach ($this->iterators as $iterator) { |
|
| 54 | 4 | $tuple[] = $iterator->current(); |
|
| 55 | } |
||
| 56 | |||
| 57 | 4 | return $tuple; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 4 | public function next() |
|
|
|
|||
| 64 | { |
||
| 65 | 4 | foreach (array_reverse($this->iterators) as $key => $iterator) { |
|
| 66 | 4 | $iterator->next(); |
|
| 67 | 4 | if ($iterator->valid()) { |
|
| 68 | 4 | $count_iterators = count($this->iterators); |
|
| 69 | 4 | foreach ($this->iterators as $key2 => $iterator2) { |
|
| 70 | 4 | if ($key >= $count_iterators - $key2) { |
|
| 71 | 4 | $iterator2->rewind(); |
|
| 72 | } |
||
| 73 | } |
||
| 74 | 4 | break; |
|
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | 4 | $this->key++; |
|
| 79 | 4 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function key() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | 4 | public function valid() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | 4 | public function rewind() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | 4 | public function count() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Convert the iterator into an array. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | * The elements. |
||
| 136 | */ |
||
| 137 | 4 | View Code Duplication | public function toArray() |
| 147 | } |
||
| 148 |
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.