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 |
||
| 15 | class EnumMapBench |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var mixed[] |
||
| 19 | */ |
||
| 20 | private $values; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Enum66[] |
||
| 24 | */ |
||
| 25 | private $enumerators; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var EnumMap |
||
| 29 | */ |
||
| 30 | private $emptyMap; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var EnumMap |
||
| 34 | */ |
||
| 35 | private $fullMap; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Will be called before every subject |
||
| 39 | */ |
||
| 40 | public function init() |
||
| 41 | { |
||
| 42 | $this->values = Enum66::getValues(); |
||
| 43 | $this->enumerators = Enum66::getEnumerators(); |
||
| 44 | |||
| 45 | $this->emptyMap = new EnumMap(Enum66::class); |
||
| 46 | $this->fullMap = new EnumMap(Enum66::class); |
||
| 47 | foreach ($this->enumerators as $i => $enumerator) { |
||
| 48 | $this->fullMap->offsetSet($enumerator, $i); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | public function benchOffsetSetEnumerator() |
||
| 53 | { |
||
| 54 | foreach ($this->enumerators as $enumerator) { |
||
| 55 | $this->emptyMap->offsetSet($enumerator); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | public function benchOffsetSetValue() |
||
| 60 | { |
||
| 61 | foreach ($this->values as $value) { |
||
| 62 | $this->emptyMap->offsetSet($value); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | public function benchOffsetUnsetEnumerator() |
||
| 67 | { |
||
| 68 | foreach ($this->enumerators as $enumerator) { |
||
| 69 | $this->fullMap->offsetUnset($enumerator); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | public function benchOffsetUnsetValue() |
||
| 74 | { |
||
| 75 | foreach ($this->values as $value) { |
||
| 76 | $this->fullMap->offsetUnset($value); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | public function benchOffsetExistsEnumeratorTrue() |
||
| 81 | { |
||
| 82 | foreach ($this->enumerators as $enumerator) { |
||
| 83 | $this->fullMap->offsetExists($enumerator); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | public function benchOffsetExistsEnumeratorFalse() |
||
| 88 | { |
||
| 89 | foreach ($this->enumerators as $enumerator) { |
||
| 90 | $this->fullMap->offsetExists($enumerator); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | public function benchOffsetExistsValueTrue() |
||
| 95 | { |
||
| 96 | foreach ($this->values as $value) { |
||
| 97 | $this->fullMap->offsetExists($value); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | public function benchOffsetExistsValueFalse() |
||
| 102 | { |
||
| 103 | foreach ($this->values as $value) { |
||
| 104 | $this->fullMap->offsetExists($value); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | public function benchIterateFull() |
||
| 109 | { |
||
| 110 | foreach ($this->fullMap as $enumerator => $_) { |
||
| 111 | $enumerator->getValue(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | public function benchIterateEmpty() |
||
| 116 | { |
||
| 117 | foreach ($this->emptyMap as $enumerator => $_) { |
||
| 118 | $enumerator->getValue(); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function benchCountFull() |
||
| 123 | { |
||
| 124 | $this->fullMap->count(); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function benchCountEmpty() |
||
| 128 | { |
||
| 129 | $this->emptyMap->count(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 |