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:
Complex classes like ServerRequestTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ServerRequestTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait ServerRequestTrait { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $sHttpMethod; |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $sServerName; |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $sServerProtocol; |
||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $aAccept = []; |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $aAcceptCharset = []; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $aAcceptEncoding = []; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $aAcceptLanguage = []; |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $sReferer = ''; |
||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $sUserAgent = ''; |
||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $sIfModifiedSince = ''; |
||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $sIfNoneMatch = ''; |
||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $sContentType = ''; |
||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $bDoNotTrack = false; |
||
64 | /** |
||
65 | * @param HttpAuthenticationA $oHttpAuthentication |
||
66 | */ |
||
67 | abstract public function setAuthentication(HttpAuthenticationA $oHttpAuthentication); |
||
68 | |||
69 | /** |
||
70 | * @return string |
||
71 | */ |
||
72 | 1 | public function getServerName() { |
|
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 1 | public function getServerProtocol() { |
|
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | 1 | public function getHttpMethod() { |
|
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | 1 | public function getHttpAccept() { |
|
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | 1 | public function getHttpAcceptCharset() { |
|
99 | /** |
||
100 | * @return bool |
||
101 | */ |
||
102 | 2 | public function hasContentType() { |
|
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 1 | public function getContentType() { |
|
111 | /** |
||
112 | * @return array |
||
113 | */ |
||
114 | 1 | public function getHttpAcceptEncoding() { |
|
117 | /** |
||
118 | * @return array |
||
119 | */ |
||
120 | 1 | public function getHttpAcceptLanguage() { |
|
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | 1 | public function getIfModifiedSince() { |
|
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | 1 | public function getIfNoneMatch() { |
|
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | 1 | public function getHttpReferer() { |
|
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | 1 | public function getHttpUserAgent() { |
|
147 | /** |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function getDoNotTrack() { |
||
153 | /** |
||
154 | * @return bool |
||
155 | */ |
||
156 | 1 | public static function isSecure() { |
|
159 | |||
160 | /** |
||
161 | * @param array $aServer |
||
162 | */ |
||
163 | 18 | private function loadServerHeaders($aServer) { |
|
171 | |||
172 | /** |
||
173 | * @param array $aServer |
||
174 | */ |
||
175 | 18 | private function loadAcceptHeaders($aServer) { |
|
198 | |||
199 | 18 | private function loadCachingHeaders($aServer) { |
|
207 | |||
208 | 19 | private function loadAuthenticationHeaders($aServer) { |
|
217 | |||
218 | /** |
||
219 | * @param $aServer |
||
220 | */ |
||
221 | 19 | public function initServer($aServer) { |
|
249 | } |
||
250 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.