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 |
||
19 | class ReactFactory |
||
20 | { |
||
21 | /** |
||
22 | * Build a react Event Loop. |
||
23 | * |
||
24 | * @return LoopInterface |
||
25 | */ |
||
26 | 109 | public static function buildEventLoop() |
|
30 | |||
31 | /** |
||
32 | * Build a React Dns Resolver. |
||
33 | * |
||
34 | * @param LoopInterface $loop |
||
35 | * @param string $dns |
||
36 | * |
||
37 | * @return DnsResolver |
||
38 | */ |
||
39 | 109 | public static function buildDnsResolver( |
|
47 | |||
48 | /** |
||
49 | * @param LoopInterface $loop |
||
50 | * @param DnsResolver|null $dns |
||
51 | * |
||
52 | * @return ConnectorInterface |
||
53 | */ |
||
54 | public static function buildConnector( |
||
62 | |||
63 | /** |
||
64 | * Build a React Http Client. |
||
65 | * |
||
66 | * @param LoopInterface $loop |
||
67 | * @param ConnectorInterface|DnsResolver|null $connector Passing a DnsResolver instance is deprecated. Should pass a |
||
68 | * ConnectorInterface instance. |
||
69 | * |
||
70 | * @return HttpClient |
||
71 | */ |
||
72 | 111 | public static function buildHttpClient( |
|
83 | |||
84 | /** |
||
85 | * Builds a React Http client v0.4 style |
||
86 | * |
||
87 | * @param LoopInterface $loop |
||
88 | * @param DnsResolver|null $dns |
||
89 | * |
||
90 | * @return HttpClient |
||
91 | */ |
||
92 | 111 | View Code Duplication | private static function buildHttpClient04( |
110 | |||
111 | /** |
||
112 | * Builds a React Http client v0.5 style |
||
113 | * |
||
114 | * @param LoopInterface $loop |
||
115 | * @param DnsResolver|ConnectorInterface|null $connector |
||
116 | * |
||
117 | * @return HttpClient |
||
118 | */ |
||
119 | View Code Duplication | private static function buildHttpClient05( |
|
137 | } |
||
138 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: