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 |
||
9 | class LSX_Sensei_Lesson { |
||
10 | |||
11 | /** |
||
12 | * Instance of class. |
||
13 | * |
||
14 | * @var self |
||
15 | */ |
||
16 | private static $instance; |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | */ |
||
21 | public function __construct() { |
||
26 | |||
27 | /** |
||
28 | * Fetches an instance of the class. |
||
29 | * |
||
30 | * @return self |
||
31 | */ |
||
32 | public static function instance() { |
||
38 | |||
39 | /** |
||
40 | * Run our changes. |
||
41 | */ |
||
42 | public function init() { |
||
46 | |||
47 | /** |
||
48 | * Register a sidebar when Sensei Participants or Sensei Progress plugins are active. |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | public function lsx_widget_area_sensei_init() { |
||
64 | |||
65 | public function lsx_widget_area_sensei_is_active( $classes ) { |
||
73 | |||
74 | /** |
||
75 | * Adds the widget content to the lesson template if the lsx-sensei-sidebar is active. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function lsx_sensei_lesson_sidebar() { |
||
90 | |||
91 | } // End Class |
||
92 | new LSX_Sensei_Lesson(); |
||
93 |
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.