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 changes to |
||
| 26 | * child components. |
||
| 27 | */ |
||
| 28 | public function __set( $name, $value ) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Apply the value to each of the child components. |
||
| 40 | * |
||
| 41 | * @param array $value |
||
| 42 | */ |
||
| 43 | public function set_value( array $value ) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | public function default_model() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function required_arguments() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | public function get_template_path() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Parse the template by converting the name tokens into UI components. |
||
| 87 | * |
||
| 88 | * @return string The parsed template |
||
| 89 | */ |
||
| 90 | public function parse_template() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * If this is the root composite component, this will return the component's |
||
| 100 | * name. If this is a child composite component, this will return the |
||
| 101 | * component's name as a key in the parent's array, i.e 'parent_name[child_name]' |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function get_name() |
||
| 113 | |||
| 114 | public function filter($v) |
||
| 128 | |||
| 129 | public function validation($v,&$e) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Instantiate child UI components when created. |
||
| 148 | */ |
||
| 149 | protected function on_created() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * |
||
| 159 | * @param type $args |
||
| 160 | * @return type |
||
| 161 | */ |
||
| 162 | private function create_component( $args ) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get a child component by name. |
||
| 181 | * |
||
| 182 | * @param string $name |
||
| 183 | * @return UI\AbstractComponent |
||
| 184 | * @throws \RuntimeException If there's no child component corresponding to the given name |
||
| 185 | */ |
||
| 186 | View Code Duplication | private function get_component( $name ) |
|
| 194 | } |