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() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function required_arguments() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function get_template_path() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Parse the template by converting the name tokens into UI components. |
||
| 109 | * |
||
| 110 | * @return string The parsed template |
||
| 111 | */ |
||
| 112 | public function parse_template() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * If this is the root composite component, this will return the component's |
||
| 123 | * name. If this is a child composite component, this will return the |
||
| 124 | * component's name as a key in the parent's array, i.e 'parent_name[child_name]' |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function get_name() |
||
| 136 | |||
| 137 | public function filter($v) |
||
| 151 | |||
| 152 | public function validation($v,&$e) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get a child component by name. |
||
| 171 | * |
||
| 172 | * @param string $name |
||
| 173 | * @return UI\AbstractComponent |
||
| 174 | * @throws \RuntimeException If there's no child component corresponding to the given name |
||
| 175 | */ |
||
| 176 | public function get_component( $name ) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Instantiate child UI components when created. |
||
| 187 | */ |
||
| 188 | protected function on_created() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * |
||
| 198 | * @param type $args |
||
| 199 | * @return type |
||
| 200 | */ |
||
| 201 | private function create_component( $args ) |
||
| 217 | } |