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 Term_Meta_Container extends Container { |
||
11 | protected $term_id; |
||
12 | |||
13 | public $settings = array( |
||
14 | // TODO remove |
||
15 | 'taxonomy' => array( 'category' ), |
||
16 | 'show_on_level' => false, |
||
17 | ); |
||
18 | |||
19 | /** |
||
20 | * Array of condition types that are checked during save requests |
||
21 | * |
||
22 | * @var array<string> |
||
23 | */ |
||
24 | protected $static_conditions = array( 'term', 'term_taxonomy' ); |
||
25 | |||
26 | /** |
||
27 | * Array of condition types that are checked during edit requests |
||
28 | * |
||
29 | * @var array<string> |
||
30 | */ |
||
31 | protected $dynamic_conditions = array( 'term_level' ); |
||
32 | |||
33 | /** |
||
34 | * Create a new container |
||
35 | * |
||
36 | * @param string $unique_id Unique id of the container |
||
37 | * @param string $title title of the container |
||
38 | * @param string $type Type of the container |
||
39 | **/ |
||
40 | View Code Duplication | public function __construct( $unique_id, $title, $type ) { |
|
47 | |||
48 | /** |
||
49 | * Bind attach() and save() to the appropriate WordPress actions. |
||
50 | **/ |
||
51 | public function init() { |
||
64 | |||
65 | /** |
||
66 | * Perform checks whether the current save() request is valid. |
||
67 | * |
||
68 | * @param int $term_id ID of the term against which save() is ran |
||
69 | * @return bool |
||
70 | **/ |
||
71 | public function is_valid_save( $term_id = null ) { |
||
78 | |||
79 | /** |
||
80 | * Perform save operation after successful is_valid_save() check. |
||
81 | * The call is propagated to all fields in the container. |
||
82 | * |
||
83 | * @param int $term_id ID of the term against which save() is ran |
||
84 | **/ |
||
85 | View Code Duplication | public function save( $term_id = null ) { |
|
95 | |||
96 | /** |
||
97 | * Get environment array for page request (in admin) |
||
98 | * |
||
99 | * @return array |
||
100 | **/ |
||
101 | protected function get_environment_for_request() { |
||
115 | |||
116 | /** |
||
117 | * Perform checks whether the container should be attached during the current request |
||
118 | * |
||
119 | * @return bool True if the container is allowed to be attached |
||
120 | **/ |
||
121 | View Code Duplication | public function is_valid_attach_for_request() { |
|
136 | |||
137 | /** |
||
138 | * Get environment array for object id |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function get_environment_for_object( $object_id ) { |
||
151 | |||
152 | /** |
||
153 | * Check container attachment rules against object id |
||
154 | * |
||
155 | * @param int $object_id |
||
156 | * @return bool |
||
157 | **/ |
||
158 | public function is_valid_attach_for_object( $object_id = null ) { |
||
173 | |||
174 | /** |
||
175 | * Add term meta for each of the container taxonomies |
||
176 | **/ |
||
177 | public function attach() { |
||
183 | |||
184 | /** |
||
185 | * Output the container markup |
||
186 | **/ |
||
187 | public function render( $term = null ) { |
||
194 | |||
195 | /** |
||
196 | * Set the term ID the container will operate with. |
||
197 | * |
||
198 | * @param int $term_id |
||
199 | **/ |
||
200 | public function set_term_id( $term_id ) { |
||
204 | |||
205 | /** |
||
206 | * Get array of taxonomies this container can appear on conditionally |
||
207 | * |
||
208 | * @return array<string> |
||
209 | */ |
||
210 | View Code Duplication | public function get_taxonomy_visibility() { |
|
225 | |||
226 | /** |
||
227 | * COMMON USAGE METHODS |
||
228 | */ |
||
229 | |||
230 | /** |
||
231 | * Show the container only on terms from the specified taxonomies. |
||
232 | * |
||
233 | * @deprecated |
||
234 | * @param string|array $taxonomies |
||
235 | * @return object $this |
||
236 | **/ |
||
237 | public function show_on_taxonomy( $taxonomies ) { |
||
242 | |||
243 | /** |
||
244 | * Show the container only on particular term level. |
||
245 | * |
||
246 | * @deprecated |
||
247 | * @param int $term_level |
||
248 | * @return object $this |
||
249 | */ |
||
250 | public function show_on_level( $term_level ) { |
||
254 | } |
||
255 |
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.