| Total Complexity | 43 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ParameterReader 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.
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 ParameterReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class ParameterReader |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * @var Container |
||
| 56 | */ |
||
| 57 | protected $di; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ConfigManager |
||
| 61 | */ |
||
| 62 | protected $xConfigManager; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Translator |
||
| 66 | */ |
||
| 67 | protected $xTranslator; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var UriDetector |
||
| 71 | */ |
||
| 72 | private $xUriDetector; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The function which decodes input parameters. |
||
| 76 | * |
||
| 77 | * @var callable |
||
| 78 | */ |
||
| 79 | private $cParamDecoder; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The function which decodes utf8 string. |
||
| 83 | * |
||
| 84 | * @var callable |
||
| 85 | */ |
||
| 86 | private $cUtf8Decoder; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The constructor |
||
| 90 | * |
||
| 91 | * @param Container $di |
||
| 92 | * @param ConfigManager $xConfigManager |
||
| 93 | * @param Translator $xTranslator |
||
| 94 | * @param UriDetector $xUriDetector |
||
| 95 | */ |
||
| 96 | public function __construct(Container $di, ConfigManager $xConfigManager, |
||
| 97 | Translator $xTranslator, UriDetector $xUriDetector) |
||
| 98 | { |
||
| 99 | $this->di = $di; |
||
| 100 | $this->xConfigManager = $xConfigManager; |
||
| 101 | $this->xTranslator = $xTranslator; |
||
| 102 | $this->xUriDetector = $xUriDetector; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Choose the function to use to decode input data. |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | private function setParamDecoder() |
||
| 111 | { |
||
| 112 | // Parameters are url encoded when uploading files |
||
| 113 | $aServerParams = $this->di->getRequest()->getServerParams(); |
||
| 114 | $sContentType = ''; |
||
| 115 | if(isset($aServerParams['CONTENT_TYPE'])) |
||
| 116 | { |
||
| 117 | $sContentType = $aServerParams['CONTENT_TYPE']; |
||
| 118 | } |
||
| 119 | elseif(isset($aServerParams['HTTP_CONTENT_TYPE'])) |
||
| 120 | { |
||
| 121 | $sContentType = $aServerParams['HTTP_CONTENT_TYPE']; |
||
| 122 | } |
||
| 123 | $sType = 'multipart/form-data'; |
||
| 124 | if(strncmp($sContentType, $sType, strlen($sType)) !== 0) |
||
| 125 | { |
||
| 126 | $this->cParamDecoder = function($sParam) { return $sParam; }; |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | $this->cParamDecoder = function($sParam) { return urldecode($sParam); }; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Choose the function to use to decode input data. |
||
| 134 | * |
||
| 135 | * @return void |
||
| 136 | * @throws RequestException |
||
| 137 | */ |
||
| 138 | private function setUtf8Decoder() |
||
| 139 | { |
||
| 140 | // By default, no decoding |
||
| 141 | $this->cUtf8Decoder = function($sStr) { |
||
| 142 | return $sStr; |
||
| 143 | }; |
||
| 144 | $sEncoding = $this->xConfigManager->getOption('core.encoding', ''); |
||
| 145 | if(function_exists('iconv')) |
||
| 146 | { |
||
| 147 | $this->cUtf8Decoder = function($sStr) use($sEncoding) { |
||
| 148 | return iconv("UTF-8", $sEncoding . '//TRANSLIT', $sStr); |
||
| 149 | }; |
||
| 150 | } |
||
| 151 | elseif(function_exists('mb_convert_encoding')) |
||
| 152 | { |
||
| 153 | $this->cUtf8Decoder = function($sStr) use($sEncoding) { |
||
| 154 | return mb_convert_encoding($sStr, $sEncoding, "UTF-8"); |
||
| 155 | }; |
||
| 156 | } |
||
| 157 | elseif($sEncoding === "ISO-8859-1") |
||
| 158 | { |
||
| 159 | $this->cUtf8Decoder = function($sStr) { |
||
| 160 | return utf8_decode($sStr); |
||
| 161 | }; |
||
| 162 | } |
||
| 163 | else |
||
| 164 | { |
||
| 165 | throw new RequestException($this->xTranslator->trans('errors.request.conversion')); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Converts a string to a bool var |
||
| 171 | * |
||
| 172 | * @param string $sValue The string to be converted |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | private function convertStringToBool(string $sValue): bool |
||
| 177 | { |
||
| 178 | if(strcasecmp($sValue, 'true') === 0) |
||
| 179 | { |
||
| 180 | return true; |
||
| 181 | } |
||
| 182 | if(strcasecmp($sValue, 'false') === 0) |
||
| 183 | { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | return (intval($sValue) !== 0); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Convert a Jaxon request argument to its value |
||
| 191 | * |
||
| 192 | * Depending on its first char, the Jaxon request argument is converted to a given type. |
||
| 193 | * |
||
| 194 | * @param string $sValue The keys of the options in the file |
||
| 195 | * |
||
| 196 | * @return string|bool|integer|double|null |
||
| 197 | */ |
||
| 198 | private function convertValue(string $sValue) |
||
| 199 | { |
||
| 200 | $cType = substr($sValue, 0, 1); |
||
| 201 | $sValue = substr($sValue, 1); |
||
| 202 | switch($cType) |
||
| 203 | { |
||
| 204 | case 'S': |
||
| 205 | $value = !$sValue ? '' : $sValue; |
||
| 206 | break; |
||
| 207 | case 'B': |
||
| 208 | $value = $this->convertStringToBool($sValue); |
||
| 209 | break; |
||
| 210 | case 'N': |
||
| 211 | $value = ($sValue == floor($sValue) ? (int)$sValue : (float)$sValue); |
||
| 212 | break; |
||
| 213 | case '*': |
||
| 214 | default: |
||
| 215 | $value = null; |
||
| 216 | break; |
||
| 217 | } |
||
| 218 | return $value; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Decode and convert a Jaxon request argument |
||
| 223 | * |
||
| 224 | * @param string $sParam The Jaxon request argument |
||
| 225 | * |
||
| 226 | * @return mixed |
||
| 227 | */ |
||
| 228 | private function decodeRequestParameter(string $sParam) |
||
| 229 | { |
||
| 230 | if($sParam === '') |
||
| 231 | { |
||
| 232 | return $sParam; |
||
| 233 | } |
||
| 234 | |||
| 235 | $sParam = call_user_func($this->cParamDecoder, $sParam); |
||
| 236 | |||
| 237 | $xJson = json_decode($sParam, true); |
||
| 238 | if($xJson !== null && $sParam != $xJson) |
||
| 239 | { |
||
| 240 | return $xJson; |
||
| 241 | } |
||
| 242 | return $this->convertValue($sParam); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | private function getRequestParameters(): array |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Decode a Jaxon request argument from UTF8 |
||
| 275 | * |
||
| 276 | * @param mixed $xValue The value of the argument being decoded |
||
| 277 | * |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | private function decodeUtf8Parameter($xValue) |
||
| 281 | { |
||
| 282 | if(is_string($xValue)) |
||
| 283 | { |
||
| 284 | return call_user_func($this->cUtf8Decoder, $xValue); |
||
| 285 | } |
||
| 286 | // elseif(is_numeric($xValue) || is_bool($xValue)) |
||
| 287 | { |
||
| 288 | return $xValue; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Decode an array of Jaxon request arguments from UTF8 |
||
| 294 | * |
||
| 295 | * @param array $aParams |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | private function decodeUtf8Parameters(array $aParams): array |
||
| 300 | { |
||
| 301 | $aValues = []; |
||
| 302 | foreach($aParams as $sKey => $xValue) |
||
| 303 | { |
||
| 304 | // Decode the key |
||
| 305 | $sKey = call_user_func($this->cUtf8Decoder, $sKey); |
||
| 306 | // Decode the value |
||
| 307 | $aValues[$sKey] = is_array($xValue) ? |
||
| 308 | $this->decodeUtf8Parameters($xValue) : $this->decodeUtf8Parameter($xValue); |
||
| 309 | } |
||
| 310 | return $aValues; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Return the array of arguments from the GET or POST data |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | * @throws RequestException |
||
| 318 | */ |
||
| 319 | public function args(): array |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get the URI of the current request |
||
| 334 | * |
||
| 335 | * @throws UriException |
||
| 336 | */ |
||
| 337 | public function uri(): string |
||
| 338 | { |
||
| 339 | return $this->xUriDetector->detect($this->di->getRequest()->getServerParams()); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $sQueryPart |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | private function parseQueryPart(string $sQueryPart): string |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Make the specified URL suitable for redirect |
||
| 372 | * |
||
| 373 | * @param string $sURL The relative or fully qualified URL |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function parseUrl(string $sURL): string |
||
| 395 | } |
||
| 396 | } |
||
| 397 |