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 | /** |
||
54 | * |
||
55 | * @param type $type |
||
56 | * @param type $props |
||
57 | * @throws \RuntimeException |
||
58 | */ |
||
59 | private static function create_core_component( $type, $props ) |
||
79 | |||
80 | /** |
||
81 | * |
||
82 | * @param type $type |
||
83 | * @param type $props |
||
84 | * @throws \RuntimeException |
||
85 | */ |
||
86 | View Code Duplication | private static function create_registered_component( $type, $props ) |
|
96 | |||
97 | /** |
||
98 | * Private constructor to prevent instantiation |
||
99 | */ |
||
100 | private function __construct() {} |
||
101 | } |