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 |
||
| 14 | trait ArgumentsAwareConfigTrait |
||
| 15 | { |
||
| 16 | protected $arguments = []; |
||
| 17 | protected $_isArgumentsBuilt; |
||
| 18 | |||
| 19 | 102 | public function buildArguments() |
|
| 20 | { |
||
| 21 | 102 | if ($this->_isArgumentsBuilt) { |
|
| 22 | return; |
||
| 23 | } |
||
| 24 | |||
| 25 | 102 | if (!empty($this->data['args'])) { |
|
| 26 | 66 | $this->addArguments($this->data['args']); |
|
|
|
|||
| 27 | } |
||
| 28 | 102 | $this->_isArgumentsBuilt = true; |
|
| 29 | 102 | } |
|
| 30 | |||
| 31 | 67 | public function addArguments($argsList) |
|
| 32 | { |
||
| 33 | 67 | foreach ($argsList as $argumentName => $argumentInfo) { |
|
| 34 | 67 | if ($argumentInfo instanceof InputField) { |
|
| 35 | 22 | $this->arguments[$argumentInfo->getName()] = $argumentInfo; |
|
| 36 | 22 | continue; |
|
| 37 | } else { |
||
| 38 | 61 | $this->addArgument($argumentName, $this->buildConfig($argumentName, $argumentInfo)); |
|
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | 67 | return $this; |
|
| 43 | } |
||
| 44 | |||
| 45 | 75 | public function addArgument($argument, $argumentInfo = null) |
|
| 46 | { |
||
| 47 | 75 | if (!($argument instanceof InputField)) { |
|
| 48 | 65 | $argument = new InputField($this->buildConfig($argument, $argumentInfo)); |
|
| 49 | } |
||
| 50 | 75 | $this->arguments[$argument->getName()] = $argument; |
|
| 51 | |||
| 52 | 75 | return $this; |
|
| 53 | } |
||
| 54 | |||
| 55 | 65 | View Code Duplication | protected function buildConfig($name, $info = null) |
| 56 | { |
||
| 57 | 65 | if (!is_array($info)) { |
|
| 58 | return [ |
||
| 59 | 50 | 'type' => $info, |
|
| 60 | 50 | 'name' => $name |
|
| 61 | ]; |
||
| 62 | } |
||
| 63 | 65 | if (empty($info['name'])) { |
|
| 64 | 27 | $info['name'] = $name; |
|
| 65 | } |
||
| 66 | |||
| 67 | 65 | return $info; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param $name |
||
| 72 | * |
||
| 73 | * @return InputField |
||
| 74 | */ |
||
| 75 | 38 | public function getArgument($name) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param $name |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | 40 | public function hasArgument($name) |
|
| 89 | |||
| 90 | 7 | public function hasArguments() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return InputField[] |
||
| 97 | */ |
||
| 98 | 66 | public function getArguments() |
|
| 102 | |||
| 103 | 2 | public function removeArgument($name) |
|
| 104 | { |
||
| 105 | 2 | if ($this->hasArgument($name)) { |
|
| 106 | 2 | unset($this->arguments[$name]); |
|
| 111 | |||
| 112 | } |
||
| 113 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: