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 | 100 | public function buildArguments() |
|
30 | |||
31 | 65 | public function addArguments($argsList) |
|
44 | |||
45 | 73 | public function addArgument($argument, $argumentInfo = null) |
|
54 | |||
55 | 63 | View Code Duplication | protected function buildConfig($name, $info = null) |
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 | 5 | public function hasArguments() |
|
94 | |||
95 | /** |
||
96 | * @return InputField[] |
||
97 | */ |
||
98 | 64 | public function getArguments() |
|
102 | |||
103 | 2 | public function removeArgument($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: