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() { |
|
| 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 ) { |
||
| 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 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 |