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 | * @return array |
||
56 | */ |
||
57 | 5 | public function register_container( Container $container ) { |
|
62 | |||
63 | /** |
||
64 | * Initialize registered containers |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | 3 | public function initialize_containers() { |
|
69 | 3 | $initialized_containers = array(); |
|
70 | 3 | while ( ( $container = array_shift( $this->pending_containers ) ) ) { |
|
71 | 3 | $container->init(); |
|
72 | 3 | $initialized_containers[] = $container; |
|
73 | 3 | } |
|
74 | |||
75 | 3 | return $initialized_containers; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Return all containers |
||
80 | * |
||
81 | * @param string $type Container type to filter for |
||
82 | * @return array |
||
83 | */ |
||
84 | public function get_containers( $type = null ) { |
||
85 | $raw_containers = $this->containers; |
||
86 | $containers = array(); |
||
87 | |||
88 | if ( $type === null ) { |
||
89 | $containers = $raw_containers; |
||
90 | } else { |
||
91 | $normalized_type = Helper::normalize_type( $type ); |
||
92 | foreach ( $raw_containers as $container ) { |
||
93 | if ( $container->type === $normalized_type ) { |
||
94 | $containers[] = $container; |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $containers; |
||
100 | } |
||
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 ) { |
|
111 | $containers = $this->get_containers(); |
||
112 | $field = null; |
||
113 | |||
114 | foreach ( $containers as $container ) { |
||
115 | if ( $container->get_id() !== $container_id ) { |
||
116 | continue; |
||
117 | } |
||
118 | |||
119 | if ( $include_nested_fields ) { |
||
120 | $field = $container->get_field_by_name( $field_name ); |
||
121 | } else { |
||
122 | $field = $container->get_root_field_by_name( $field_name ); |
||
123 | } |
||
124 | break; |
||
125 | } |
||
126 | |||
127 | return $field; |
||
128 | } |
||
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 ) { |
|
139 | $containers = $this->get_containers( $container_type ); |
||
140 | $field = null; |
||
141 | |||
142 | foreach ( $containers as $container ) { |
||
143 | if ( $include_nested_fields ) { |
||
144 | $field = $container->get_field_by_name( $field_name ); |
||
145 | } else { |
||
146 | $field = $container->get_root_field_by_name( $field_name ); |
||
147 | } |
||
148 | if ( $field ) { |
||
149 | break; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return $field; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Return all currently active containers |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | public function get_active_containers() { |
||
166 | |||
167 | /** |
||
168 | * Check if container identificator id is unique |
||
169 | * |
||
170 | * @param string $id |
||
171 | */ |
||
172 | public function is_unique_container_id( $id ) { |
||
175 | |||
176 | /** |
||
177 | * Generate a unique container identificator id based on container title |
||
178 | * |
||
179 | * @param string $title |
||
180 | */ |
||
181 | 3 | public function get_unique_container_id( $title ) { |
|
212 | |||
213 | /** |
||
214 | * Add container identificator id to the list of unique container ids |
||
215 | * |
||
216 | * @param string $id |
||
217 | */ |
||
218 | protected function register_unique_container_id( $id ) { |
||
223 | |||
224 | /** |
||
225 | * Remove container identificator id from the list of unique container ids |
||
226 | * |
||
227 | * @param string $id |
||
228 | */ |
||
229 | protected function unregister_unique_container_id( $id ) { |
||
234 | } |
||
235 |