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 |
||
| 9 | class ComponentFactory |
||
| 10 | { |
||
|
|
|||
| 11 | /** |
||
| 12 | * @var array A list of user-registered custom components |
||
| 13 | */ |
||
| 14 | private static $registered_components = array(); |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Create a UI component instance. |
||
| 18 | * |
||
| 19 | * @param string $type |
||
| 20 | * @param array $props |
||
| 21 | * @return AbstractComponent |
||
| 22 | */ |
||
| 23 | public static function create( $type, array $props ) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Register a custom component class. If the given component name is similar |
||
| 36 | * to the name of one of the core components, it will override it. If a |
||
| 37 | * custom component with a similar name has already been registered, an |
||
| 38 | * exception will be thrown. |
||
| 39 | * |
||
| 40 | * @param string $type |
||
| 41 | * @param string $class_name |
||
| 42 | * @throws \RuntimeException |
||
| 43 | */ |
||
| 44 | View Code Duplication | public static function register( $type, $class_name ) |
|
| 52 | |||
| 53 | public static function get_url( $path ) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * |
||
| 61 | * @param type $type |
||
| 62 | * @param type $props |
||
| 63 | * @throws \RuntimeException |
||
| 64 | */ |
||
| 65 | private static function create_core_component( $type, $props ) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * |
||
| 88 | * @param type $type |
||
| 89 | * @param type $props |
||
| 90 | * @throws \RuntimeException |
||
| 91 | */ |
||
| 92 | View Code Duplication | private static function create_registered_component( $type, $props ) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Private constructor to prevent instantiation |
||
| 105 | */ |
||
| 106 | private function __construct() {} |
||
| 107 | } |