| Total Complexity | 61 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like VariableInfo 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 VariableInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class VariableInfo implements Interface_Optionable |
||
| 8 | { |
||
| 9 | use Traits_Optionable; |
||
| 10 | |||
| 11 | const TYPE_DOUBLE = 'double'; |
||
| 12 | const TYPE_INTEGER = 'integer'; |
||
| 13 | const TYPE_BOOLEAN = 'boolean'; |
||
| 14 | const TYPE_STRING = 'string'; |
||
| 15 | const TYPE_ARRAY = 'array'; |
||
| 16 | const TYPE_OBJECT = 'object'; |
||
| 17 | const TYPE_RESOURCE = 'resource'; |
||
| 18 | const TYPE_NULL = 'null'; |
||
| 19 | const TYPE_UNKNOWN = 'unknown type'; |
||
| 20 | const TYPE_CALLABLE = 'callable'; |
||
| 21 | |||
| 22 | protected static $colors = array( |
||
| 23 | self::TYPE_DOUBLE => 'ce0237', |
||
| 24 | self::TYPE_INTEGER => 'ce0237', |
||
| 25 | self::TYPE_ARRAY => '027ace', |
||
| 26 | self::TYPE_OBJECT => 'cf5e20', |
||
| 27 | self::TYPE_RESOURCE => '1c2eb1', |
||
| 28 | self::TYPE_STRING => '1fa507', |
||
| 29 | self::TYPE_BOOLEAN => '1c2eb1', |
||
| 30 | self::TYPE_NULL => '1c2eb1', |
||
| 31 | self::TYPE_UNKNOWN => 'cc0000', |
||
| 32 | self::TYPE_CALLABLE => 'cf5e20' |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $string; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var mixed |
||
| 42 | */ |
||
| 43 | protected $value; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $type; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param mixed $value |
||
| 52 | * @param array|null $serialized |
||
| 53 | */ |
||
| 54 | public function __construct($value, $serialized=null) |
||
| 55 | { |
||
| 56 | if(is_array($serialized)) |
||
| 57 | { |
||
| 58 | $this->parseSerialized($serialized); |
||
| 59 | } |
||
| 60 | else |
||
| 61 | { |
||
| 62 | $this->parseValue($value); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a new variable info instance from a PHP variable |
||
| 68 | * of any type. |
||
| 69 | * |
||
| 70 | * @param mixed $variable |
||
| 71 | * @return VariableInfo |
||
| 72 | */ |
||
| 73 | public static function fromVariable($variable) : VariableInfo |
||
| 74 | { |
||
| 75 | return new VariableInfo($variable); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Restores a variable info instance using a previously serialized |
||
| 80 | * array using the serialize() method. |
||
| 81 | * |
||
| 82 | * @param array $serialized |
||
| 83 | * @return VariableInfo |
||
| 84 | * @see VariableInfo::serialize() |
||
| 85 | */ |
||
| 86 | public static function fromSerialized(array $serialized) : VariableInfo |
||
| 87 | { |
||
| 88 | return new VariableInfo(null, $serialized); |
||
| 89 | } |
||
| 90 | |||
| 91 | protected function parseSerialized(array $serialized) |
||
| 92 | { |
||
| 93 | $this->string = $serialized['string']; |
||
| 94 | $this->type = $serialized['type']; |
||
| 95 | |||
| 96 | $this->setOptions($serialized['options']); |
||
| 97 | } |
||
| 98 | |||
| 99 | protected function parseValue($value) |
||
| 100 | { |
||
| 101 | $this->value = $value; |
||
| 102 | $this->type = strtolower(gettype($value)); |
||
| 103 | |||
| 104 | if(is_array($value) && is_callable($value)) { |
||
| 105 | $this->type = self::TYPE_CALLABLE; |
||
| 106 | } |
||
| 107 | |||
| 108 | $this->string = $this->_toString(); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The variable type - this is the same string that |
||
| 113 | * is returned by the PHP function `gettype`. |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getType() : string |
||
| 118 | { |
||
| 119 | return $this->type; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getDefaultOptions() : array |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Whether to prepend the variable type before the value, |
||
| 132 | * like the var_dump function. Example: <code>string "Some text"</code>. |
||
| 133 | * |
||
| 134 | * @param bool $enable |
||
| 135 | * @return VariableInfo |
||
| 136 | */ |
||
| 137 | public function enableType(bool $enable=true) : VariableInfo |
||
| 138 | { |
||
| 139 | return $this->setOption('prepend-type', $enable); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function toString() : string |
||
| 143 | { |
||
| 144 | $converted = $this->string; |
||
| 145 | |||
| 146 | if($this->getOption('prepend-type') === true && !$this->isNull()) |
||
| 147 | { |
||
| 148 | if($this->isString()) |
||
| 149 | { |
||
| 150 | $converted = '"'.$converted.'"'; |
||
| 151 | } |
||
| 152 | |||
| 153 | $converted = $this->type.' '.$converted; |
||
| 154 | } |
||
| 155 | |||
| 156 | return $converted; |
||
| 157 | } |
||
| 158 | |||
| 159 | protected function _toString() : string |
||
| 164 | } |
||
| 165 | |||
| 166 | protected function _toHTML() : string |
||
| 167 | { |
||
| 168 | $type = str_replace(' ', '_', $this->type); |
||
| 169 | $varMethod = 'toHTML_'.$type; |
||
| 170 | return $this->$varMethod(); |
||
| 171 | } |
||
| 172 | |||
| 173 | protected function toFormat(string $format) |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Converts an array to a string. |
||
| 208 | * @return string |
||
| 209 | * |
||
| 210 | * @todo Create custom dump implementation, using VariableInfo instances. |
||
| 211 | */ |
||
| 212 | protected function toString_array() : string |
||
| 213 | { |
||
| 214 | $result = json_encode($this->value, JSON_PRETTY_PRINT); |
||
| 215 | |||
| 216 | // the array may not be encodable - for example if it contains |
||
| 217 | // broken unicode characters. |
||
| 218 | if(is_string($result) && $result !== '') { |
||
| 219 | return $result; |
||
| 220 | } |
||
| 221 | |||
| 222 | return print_r($this->value, true); |
||
| 223 | } |
||
| 224 | |||
| 225 | protected function toString_callable() : string |
||
| 226 | { |
||
| 227 | $string = ''; |
||
| 228 | |||
| 229 | if(is_string($this->value[0])) { |
||
| 230 | $string .= $this->value[0].'::'; |
||
| 231 | } else { |
||
| 232 | $string .= get_class($this->value[0]).'->'; |
||
| 233 | } |
||
| 234 | |||
| 235 | $string .= $this->value[1].'()'; |
||
| 236 | |||
| 237 | return $string; |
||
| 238 | } |
||
| 239 | |||
| 240 | protected function toString_boolean() : string |
||
| 241 | { |
||
| 242 | return ConvertHelper::bool2string($this->value); |
||
| 243 | } |
||
| 244 | |||
| 245 | protected function toString_double() : string |
||
| 246 | { |
||
| 247 | return (string)$this->value; |
||
| 248 | } |
||
| 249 | |||
| 250 | protected function toString_integer() : string |
||
| 251 | { |
||
| 252 | return (string)$this->value; |
||
| 253 | } |
||
| 254 | |||
| 255 | protected function toString_null() : string |
||
| 256 | { |
||
| 257 | return 'null'; |
||
| 258 | } |
||
| 259 | |||
| 260 | protected function toString_object() : string |
||
| 261 | { |
||
| 262 | return get_class($this->value); |
||
| 263 | } |
||
| 264 | |||
| 265 | protected function toString_resource() : string |
||
| 266 | { |
||
| 267 | $string = (string)$this->value; |
||
| 268 | $string = substr($string, strpos($string, '#')); |
||
| 269 | |||
| 270 | return $string; |
||
| 271 | } |
||
| 272 | |||
| 273 | protected function toString_string() : string |
||
| 274 | { |
||
| 275 | return $this->value; |
||
| 276 | } |
||
| 277 | |||
| 278 | protected function toString_unknown_type() : string |
||
| 279 | { |
||
| 280 | return 'unknown type'; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function __toString() |
||
| 284 | { |
||
| 285 | return $this->toString(); |
||
| 286 | } |
||
| 287 | |||
| 288 | public function isInteger() : bool |
||
| 289 | { |
||
| 290 | return $this->isType(self::TYPE_INTEGER); |
||
| 291 | } |
||
| 292 | |||
| 293 | public function isString() : bool |
||
| 294 | { |
||
| 295 | return $this->isType(self::TYPE_STRING); |
||
| 296 | } |
||
| 297 | |||
| 298 | public function isBoolean() : bool |
||
| 299 | { |
||
| 300 | return $this->isType(self::TYPE_BOOLEAN); |
||
| 301 | } |
||
| 302 | |||
| 303 | public function isDouble() : bool |
||
| 304 | { |
||
| 305 | return $this->isType(self::TYPE_DOUBLE); |
||
| 306 | } |
||
| 307 | |||
| 308 | public function isArray() : bool |
||
| 309 | { |
||
| 310 | return $this->isType(self::TYPE_ARRAY); |
||
| 311 | } |
||
| 312 | |||
| 313 | public function isNull() : bool |
||
| 314 | { |
||
| 315 | return $this->isType(self::TYPE_NULL); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function isResource() : bool |
||
| 319 | { |
||
| 320 | return $this->isType(self::TYPE_RESOURCE); |
||
| 321 | } |
||
| 322 | |||
| 323 | public function isObject() : bool |
||
| 324 | { |
||
| 325 | return $this->isType(self::TYPE_OBJECT); |
||
| 326 | } |
||
| 327 | |||
| 328 | public function isType(string $type) : bool |
||
| 329 | { |
||
| 330 | return $this->type === $type; |
||
| 331 | } |
||
| 332 | |||
| 333 | protected function cutString($string) |
||
| 334 | { |
||
| 335 | $cutAt = $this->getOption('cut-length'); |
||
| 336 | |||
| 337 | return ConvertHelper::text_cut($string, $cutAt, ' [...]'); |
||
|
|
|||
| 338 | } |
||
| 339 | |||
| 340 | public function getTypeColor() : string |
||
| 341 | { |
||
| 342 | return self::$colors[$this->type]; |
||
| 343 | } |
||
| 344 | |||
| 345 | public function toHTML() : string |
||
| 346 | { |
||
| 347 | $converted = sprintf( |
||
| 348 | '<span style="color:#%1$s" class="variable-value-%3$s">'. |
||
| 349 | '%2$s'. |
||
| 350 | '</span>', |
||
| 351 | $this->getTypeColor(), |
||
| 352 | $this->_toHTML(), |
||
| 353 | str_replace(' ', '-', $this->type) |
||
| 354 | ); |
||
| 355 | |||
| 356 | if($this->getOption('prepend-type') === true && !$this->isNull()) |
||
| 357 | { |
||
| 358 | $typeLabel = '<span style="color:#1c2eb1" class="variable-type">'.$this->type.'</span> '; |
||
| 359 | $converted = $typeLabel.' '.$converted; |
||
| 360 | } |
||
| 361 | |||
| 362 | return $converted; |
||
| 363 | } |
||
| 364 | |||
| 365 | protected function toHTML_integer() : string |
||
| 366 | { |
||
| 367 | return $this->toString(); |
||
| 368 | } |
||
| 369 | |||
| 370 | protected function toHTML_array() : string |
||
| 371 | { |
||
| 372 | $json = $this->toString(); |
||
| 373 | $json = $this->cutString($json); |
||
| 374 | $json = nl2br($json); |
||
| 375 | |||
| 376 | return $json; |
||
| 377 | } |
||
| 378 | |||
| 379 | protected function toHTML_callable() : string |
||
| 380 | { |
||
| 381 | return $this->toString(); |
||
| 382 | } |
||
| 383 | |||
| 384 | protected function toHTML_object() : string |
||
| 385 | { |
||
| 386 | return $this->toString(); |
||
| 387 | } |
||
| 388 | |||
| 389 | protected function toHTML_resource() : string |
||
| 390 | { |
||
| 391 | return $this->toString(); |
||
| 392 | } |
||
| 393 | |||
| 394 | protected function toHTML_string() : string |
||
| 395 | { |
||
| 396 | $string = $this->toString(); |
||
| 397 | $string = $this->cutString($string); |
||
| 398 | $string = nl2br(htmlspecialchars($string)); |
||
| 399 | |||
| 400 | return '"'.$string.'"'; |
||
| 401 | } |
||
| 402 | |||
| 403 | protected function toHTML_boolean() : string |
||
| 406 | } |
||
| 407 | |||
| 408 | protected function toHTML_null() : string |
||
| 411 | } |
||
| 412 | |||
| 413 | protected function toHTML_unknown_type() : string |
||
| 414 | { |
||
| 415 | return $this->toString(); |
||
| 416 | } |
||
| 417 | |||
| 418 | public function serialize() |
||
| 424 | ); |
||
| 425 | } |
||
| 426 | } |