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 Set |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | private $bit = 0; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var mixed[][] |
||
| 25 | */ |
||
| 26 | private static $bits = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var mixed[][] |
||
| 30 | */ |
||
| 31 | private static $keys = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $values |
||
| 35 | */ |
||
| 36 | 23 | final public function __construct(array $values = []) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @return mixed[] |
||
| 45 | */ |
||
| 46 | 5 | final public function values() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Attach the given value. |
||
| 64 | * |
||
| 65 | * @param mixed $value |
||
| 66 | */ |
||
| 67 | 23 | final public function attach($value) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Detach the given value. |
||
| 76 | * |
||
| 77 | * @param mixed $value |
||
| 78 | */ |
||
| 79 | 2 | final public function detach($value) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Given value was attached. |
||
| 88 | * |
||
| 89 | * @param mixed $value |
||
| 90 | * |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | 2 | final public function contains($value) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Is value supported. |
||
| 102 | * |
||
| 103 | * @param mixed $value |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | 23 | final public static function isValid($value) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Check if this Set is the same as other. |
||
| 114 | * |
||
| 115 | * @param Set $set |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 3 | final public function equal(Set $set) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Check if this Set is a subset of other. |
||
| 128 | * |
||
| 129 | * @param Set $set |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | 2 | final public function subset(Set $set) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Check if this Set is a superset of other. |
||
| 142 | * |
||
| 143 | * @param Set $set |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 2 | final public function superset(Set $set) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Produce a new set with enum from both this and other (this | other). |
||
| 156 | * |
||
| 157 | * @param Set ...$set Other Set(s) of the same enum to produce the union |
||
| 158 | * |
||
| 159 | * @return Set |
||
| 160 | */ |
||
| 161 | 2 | View Code Duplication | final public function union(Set $set) |
| 175 | |||
| 176 | /** |
||
| 177 | * Produce a new set with enum common to both this and other (this & other). |
||
| 178 | * |
||
| 179 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
||
| 180 | * |
||
| 181 | * @return Set |
||
| 182 | */ |
||
| 183 | 2 | View Code Duplication | final public function intersect(Set $set) |
| 197 | |||
| 198 | /** |
||
| 199 | * Produce a new set with enum in this but not in other (this - other). |
||
| 200 | * |
||
| 201 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
||
| 202 | * |
||
| 203 | * @return Set |
||
| 204 | */ |
||
| 205 | 2 | View Code Duplication | final public function diff(Set $set) |
| 219 | |||
| 220 | /** |
||
| 221 | * Produce a new set with enum in either this and other but not in both (this ^ (other | other)). |
||
| 222 | * |
||
| 223 | * @param Set ...$set Other Set(s) of the same enumeration to produce the union |
||
| 224 | * |
||
| 225 | * @return Set |
||
| 226 | */ |
||
| 227 | 2 | View Code Duplication | final public function symDiff(Set $set) |
| 241 | |||
| 242 | /** |
||
| 243 | * Get choices for checkbox group. |
||
| 244 | * |
||
| 245 | * <code> |
||
| 246 | * { |
||
| 247 | * value1: 'Readable value 1', |
||
| 248 | * value2: 'Readable value 2', |
||
| 249 | * } |
||
| 250 | * </code> |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | 1 | final public static function choices() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param mixed $value |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 2 | public static function readable($value) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param mixed $value |
||
| 281 | */ |
||
| 282 | 23 | private static function validateValue($value) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @param object $object |
||
| 291 | */ |
||
| 292 | 15 | private static function validateType($object) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $class |
||
| 302 | */ |
||
| 303 | 23 | private static function detectConstants($class) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param mixed $value |
||
| 337 | * |
||
| 338 | * @return int |
||
| 339 | */ |
||
| 340 | 23 | private function bit($value) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | 23 | private static function bits() |
|
| 355 | } |
||
| 356 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.