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 |
||
| 20 | abstract class AbstractConfig |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $data = []; |
||
| 27 | |||
| 28 | protected $contextObject; |
||
| 29 | |||
| 30 | protected $finalClass = false; |
||
| 31 | |||
| 32 | protected $extraFieldsAllowed = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * TypeConfig constructor. |
||
| 36 | * @param array $configData |
||
| 37 | * @param mixed $contextObject |
||
| 38 | * @param bool $finalClass |
||
| 39 | * |
||
| 40 | * @throws ConfigurationException |
||
| 41 | * @throws ValidationException |
||
| 42 | */ |
||
| 43 | 112 | public function __construct(array $configData, $contextObject = null, $finalClass = false) |
|
| 56 | |||
| 57 | public function validate() |
||
| 65 | |||
| 66 | 1 | public function getContextRules() |
|
| 79 | |||
| 80 | abstract public function getRules(); |
||
| 81 | |||
| 82 | 58 | public function getName() |
|
| 86 | |||
| 87 | 3 | public function getType() |
|
| 91 | |||
| 92 | 52 | public function getData() |
|
| 96 | |||
| 97 | 20 | public function getContextObject() |
|
| 101 | |||
| 102 | 52 | public function isFinalClass() |
|
| 106 | |||
| 107 | 52 | public function isExtraFieldsAllowed() |
|
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @return null|callable |
||
| 115 | */ |
||
| 116 | 26 | public function getResolveFunction() |
|
| 120 | |||
| 121 | 67 | protected function build() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param $key |
||
| 127 | * @param null $defaultValue |
||
| 128 | * @return mixed|null|callable |
||
| 129 | */ |
||
| 130 | 76 | public function get($key, $defaultValue = null) |
|
| 134 | |||
| 135 | 9 | public function set($key, $value) |
|
| 141 | |||
| 142 | 2 | public function __call($method, $arguments) |
|
| 159 | |||
| 160 | |||
| 161 | } |
||
| 162 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.