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:
Complex classes like Valuestore often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Valuestore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Valuestore implements ArrayAccess, Countable |
||
| 9 | { |
||
| 10 | /** @var string */ |
||
| 11 | protected $fileName; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param string $fileName |
||
| 15 | * @param array|null $values |
||
| 16 | * |
||
| 17 | * @return $this |
||
| 18 | */ |
||
| 19 | public static function make(string $fileName, array $values = null) |
||
| 29 | |||
| 30 | protected function __construct() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set the filename where all values will be stored. |
||
| 36 | * |
||
| 37 | * @param string $fileName |
||
| 38 | * |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | protected function setFileName(string $fileName) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Put a value in the store. |
||
| 50 | * |
||
| 51 | * @param string|array $name |
||
| 52 | * @param string|int|null $value |
||
| 53 | * |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function put($name, $value = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * set a value in the store. |
||
| 77 | * |
||
| 78 | * @param string|array $name |
||
| 79 | * @param string|int|null $value |
||
| 80 | * |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | public function set($name, $value = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Push a new value into an array. |
||
| 90 | * |
||
| 91 | * @param string $name |
||
| 92 | * @param $pushValue |
||
| 93 | * |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function push(string $name, $pushValue) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Prepend a new value in an array. |
||
| 123 | * |
||
| 124 | * @param string $name |
||
| 125 | * @param $prependValue |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function prepend(string $name, $prependValue) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get a value from the store. |
||
| 156 | * |
||
| 157 | * @param string $name |
||
| 158 | * @param $default |
||
| 159 | * |
||
| 160 | * @return null|string |
||
| 161 | */ |
||
| 162 | public function get(string $name, $default = null) |
||
| 172 | |||
| 173 | /* |
||
| 174 | * Determine if the store has a value for the given name. |
||
| 175 | */ |
||
| 176 | public function has(string $name) : bool |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get all values from the store. |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function all() : array |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get all keys starting with a given string from the store. |
||
| 197 | * |
||
| 198 | * @param string $startingWith |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function allStartingWith(string $startingWith = '') : array |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Forget a value from the store. |
||
| 215 | * |
||
| 216 | * @param string $key |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function forget(string $key) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Flush all values from the store. |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function flush() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Flush all values which keys start with a given string. |
||
| 243 | * |
||
| 244 | * @param string $startingWith |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function flushStartingWith(string $startingWith = '') |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get and forget a value from the store. |
||
| 261 | * |
||
| 262 | * @param string $name |
||
| 263 | * |
||
| 264 | * @return null|string |
||
| 265 | */ |
||
| 266 | public function pull(string $name) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Increment a value from the store. |
||
| 277 | * |
||
| 278 | * @param string $name |
||
| 279 | * @param int $by |
||
| 280 | * |
||
| 281 | * @return int|null|string |
||
| 282 | */ |
||
| 283 | public function increment(string $name, int $by = 1) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Decrement a value from the store. |
||
| 296 | * |
||
| 297 | * @param string $name |
||
| 298 | * @param int $by |
||
| 299 | * |
||
| 300 | * @return int|null|string |
||
| 301 | */ |
||
| 302 | public function decrement(string $name, int $by = 1) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Whether a offset exists. |
||
| 309 | * |
||
| 310 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 311 | * |
||
| 312 | * @param mixed $offset |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function offsetExists($offset) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Offset to retrieve. |
||
| 323 | * |
||
| 324 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 325 | * |
||
| 326 | * @param mixed $offset |
||
| 327 | * |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | public function offsetGet($offset) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Offset to set. |
||
| 337 | * |
||
| 338 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 339 | * |
||
| 340 | * @param mixed $offset |
||
| 341 | * @param mixed $value |
||
| 342 | */ |
||
| 343 | public function offsetSet($offset, $value) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Offset to unset. |
||
| 350 | * |
||
| 351 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 352 | * |
||
| 353 | * @param mixed $offset |
||
| 354 | */ |
||
| 355 | public function offsetUnset($offset) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Count elements. |
||
| 362 | * |
||
| 363 | * @link http://php.net/manual/en/countable.count.php |
||
| 364 | * |
||
| 365 | * @return int |
||
| 366 | */ |
||
| 367 | public function count() |
||
| 371 | |||
| 372 | protected function filterKeysStartingWith(array $values, string $startsWith) : array |
||
| 378 | |||
| 379 | protected function filterKeysNotStartingWith(array $values, string $startsWith) : array |
||
| 385 | |||
| 386 | protected function startsWith(string $haystack, string $needle) : bool |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param array $values |
||
| 393 | * |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | protected function setContent(array $values) |
||
| 406 | } |
||
| 407 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.