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 |
||
11 | class Repository { |
||
12 | /** |
||
13 | * List of registered unique container ids |
||
14 | * |
||
15 | * @see get_unique_container_id() |
||
16 | * @see register_unique_container_id() |
||
17 | * @see unregister_unique_container_id() |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $registered_container_ids = array(); |
||
21 | |||
22 | /** |
||
23 | * List of registered containers that should be initialized |
||
24 | * |
||
25 | * @see initialize_containers() |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $pending_containers = array(); |
||
29 | |||
30 | /** |
||
31 | * List of all containers |
||
32 | * |
||
33 | * @see _attach() |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $containers = array(); |
||
37 | |||
38 | /** |
||
39 | * Container id prefix |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $container_id_prefix = 'carbon_fields_container_'; |
||
44 | |||
45 | /** |
||
46 | * Container id prefix |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $widget_id_wildcard_suffix = '-__i__'; |
||
51 | |||
52 | /** |
||
53 | * Register a container with the repository |
||
54 | * |
||
55 | * @param Container $container |
||
56 | */ |
||
57 | 5 | public function register_container( Container $container ) { |
|
62 | |||
63 | /** |
||
64 | * Initialize registered containers |
||
65 | * |
||
66 | * @return Container[] |
||
67 | */ |
||
68 | 3 | public function initialize_containers() { |
|
77 | |||
78 | /** |
||
79 | * Return all containers |
||
80 | * |
||
81 | * @param string $type Container type to filter for |
||
82 | * @return Container[] |
||
83 | */ |
||
84 | public function get_containers( $type = null ) { |
||
101 | |||
102 | /** |
||
103 | * Return field in a container with supplied id |
||
104 | * |
||
105 | * @param string $field_name |
||
106 | * @param string $container_id |
||
107 | * @param bool $include_nested_fields |
||
108 | * @return \Carbon_Fields\Field\Field |
||
109 | */ |
||
110 | View Code Duplication | public function get_field_in_container( $field_name, $container_id, $include_nested_fields = true ) { |
|
129 | |||
130 | /** |
||
131 | * Return field in containers |
||
132 | * |
||
133 | * @param string $field_name |
||
134 | * @param string $container_type |
||
135 | * @param bool $include_nested_fields |
||
136 | * @return \Carbon_Fields\Field\Field |
||
137 | */ |
||
138 | View Code Duplication | public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) { |
|
155 | |||
156 | /** |
||
157 | * Return all currently active containers |
||
158 | * |
||
159 | * @return Container[] |
||
160 | */ |
||
161 | public function get_active_containers() { |
||
167 | |||
168 | /** |
||
169 | * Check if container identificator id is unique |
||
170 | * |
||
171 | * @param string $id |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function is_unique_container_id( $id ) { |
||
177 | |||
178 | /** |
||
179 | * Generate a unique container identificator id based on container title |
||
180 | * |
||
181 | * @param string $title |
||
182 | * @return string |
||
183 | */ |
||
184 | 3 | public function get_unique_container_id( $title ) { |
|
215 | |||
216 | /** |
||
217 | * Add container identificator id to the list of unique container ids |
||
218 | * |
||
219 | * @param string $id |
||
220 | */ |
||
221 | protected function register_unique_container_id( $id ) { |
||
226 | |||
227 | /** |
||
228 | * Remove container identificator id from the list of unique container ids |
||
229 | * |
||
230 | * @param string $id |
||
231 | */ |
||
232 | protected function unregister_unique_container_id( $id ) { |
||
237 | } |
||
238 |