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 | public function getServerName() { |
||
| 73 | return $this->sServerName; |
||
| 74 | } |
||
| 75 | /** |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getServerProtocol() { |
||
| 79 | return $this->sServerProtocol; |
||
| 80 | } |
||
| 81 | /** |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getHttpMethod() { |
||
| 85 | return $this->sHttpMethod; |
||
| 86 | } |
||
| 87 | /** |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function getHttpAccept() { |
||
| 91 | return $this->aAccept; |
||
| 92 | } |
||
| 93 | /** |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public function getHttpAcceptCharset() { |
||
| 97 | return $this->aAcceptCharset; |
||
| 98 | } |
||
| 99 | /** |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | public function hasContentType() { |
||
| 103 | return !empty($this->sContentType); |
||
| 104 | } |
||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getContentType() { |
||
| 109 | return $this->sContentType; |
||
| 110 | } |
||
| 111 | /** |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getHttpAcceptEncoding() { |
||
| 115 | return $this->aAcceptEncoding; |
||
| 116 | } |
||
| 117 | /** |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | public function getHttpAcceptLanguage() { |
||
| 121 | return $this->aAcceptLanguage; |
||
| 122 | } |
||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getIfModifiedSince() { |
||
| 127 | return $this->sIfModifiedSince; |
||
| 128 | } |
||
| 129 | /** |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getIfNoneMatch() { |
||
| 133 | return $this->sIfNoneMatch; |
||
| 134 | } |
||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getHttpReferer() { |
||
| 139 | return $this->sReferer; |
||
| 140 | } |
||
| 141 | /** |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getHttpUserAgent() { |
||
| 145 | return $this->sUserAgent; |
||
| 146 | } |
||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function getDoNotTrack() { |
||
| 153 | /** |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public static function isSecure() { |
||
| 157 | return (array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] == 'on'); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param array $aServer |
||
| 162 | */ |
||
| 163 | 17 | private function loadServerHeaders($aServer) { |
|
| 164 | 17 | if (isset ($aServer['SERVER_PROTOCOL'])) { |
|
| 165 | $this->sServerProtocol = $aServer['SERVER_PROTOCOL']; |
||
| 166 | } |
||
| 167 | 17 | if (isset ($aServer['SERVER_NAME'])) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param array $aServer |
||
| 174 | */ |
||
| 175 | 17 | private function loadAcceptHeaders($aServer) { |
|
| 198 | |||
| 199 | 17 | private function loadCachingHeaders($aServer) { |
|
| 207 | |||
| 208 | 17 | private function loadAuthenticationHeaders($aServer) { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param $aServer |
||
| 220 | */ |
||
| 221 | 17 | public function initServer($aServer) { |
|
| 249 | } |
||
| 250 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.