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) |
|
| 44 | { |
||
| 45 | 112 | if (empty($configData)) { |
|
| 46 | 3 | throw new ConfigurationException('Config for Type should be an array'); |
|
| 47 | } |
||
| 48 | |||
| 49 | 109 | $this->contextObject = $contextObject; |
|
| 50 | 109 | $this->data = $configData; |
|
| 51 | 109 | $this->finalClass = $finalClass; |
|
| 52 | |||
| 53 | // $this->validate(); |
||
|
|
|||
| 54 | 109 | $this->build(); |
|
| 55 | 109 | } |
|
| 56 | |||
| 57 | public function validate() |
||
| 58 | { |
||
| 59 | $validator = ConfigValidator::getInstance(); |
||
| 60 | |||
| 61 | if (!$validator->validate($this->data, $this->getContextRules(), $this->extraFieldsAllowed)) { |
||
| 62 | throw new ConfigurationException('Config is not valid for ' . ($this->contextObject ? get_class($this->contextObject) : null) . "\n" . implode("\n", $validator->getErrorsArray(false))); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | 1 | public function getContextRules() |
|
| 67 | { |
||
| 68 | 1 | $rules = $this->getRules(); |
|
| 69 | 1 | View Code Duplication | if ($this->finalClass) { |
| 70 | 1 | foreach ($rules as $name => $info) { |
|
| 71 | 1 | if (!empty($info['final'])) { |
|
| 72 | 1 | $rules[$name]['required'] = true; |
|
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | 1 | return $rules; |
|
| 78 | } |
||
| 79 | |||
| 80 | abstract public function getRules(); |
||
| 81 | |||
| 82 | 58 | public function getName() |
|
| 86 | |||
| 87 | 3 | public function getType() |
|
| 91 | |||
| 92 | 52 | public function getData() |
|
| 93 | { |
||
| 94 | 52 | return $this->data; |
|
| 95 | } |
||
| 96 | |||
| 97 | 20 | public function getContextObject() |
|
| 98 | { |
||
| 99 | 20 | return $this->contextObject; |
|
| 100 | } |
||
| 101 | |||
| 102 | 52 | public function isFinalClass() |
|
| 103 | { |
||
| 104 | 52 | return $this->finalClass; |
|
| 105 | } |
||
| 106 | |||
| 107 | 52 | public function isExtraFieldsAllowed() |
|
| 108 | { |
||
| 109 | 52 | return $this->extraFieldsAllowed; |
|
| 110 | } |
||
| 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) |
|
| 143 | { |
||
| 144 | 2 | if (substr($method, 0, 3) == 'get') { |
|
| 145 | 1 | $propertyName = lcfirst(substr($method, 3)); |
|
| 146 | 2 | View Code Duplication | } elseif (substr($method, 0, 3) == 'set') { |
| 147 | 1 | $propertyName = lcfirst(substr($method, 3)); |
|
| 148 | 1 | $this->set($propertyName, $arguments[0]); |
|
| 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.