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 |
||
| 16 | class EnumSet32Bench |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var mixed[] |
||
| 20 | */ |
||
| 21 | private $values; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Enum32[] |
||
| 25 | */ |
||
| 26 | private $enumerators; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var EnumSet |
||
| 30 | */ |
||
| 31 | private $emptySet; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var EnumSet |
||
| 35 | */ |
||
| 36 | private $fullSet; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Will be called before every subject |
||
| 40 | */ |
||
| 41 | public function init() |
||
| 42 | { |
||
| 43 | $this->values = Enum32::getValues(); |
||
| 44 | $this->enumerators = Enum32::getEnumerators(); |
||
| 45 | |||
| 46 | $this->emptySet = new EnumSet(Enum32::class); |
||
| 47 | $this->fullSet = new EnumSet(Enum32::class); |
||
| 48 | foreach ($this->enumerators as $enumerator) { |
||
| 49 | $this->fullSet->attach($enumerator); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | public function benchAttachEnumerator() |
||
| 54 | { |
||
| 55 | foreach ($this->enumerators as $enumerator) { |
||
| 56 | $this->emptySet->attach($enumerator); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | public function benchAttachValue() |
||
| 61 | { |
||
| 62 | foreach ($this->values as $value) { |
||
| 63 | $this->emptySet->attach($value); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | public function benchDetachEnumerator() |
||
| 68 | { |
||
| 69 | foreach ($this->enumerators as $enumerator) { |
||
| 70 | $this->fullSet->detach($enumerator); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | public function benchDetachValue() |
||
| 75 | { |
||
| 76 | foreach ($this->values as $value) { |
||
| 77 | $this->fullSet->detach($value); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function benchContainsEnumeratorTrue() |
||
| 82 | { |
||
| 83 | foreach ($this->enumerators as $enumerator) { |
||
| 84 | $this->fullSet->contains($enumerator); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | public function benchContainsEnumeratorFalse() |
||
| 89 | { |
||
| 90 | foreach ($this->enumerators as $enumerator) { |
||
| 91 | $this->fullSet->contains($enumerator); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | public function benchContainsValueTrue() |
||
| 96 | { |
||
| 97 | foreach ($this->values as $value) { |
||
| 98 | $this->fullSet->contains($value); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | public function benchContainsValueFalse() |
||
| 103 | { |
||
| 104 | foreach ($this->values as $value) { |
||
| 105 | $this->fullSet->contains($value); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | public function benchIterateFull() |
||
| 110 | { |
||
| 111 | foreach ($this->fullSet as $enumerator) { |
||
| 112 | $enumerator->getValue(); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | public function benchIterateEmpty() |
||
| 117 | { |
||
| 118 | foreach ($this->emptySet as $enumerator) { |
||
| 119 | $enumerator->getValue(); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | public function benchCountFull() |
||
| 124 | { |
||
| 125 | $this->fullSet->count(); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function benchCountEmpty() |
||
| 129 | { |
||
| 130 | $this->emptySet->count(); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function benchIsEqual() |
||
| 134 | { |
||
| 135 | $this->fullSet->isEqual($this->fullSet); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function benchIsSubset() |
||
| 139 | { |
||
| 140 | $this->fullSet->isEqual($this->fullSet); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function benchIsSuperset() |
||
| 144 | { |
||
| 145 | $this->fullSet->isSuperset($this->fullSet); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function benchUnion() |
||
| 149 | { |
||
| 150 | $this->fullSet->union($this->emptySet); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function benchIntersect() |
||
| 154 | { |
||
| 155 | $this->fullSet->intersect($this->emptySet); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function benchDiff() |
||
| 159 | { |
||
| 160 | $this->fullSet->diff($this->emptySet); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function benchSymDiff() |
||
| 164 | { |
||
| 165 | $this->fullSet->symDiff($this->emptySet); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function benchGetOrdinals() |
||
| 169 | { |
||
| 170 | $this->fullSet->getOrdinals(); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function benchGetValues() |
||
| 174 | { |
||
| 175 | $this->fullSet->getValues(); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function benchGetNames() |
||
| 179 | { |
||
| 180 | $this->fullSet->getNames(); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function benchGetEnumerators() |
||
| 184 | { |
||
| 185 | $this->fullSet->getEnumerators(); |
||
| 186 | } |
||
| 187 | } |
||
| 188 |