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 EnumMap extends SplObjectStorage |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * key()-behaviour: return the iterator position |
||
| 21 | */ |
||
| 22 | const KEY_AS_INDEX = 1; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * key()-behaviour: return the name of the current element |
||
| 26 | */ |
||
| 27 | const KEY_AS_NAME = 2; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * key()-behaviour: return the value of the current element |
||
| 31 | */ |
||
| 32 | const KEY_AS_VALUE = 3; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * key()-behaviour: return the ordinal number of the current element |
||
| 36 | */ |
||
| 37 | const KEY_AS_ORDINAL = 4; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * current()-behaviour: return the instance of the enumerator |
||
| 41 | */ |
||
| 42 | const CURRENT_AS_ENUM = 8; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * current()-behaviour: return the data mapped the enumerator |
||
| 46 | */ |
||
| 47 | const CURRENT_AS_DATA = 16; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * current()-behaviour: return the name of the enumerator |
||
| 51 | */ |
||
| 52 | const CURRENT_AS_NAME = 24; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * current()-behaviour: return the value of the enumerator |
||
| 56 | */ |
||
| 57 | const CURRENT_AS_VALUE = 32; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * current()-behaviour: return the ordinal number of the enumerator |
||
| 61 | */ |
||
| 62 | const CURRENT_AS_ORDINAL = 40; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The classname of the enumeration type |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $enumeration; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Flags to define behaviors |
||
| 72 | * (Default = KEY_AS_INDEX | CURRENT_AS_ENUM) |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | private $flags = 9; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Constructor |
||
| 79 | * @param string $enumeration The classname of the enumeration type |
||
| 80 | * @param int|null $flags Behaviour flags, see KEY_AS_* and CURRENT_AS_* constants |
||
| 81 | * @throws InvalidArgumentException |
||
| 82 | */ |
||
| 83 | 13 | public function __construct($enumeration, $flags = null) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get the classname of the enumeration |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 1 | public function getEnumeration() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Set behaviour flags |
||
| 109 | * see KEY_AS_* and CURRENT_AS_* constants |
||
| 110 | * @param int $flags |
||
| 111 | * @return void |
||
| 112 | * @throws InvalidArgumentException On invalid or unsupported flags |
||
| 113 | */ |
||
| 114 | 3 | public function setFlags($flags) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get the behaviour flags |
||
| 141 | * @return int |
||
| 142 | */ |
||
| 143 | 1 | public function getFlags() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Attach a new enumerator or overwrite an existing one |
||
| 150 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 151 | * @param mixed $data |
||
| 152 | * @return void |
||
| 153 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 154 | */ |
||
| 155 | 6 | public function attach($enumerator, $data = null) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Test if the given enumerator exists |
||
| 163 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | 5 | public function contains($enumerator) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Detach an enumerator |
||
| 179 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 180 | * @return void |
||
| 181 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 182 | */ |
||
| 183 | 2 | public function detach($enumerator) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get a unique identifier for the given enumerator |
||
| 191 | * @param Enum|scalar $enumerator |
||
| 192 | * @return string |
||
| 193 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 194 | */ |
||
| 195 | 8 | public function getHash($enumerator) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Test if the given enumerator exists |
||
| 204 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 205 | * @return boolean |
||
| 206 | * @see contains() |
||
| 207 | */ |
||
| 208 | 3 | public function offsetExists($enumerator) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get mapped data for the given enumerator |
||
| 215 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 216 | * @return mixed |
||
| 217 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 218 | */ |
||
| 219 | 4 | public function offsetGet($enumerator) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Attach a new enumerator or overwrite an existing one |
||
| 227 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 228 | * @param mixed $data |
||
| 229 | * @return void |
||
| 230 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 231 | * @see attach() |
||
| 232 | */ |
||
| 233 | 3 | public function offsetSet($enumerator, $data = null) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Detach an existing enumerator |
||
| 241 | * @param Enum|null|boolean|int|float|string $enumerator |
||
| 242 | * @return void |
||
| 243 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 244 | * @see detach() |
||
| 245 | */ |
||
| 246 | 2 | public function offsetUnset($enumerator) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get the current item |
||
| 254 | * The return value varied by the behaviour of the current flag |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | 3 | View Code Duplication | public function current() |
| 274 | |||
| 275 | /** |
||
| 276 | * Get the current item-key |
||
| 277 | * The return value varied by the behaviour of the key flag |
||
| 278 | * @return null|boolean|int|float|string |
||
| 279 | */ |
||
| 280 | 3 | View Code Duplication | public function key() |
| 295 | } |
||
| 296 |