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 |
||
| 8 | class Component_composite |
||
| 9 | extends AbstractComponent |
||
|
|
|||
| 10 | implements ValueComponentInterface, |
||
| 11 | DisableableComponentInterface, |
||
| 12 | FilterableComponentInterface, |
||
| 13 | ValidatableComponentInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The list of child components. |
||
| 17 | * |
||
| 18 | * @var UI\AbstractComponent[] |
||
| 19 | */ |
||
| 20 | private $components; |
||
| 21 | |||
| 22 | public $component_type = 'composite'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The __set magic method is overridden here to apply value & name changes to |
||
| 26 | * child components. |
||
| 27 | */ |
||
| 28 | public function __set( $name, $value ) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Apply the value to each of the child components. |
||
| 45 | * |
||
| 46 | * @param array $value |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | public function set_value( array $value ) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Apply the new name to each of the child components. |
||
| 60 | * |
||
| 61 | * @param string $name |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | public function set_name( $name ) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | public function default_model() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function required_arguments() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | public function get_template_path() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Parse the template by converting the name tokens into UI components. |
||
| 107 | * |
||
| 108 | * @return string The parsed template |
||
| 109 | */ |
||
| 110 | public function parse_template() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * If this is the root composite component, this will return the component's |
||
| 120 | * name. If this is a child composite component, this will return the |
||
| 121 | * component's name as a key in the parent's array, i.e 'parent_name[child_name]' |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function get_name() |
||
| 133 | |||
| 134 | public function filter($v) |
||
| 148 | |||
| 149 | public function validation($v,&$e) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Instantiate child UI components when created. |
||
| 168 | */ |
||
| 169 | protected function on_created() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * |
||
| 179 | * @param type $args |
||
| 180 | * @return type |
||
| 181 | */ |
||
| 182 | private function create_component( $args ) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get a child component by name. |
||
| 201 | * |
||
| 202 | * @param string $name |
||
| 203 | * @return UI\AbstractComponent |
||
| 204 | * @throws \RuntimeException If there's no child component corresponding to the given name |
||
| 205 | */ |
||
| 206 | private function get_component( $name ) |
||
| 214 | } |