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 User_Meta_Container extends Container { |
||
9 | protected $user_id; |
||
10 | |||
11 | public $settings = array( |
||
12 | 'show_on' => array( |
||
13 | 'role' => array() |
||
14 | ) |
||
15 | ); |
||
16 | |||
17 | /** |
||
18 | * Create a new user meta container |
||
19 | * |
||
20 | * @param string $title Unique title of the container |
||
21 | **/ |
||
22 | 1 | public function __construct( $title ) { |
|
29 | |||
30 | /** |
||
31 | * Bind attach() and save() to the appropriate WordPress actions. |
||
32 | **/ |
||
33 | public function init() { |
||
38 | |||
39 | /** |
||
40 | * Perform save operation after successful is_valid_save() check. |
||
41 | * The call is propagated to all fields in the container. |
||
42 | * |
||
43 | * @param int $user_id ID of the user against which save() is ran |
||
44 | **/ |
||
45 | View Code Duplication | public function save( $user_id ) { |
|
58 | |||
59 | /** |
||
60 | * Checks whether the current request is valid |
||
61 | * |
||
62 | * @return bool |
||
63 | **/ |
||
64 | public function is_valid_save( $user_id = 0 ) { |
||
73 | |||
74 | /** |
||
75 | * Perform checks whether the current save() request is valid |
||
76 | * |
||
77 | * @param int $user_id ID of the user against which save() is ran |
||
78 | * @return bool |
||
79 | **/ |
||
80 | public function is_valid_save_conditions( $user_id ) { |
||
103 | |||
104 | /** |
||
105 | * Show the container only on users who have the $role role. |
||
106 | * |
||
107 | * @param string $role |
||
108 | * @return object $this |
||
109 | **/ |
||
110 | public function show_on_user_role( $role ) { |
||
115 | |||
116 | /** |
||
117 | * Add the container to the user |
||
118 | **/ |
||
119 | public function attach() { |
||
124 | |||
125 | /** |
||
126 | * Whether we're on the user profile page |
||
127 | **/ |
||
128 | public function is_profile_page() { |
||
133 | |||
134 | /** |
||
135 | * Perform checks whether the container should be attached during the current request |
||
136 | * |
||
137 | * @return bool True if the container is allowed to be attached |
||
138 | **/ |
||
139 | public function is_valid_attach() { |
||
146 | |||
147 | /** |
||
148 | * Output the container markup |
||
149 | **/ |
||
150 | public function render( $user_profile = null ) { |
||
164 | |||
165 | /** |
||
166 | * Set the user ID the container will operate with. |
||
167 | * |
||
168 | * @param int $user_id |
||
169 | **/ |
||
170 | public function set_user_id( $user_id ) { |
||
174 | } |
||
175 |
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.