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 |
||
10 | class Archive extends Settings |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | CONST ID = 'archives'; |
||
16 | |||
17 | /** |
||
18 | * {@inheritdoc} |
||
19 | */ |
||
20 | public function init() |
||
28 | |||
29 | /** |
||
30 | * @return void |
||
31 | * @action pollux/archives/saved |
||
32 | */ |
||
33 | public function addCustomNoticeOnSave() |
||
37 | |||
38 | /** |
||
39 | * @return void |
||
40 | * @action admin_menu |
||
41 | */ |
||
42 | View Code Duplication | public function addPage() |
|
55 | |||
56 | /** |
||
57 | * @param string $instruction |
||
58 | * @param string $fieldId |
||
59 | * @param string $metaboxId |
||
60 | * @return string |
||
61 | * @action pollux/{static::ID}/instruction |
||
62 | */ |
||
63 | public function filterInstruction( $instruction, $fieldId, $metaboxId ) |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | * @action pollux/{static::ID}/metabox/submit |
||
71 | */ |
||
72 | public function filterSubmitMetaBox( array $args ) |
||
77 | |||
78 | /** |
||
79 | * @return void |
||
80 | * @action pollux/archives/editor |
||
81 | */ |
||
82 | public function renderEditor( $content ) |
||
96 | |||
97 | /** |
||
98 | * @return void |
||
99 | * @callback add_menu_page |
||
100 | */ |
||
101 | public function renderPage() |
||
111 | |||
112 | /** |
||
113 | * @return array |
||
114 | */ |
||
115 | protected function getDefaults() |
||
119 | |||
120 | /** |
||
121 | * @return string|array |
||
122 | */ |
||
123 | protected function getValue( $key, $group ) |
||
127 | } |
||
128 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.