| Total Complexity | 42 |
| Total Lines | 167 |
| 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 | private static $result; |
||
| 20 | private static $options; |
||
| 21 | //options |
||
| 22 | public const EXPAND_SHORT = 00001; |
||
| 23 | public const SERIALIZE_CUSTOM_OBJECTS = 00010; |
||
| 24 | |||
| 25 | // public function __construct(int $options = null) |
||
| 26 | // { |
||
| 27 | // if (is_int($options)) self::$options = $options; |
||
| 28 | // } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Returns the YAML representation as a string of the $dataType provided |
||
| 32 | * |
||
| 33 | * @param mixed $dataType The data type |
||
| 34 | * @param int|null $options The options |
||
| 35 | * |
||
| 36 | * @throws \Exception datatype cannot be null |
||
| 37 | * |
||
| 38 | * @return string The Yaml string content |
||
| 39 | */ |
||
| 40 | public static function toString($dataType, int $options = null):string |
||
| 41 | { |
||
| 42 | if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
||
| 43 | self::$options = is_int($options) ? $options : self::OPTIONS; |
||
| 44 | self::$result = new DLL; |
||
| 45 | if ($dataType instanceof YamlObject) { |
||
| 46 | self::dumpYamlObject($dataType); |
||
| 47 | } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) { |
||
| 48 | array_map([self::class, 'dumpYamlObject'], $dataType); |
||
| 49 | } else { |
||
| 50 | self::dump($dataType, 0); |
||
| 51 | } |
||
| 52 | $out = implode("\n", iterator_to_array(self::$result)); |
||
| 53 | self::$result = null; |
||
| 54 | return $out; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Calls and saves the result of Dumper::toString to the file $filePath provided |
||
| 59 | * |
||
| 60 | * @param string $filePath The file path |
||
| 61 | * @param mixed $dataType The data type |
||
| 62 | * @param int|null $options The options |
||
| 63 | * |
||
| 64 | * @return boolean true = if the file has been correctly saved (according to return from 'file_put_contents') |
||
| 65 | */ |
||
| 66 | public static function toFile(string $filePath, $dataType, int $options = null):bool |
||
| 67 | { |
||
| 68 | return !is_bool(file_put_contents($filePath, self::toString($dataType, $options))); |
||
| 69 | } |
||
| 70 | |||
| 71 | private static function dump($dataType, int $indent) |
||
| 72 | { |
||
| 73 | if (is_scalar($dataType)) { |
||
| 74 | switch (gettype($dataType)) { |
||
| 75 | case 'boolean': return $dataType ? 'true' : 'false'; |
||
| 76 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
||
| 77 | return sprintf('%.2F', $dataType); |
||
| 78 | case 'double': if (is_nan((float) $dataType)) return '.nan'; |
||
| 79 | default: |
||
| 80 | return $dataType; |
||
| 81 | } |
||
| 82 | } elseif (is_object($dataType)) { |
||
| 83 | self::dumpObject($dataType, $indent); |
||
| 84 | } elseif (is_array($dataType)) { |
||
| 85 | self::dumpSequence($dataType, $indent); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | private static function dumpYamlObject(YamlObject $dataType) |
||
| 90 | { |
||
| 91 | self::$result->push("---"); |
||
| 92 | // self::dump($dataType, 0); |
||
| 93 | if (count($dataType) > 0) { |
||
| 94 | self::dumpSequence($dataType->getArrayCopy(), 0); |
||
| 95 | } else { |
||
| 96 | self::dumpObject($dataType, 0); |
||
| 97 | } |
||
| 98 | // self::insertComments($dataType->getComment()); |
||
| 99 | //TODO: $references = $dataType->getAllReferences(); |
||
| 100 | } |
||
| 101 | |||
| 102 | private static function add($value, $indent) |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | private static function dumpSequence(array $array, int $indent) |
||
| 111 | { |
||
| 112 | $refKeys = range(0, count($array)); |
||
| 113 | foreach ($array as $key => $item) { |
||
| 114 | $lineStart = current($refKeys) === $key ? "- " : "- $key: "; |
||
| 115 | if (is_scalar($item)) { |
||
| 116 | self::add($lineStart.$item, $indent); |
||
| 117 | } else { |
||
| 118 | self::add($lineStart, $indent); |
||
| 119 | self::dump($item, $indent + self::INDENT); |
||
| 120 | } |
||
| 121 | next($refKeys); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | private static function insertComments(array $commentsArray) |
||
| 126 | { |
||
| 127 | foreach ($commentsArray as $lineNb => $comment) { |
||
| 128 | self::$result->add($lineNb, $comment); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | private static function dumpObject(object $obj, int $indent) |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | public static function dumpCompact($subject, int $indent) |
||
| 182 |