| Total Complexity | 40 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Parameter 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 Parameter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Parameter implements ParameterInterface |
||
| 24 | { |
||
| 25 | /* |
||
| 26 | * Request parameters |
||
| 27 | */ |
||
| 28 | // Specifies that the parameter will consist of an array of form values. |
||
| 29 | const FORM_VALUES = 'FormValues'; |
||
| 30 | // Specifies that the parameter will contain the value of an input control. |
||
| 31 | const INPUT_VALUE = 'InputValue'; |
||
| 32 | // Specifies that the parameter will consist of a bool value of a checkbox. |
||
| 33 | const CHECKED_VALUE = 'CheckedValue'; |
||
| 34 | // Specifies that the parameter value will be the innerHTML value of the element. |
||
| 35 | const ELEMENT_INNERHTML = 'ElementInnerHTML'; |
||
| 36 | // Specifies that the parameter will be a quoted value (string). |
||
| 37 | const QUOTED_VALUE = 'QuotedValue'; |
||
| 38 | // Specifies that the parameter will be a bool value (true or false). |
||
| 39 | const BOOL_VALUE = 'BoolValue'; |
||
| 40 | // Specifies that the parameter will be a numeric, non-quoted value. |
||
| 41 | const NUMERIC_VALUE = 'NumericValue'; |
||
| 42 | // Specifies that the parameter will be a non-quoted value |
||
| 43 | // (evaluated by the browsers javascript engine at run time). |
||
| 44 | const JS_VALUE = 'UnquotedValue'; |
||
| 45 | // Specifies that the parameter is a call to a javascript function |
||
| 46 | const JS_CALL = 'JsCall'; |
||
| 47 | // Specifies that the parameter must be json encoded |
||
| 48 | const JSON_VALUE = 'JsonValue'; |
||
| 49 | // Specifies that the parameter will be an integer used to generate pagination links. |
||
| 50 | const PAGE_NUMBER = 'PageNumber'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The parameter type |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $sType; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The parameter value |
||
| 61 | * |
||
| 62 | * @var mixed |
||
| 63 | */ |
||
| 64 | protected $xValue; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Convert the parameter value to integer |
||
| 68 | * |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $bToInt = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The constructor. |
||
| 75 | * |
||
| 76 | * @param string $sType The parameter type |
||
| 77 | * @param mixed $xValue The parameter value |
||
| 78 | */ |
||
| 79 | public function __construct(string $sType, $xValue) |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the parameter type |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getType(): string |
||
| 91 | { |
||
| 92 | return $this->sType; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the parameter value |
||
| 97 | * |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | public function getValue() |
||
| 101 | { |
||
| 102 | return $this->xValue; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set the parameter value |
||
| 107 | * |
||
| 108 | * @param mixed $xValue The parameter value |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function setValue($xValue) |
||
| 113 | { |
||
| 114 | $this->xValue = $xValue; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return ParameterInterface |
||
| 119 | */ |
||
| 120 | public function toInt(): ParameterInterface |
||
| 121 | { |
||
| 122 | $this->bToInt = true; |
||
| 123 | return $this; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Create a Parameter instance using the given value |
||
| 128 | * |
||
| 129 | * @param mixed $xValue The parameter value |
||
| 130 | * |
||
| 131 | * @return ParameterInterface |
||
| 132 | */ |
||
| 133 | public static function make($xValue): ParameterInterface |
||
| 134 | { |
||
| 135 | if($xValue instanceof ParameterInterface) |
||
| 136 | { |
||
| 137 | return $xValue; |
||
| 138 | } |
||
| 139 | if(is_numeric($xValue)) |
||
| 140 | { |
||
| 141 | return new Parameter(self::NUMERIC_VALUE, $xValue); |
||
| 142 | } |
||
| 143 | if(is_string($xValue)) |
||
| 144 | { |
||
| 145 | return new Parameter(self::QUOTED_VALUE, $xValue); |
||
| 146 | } |
||
| 147 | if(is_bool($xValue)) |
||
| 148 | { |
||
| 149 | return new Parameter(self::BOOL_VALUE, $xValue); |
||
| 150 | } |
||
| 151 | if($xValue instanceof Call) |
||
| 152 | { |
||
| 153 | return new Parameter(self::JS_CALL, $xValue); |
||
| 154 | } |
||
| 155 | // if(is_array($xValue) || is_object($xValue)) |
||
| 156 | { |
||
| 157 | return new Parameter(self::JSON_VALUE, $xValue); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Convert to a value to be inserted in an array for json conversion |
||
| 163 | * |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | public function jsonSerialize() |
||
| 167 | { |
||
| 168 | switch($this->getType()) |
||
| 169 | { |
||
| 170 | case self::JS_CALL: |
||
| 171 | return $this->getValue()->toArray(); |
||
| 172 | case self::JS_VALUE: |
||
| 173 | return [ |
||
| 174 | '_type' => 'expr', |
||
| 175 | 'calls' => [['_type' => 'attr', '_name' => $this->getValue()]], |
||
| 176 | ]; |
||
| 177 | case self::FORM_VALUES: |
||
| 178 | return ['_type' => 'form', '_name' => $this->getValue()]; |
||
| 179 | case self::INPUT_VALUE: |
||
| 180 | return ['_type' => 'input', '_name' => $this->getValue()]; |
||
| 181 | case self::CHECKED_VALUE: |
||
| 182 | return ['_type' => 'checked', '_name' => $this->getValue()]; |
||
| 183 | case self::ELEMENT_INNERHTML: |
||
| 184 | return ['_type' => 'html', '_name' => $this->getValue()]; |
||
| 185 | case self::PAGE_NUMBER: |
||
| 186 | return ['_type' => 'page', '_name' => '']; |
||
| 187 | case self::QUOTED_VALUE: |
||
| 188 | case self::BOOL_VALUE: |
||
| 189 | case self::NUMERIC_VALUE: |
||
| 190 | case self::JSON_VALUE: |
||
| 191 | default: |
||
| 192 | // Return the value as is. |
||
| 193 | return $this->getValue(); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Add quotes to a given value |
||
| 199 | * |
||
| 200 | * @param string $sValue The value to be quoted |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function getQuotedValue(string $sValue): string |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get a js call to Jaxon with a single parameter |
||
| 211 | * |
||
| 212 | * @param string $sFunction The function name |
||
| 213 | * @param string $sParameter The function parameter |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | private function getJaxonCall(string $sFunction, string $sParameter): string |
||
| 218 | { |
||
| 219 | return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')'; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the script for an array of form values. |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | protected function getFormValuesScript(): string |
||
| 228 | { |
||
| 229 | return $this->getJaxonCall('getFormValues', $this->xValue); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the script for an input control. |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | protected function getInputValueScript(): string |
||
| 238 | { |
||
| 239 | return $this->getJaxonCall('$', $this->xValue) . '.value'; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the script for a bool value of a checkbox. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | protected function getCheckedValueScript(): string |
||
| 248 | { |
||
| 249 | return $this->getJaxonCall('$', $this->xValue) . '.checked'; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the script for the innerHTML value of the element. |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function getElementInnerHTMLScript(): string |
||
| 258 | { |
||
| 259 | return $this->getJaxonCall('$', $this->xValue) . '.innerHTML'; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the script for a quoted value (string). |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function getQuotedValueScript(): string |
||
| 268 | { |
||
| 269 | return $this->getQuotedValue(addslashes($this->xValue)); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the script for a bool value (true or false). |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | protected function getBoolValueScript(): string |
||
| 278 | { |
||
| 279 | return ($this->xValue) ? 'true' : 'false'; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the script for a numeric, non-quoted value. |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | protected function getNumericValueScript(): string |
||
| 288 | { |
||
| 289 | return (string)$this->xValue; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time). |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | protected function getUnquotedValueScript(): string |
||
| 298 | { |
||
| 299 | return (string)$this->xValue; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time). |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | protected function getJsonValueScript(): string |
||
| 308 | { |
||
| 309 | // Unable to use double quotes here because they cannot be handled on client side. |
||
| 310 | // So we are using simple quotes even if the Json standard recommends double quotes. |
||
| 311 | return str_replace('"', "'", json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT)); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the script for an integer used to generate pagination links. |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | protected function getPageNumberScript(): string |
||
| 320 | { |
||
| 321 | return (string)$this->xValue; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Generate the javascript code. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getScript(): string |
||
| 330 | { |
||
| 331 | $sMethodName = 'get' . $this->sType . 'Script'; |
||
| 332 | if(!method_exists($this, $sMethodName)) |
||
| 333 | { |
||
| 334 | return ''; |
||
| 335 | } |
||
| 336 | $sScript = $this->$sMethodName(); |
||
| 337 | return $this->bToInt ? "parseInt($sScript)" : $sScript; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Convert this call to string |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function __toString() |
||
| 348 | } |
||
| 349 | } |
||
| 350 |