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 |
||
8 | class Config |
||
9 | { |
||
10 | const ID = 'config'; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | public $hook; |
||
16 | |||
17 | /** |
||
18 | * @var Application |
||
19 | */ |
||
20 | protected $app; |
||
21 | |||
22 | /** |
||
23 | * @return string |
||
24 | */ |
||
25 | public static function id() |
||
29 | |||
30 | public function __construct( Application $app ) |
||
34 | |||
35 | /** |
||
36 | * @return void |
||
37 | */ |
||
38 | public function init() |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | * @callback register_setting |
||
50 | */ |
||
51 | public function filterSavedSettings( array $settings ) |
||
55 | |||
56 | /** |
||
57 | * @return void |
||
58 | * @action admin_menu |
||
59 | */ |
||
60 | public function registerPage() |
||
71 | |||
72 | /** |
||
73 | * @return void |
||
74 | * @action admin_menu |
||
75 | */ |
||
76 | public function registerSetting() |
||
80 | |||
81 | /** |
||
82 | * @return void |
||
83 | * @callback add_submenu_page |
||
84 | */ |
||
85 | public function renderPage() |
||
100 | |||
101 | /** |
||
102 | * @return void |
||
103 | * @action pollux/{static::ID}/init |
||
104 | */ |
||
105 | View Code Duplication | public function resetPage() |
|
121 | } |
||
122 |
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.