| Total Complexity | 43 |
| Total Lines | 171 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Dumper 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 Dumper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Dumper //extends AnotherClass |
||
| 14 | { |
||
| 15 | private const INDENT = 2; |
||
| 16 | private const WIDTH = 120; |
||
| 17 | private const OPTIONS = 00000; |
||
| 18 | |||
| 19 | /** @var null|NodeList */ |
||
| 20 | private static $result; |
||
| 21 | private static $options; |
||
| 22 | //options |
||
| 23 | public const EXPAND_SHORT = 00001; |
||
| 24 | public const SERIALIZE_CUSTOM_OBJECTS = 00010; |
||
| 25 | |||
| 26 | // public function __construct(int $options = null) |
||
| 27 | // { |
||
| 28 | // if (is_int($options)) self::$options = $options; |
||
| 29 | // } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns (as a string) the YAML representation of the $dataType provided |
||
| 33 | * |
||
| 34 | * @param mixed $dataType The data type |
||
| 35 | * @param int|null $options The options |
||
| 36 | * |
||
| 37 | * @throws \Exception datatype cannot be null |
||
| 38 | * |
||
| 39 | * @return string The Yaml string content |
||
| 40 | */ |
||
| 41 | public static function toString($dataType, int $options = null):string |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Calls and saves the result of Dumper::toString to the file $filePath provided |
||
| 60 | * |
||
| 61 | * @param string $filePath The file path |
||
| 62 | * @param mixed $dataType The data type |
||
| 63 | * @param int|null $options The options |
||
| 64 | * |
||
| 65 | * @throws \Exception datatype cannot be null |
||
| 66 | * |
||
| 67 | * @return boolean true = if the file has been correctly saved (according to return from 'file_put_contents') |
||
| 68 | */ |
||
| 69 | public static function toFile(string $filePath, $dataType, int $options = null):bool |
||
| 70 | { |
||
| 71 | return !is_bool(file_put_contents($filePath, self::toString($dataType, $options))); |
||
| 72 | } |
||
| 73 | |||
| 74 | private static function dump($dataType, int $indent) |
||
| 75 | { |
||
| 76 | if (is_scalar($dataType)) { |
||
| 77 | switch (gettype($dataType)) { |
||
| 78 | case 'boolean': return $dataType ? 'true' : 'false'; |
||
| 79 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
||
| 80 | return sprintf('%.2F', $dataType); |
||
| 81 | case 'double': if (is_nan((float) $dataType)) return '.nan'; |
||
| 82 | default: |
||
| 83 | return $dataType; |
||
| 84 | } |
||
| 85 | } elseif (is_object($dataType)) { |
||
| 86 | self::dumpObject($dataType, $indent); |
||
| 87 | } elseif (is_array($dataType)) { |
||
| 88 | self::dumpSequence($dataType, $indent); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | private static function dumpYamlObject(YamlObject $dataType) |
||
| 93 | { |
||
| 94 | if ($dataType->hasDocStart()) self::$result->push("---"); |
||
| 95 | // self::dump($dataType, 0); |
||
| 96 | if (count($dataType) > 0) { |
||
| 97 | self::dumpSequence($dataType->getArrayCopy(), 0); |
||
| 98 | } else { |
||
| 99 | self::dumpObject($dataType, 0); |
||
| 100 | } |
||
| 101 | // self::insertComments($dataType->getComment()); |
||
| 102 | //TODO: $references = $dataType->getAllReferences(); |
||
| 103 | } |
||
| 104 | |||
| 105 | private static function add($value, $indent) |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | private static function dumpSequence(array $array, int $indent) |
||
| 114 | { |
||
| 115 | $refKeys = range(0, count($array)); |
||
| 116 | foreach ($array as $key => $item) { |
||
| 117 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
||
| 118 | if (is_scalar($item)) { |
||
| 119 | self::add($lineStart.self::dump($item,0), $indent); |
||
| 120 | } else { |
||
| 121 | self::add($lineStart, $indent); |
||
| 122 | self::dump($item, $indent + self::INDENT); |
||
| 123 | } |
||
| 124 | next($refKeys); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | private static function insertComments(array $commentsArray) |
||
| 129 | { |
||
| 130 | foreach ($commentsArray as $lineNb => $comment) { |
||
| 131 | self::$result->add($lineNb, $comment); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | private static function dumpObject(object $obj, int $indent) |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function dumpCompact($subject, int $indent) |
||
| 186 |