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 |
||
23 | class WordPoints_App { |
||
24 | |||
25 | /** |
||
26 | * The main app. |
||
27 | * |
||
28 | * @since 1.0.0 |
||
29 | * |
||
30 | * @var WordPoints_App |
||
31 | */ |
||
32 | public static $main; |
||
33 | |||
34 | /** |
||
35 | * The slug of this app. |
||
36 | * |
||
37 | * @since 1.0.0 |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $slug; |
||
42 | |||
43 | /** |
||
44 | * The full slug of this app, prefixed with the slug of the parent app. |
||
45 | * |
||
46 | * @since 1.0.0 |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $full_slug; |
||
51 | |||
52 | /** |
||
53 | * A registry for child apps. |
||
54 | * |
||
55 | * @since 1.0.0 |
||
56 | * |
||
57 | * @var WordPoints_Class_Registry_Persistent |
||
58 | */ |
||
59 | protected $sub_apps; |
||
60 | |||
61 | /** |
||
62 | * The parent of this app, if this is a sub-app. |
||
63 | * |
||
64 | * @since 1.0.0 |
||
65 | * |
||
66 | * @var WordPoints_App |
||
67 | */ |
||
68 | protected $parent; |
||
69 | |||
70 | /** |
||
71 | * Whether to skip calling an action when each registry is initialized. |
||
72 | * |
||
73 | * @since 1.0.0 |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | protected $silent = false; |
||
78 | |||
79 | /** |
||
80 | * Keeps track of which registries have been initialized. |
||
81 | * |
||
82 | * @since 1.0.0 |
||
83 | * |
||
84 | * @var bool[] |
||
85 | */ |
||
86 | protected $did_init = array(); |
||
87 | |||
88 | /** |
||
89 | * Whether properties are allowed to be set. |
||
90 | * |
||
91 | * @since 1.0.0 |
||
92 | * |
||
93 | * @var bool |
||
94 | */ |
||
95 | protected $allow_set = false; |
||
96 | |||
97 | /** |
||
98 | * @since 1.0.0 |
||
99 | */ |
||
100 | public function __construct( $slug, $parent = null ) { |
||
117 | |||
118 | /** |
||
119 | * @since 1.0.0 |
||
120 | */ |
||
121 | public function __isset( $var ) { |
||
124 | |||
125 | /** |
||
126 | * @since 1.0.0 |
||
127 | */ |
||
128 | public function __get( $var ) { |
||
194 | |||
195 | /** |
||
196 | * @since 1.0.0 |
||
197 | */ |
||
198 | public function __set( $var, $value ) { |
||
210 | |||
211 | /** |
||
212 | * @since 1.0.0 |
||
213 | */ |
||
214 | public function __unset( $var ) { |
||
226 | |||
227 | /** |
||
228 | * Check whether to call the init action for a registry sub-app. |
||
229 | * |
||
230 | * @since 1.0.0 |
||
231 | * |
||
232 | * @param object $registry The sub-app object. |
||
233 | * |
||
234 | * @return bool Whether to call the init action or not. |
||
235 | */ |
||
236 | protected function should_do_registry_init( $registry ) { |
||
242 | |||
243 | /** |
||
244 | * Initialize this app. |
||
245 | * |
||
246 | * @since 1.0.0 |
||
247 | */ |
||
248 | protected function init() { |
||
262 | } |
||
263 | |||
265 |
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.