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(); |
||
|
1 ignored issue
–
show
|
|||
| 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 | * Register a container with the repository |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | 5 | public function register_container( Container $container ) { |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Initialize registered containers |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | 3 | public function initialize_containers() { |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Return all containers |
||
| 66 | * |
||
| 67 | * @param string $type Container type to filter for |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function get_containers( $type = null ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Return field in a container with supplied id |
||
| 90 | * |
||
| 91 | * @param string $field_name |
||
| 92 | * @param string $container_id |
||
| 93 | * @param bool $include_nested_fields |
||
| 94 | * @return Carbon_Fields\Field\Field |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function get_field_in_container( $field_name, $container_id, $include_nested_fields = true ) { |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Return field in containers |
||
| 118 | * |
||
| 119 | * @param string $field_name |
||
| 120 | * @param string $container_type |
||
| 121 | * @param bool $include_nested_fields |
||
| 122 | * @return Carbon_Fields\Field\Field |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) { |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Return all currently active containers |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function get_active_containers() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Check if container identificator id is unique |
||
| 155 | * |
||
| 156 | * @param string $id |
||
| 157 | */ |
||
| 158 | public function is_unique_container_id( $id ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Generate a unique container identificator id based on container title |
||
| 164 | * |
||
| 165 | * @param string $title |
||
| 166 | */ |
||
| 167 | 3 | public function get_unique_container_id( $title ) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Add container identificator id to the list of unique container ids |
||
| 186 | * |
||
| 187 | * @param string $id |
||
| 188 | */ |
||
| 189 | protected function register_unique_container_id( $id ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Remove container identificator id from the list of unique container ids |
||
| 197 | * |
||
| 198 | * @param string $id |
||
| 199 | */ |
||
| 200 | protected function unregister_unique_container_id( $id ) { |
||
| 205 | } |
||
| 206 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.