| Total Complexity | 65 |
| Total Lines | 229 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | final class Builder |
||
| 15 | { |
||
| 16 | private static $_root; |
||
| 17 | private static $_debug; |
||
| 18 | |||
| 19 | const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
||
| 20 | const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence"; |
||
| 21 | |||
| 22 | |||
| 23 | private static function build(object $node, &$parent = null) |
||
| 27 | } |
||
| 28 | |||
| 29 | private static function buildNodeList(NodeList $node, &$parent) |
||
| 30 | { |
||
| 31 | if ($node->type & (Y::RAW | Y::LITTERALS)) { |
||
| 32 | return self::buildLitteral($node, $node->type); |
||
| 33 | } |
||
| 34 | $p = $parent; |
||
| 35 | switch ($node->type) { |
||
| 36 | case Y::MAPPING: //fall through |
||
| 37 | case Y::SET: $p = new \StdClass; break; |
||
| 38 | case Y::SEQUENCE: $p = []; break; |
||
| 39 | // case Y::KEY: $p = $parent;break; |
||
| 40 | } |
||
| 41 | $out = null; |
||
| 42 | foreach ($node as $child) { |
||
| 43 | $result = self::build($child, $p); |
||
| 44 | if (!is_null($result)) { |
||
| 45 | if (is_string($result)) { |
||
| 46 | $out .= $result.' '; |
||
| 47 | } else { |
||
| 48 | return $result; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | } |
||
| 52 | return is_null($out) ? $p : rtrim($out); |
||
| 53 | } |
||
| 54 | |||
| 55 | private static function buildNode(Node $node, &$parent) |
||
| 56 | { |
||
| 57 | extract((array) $node, EXTR_REFS); |
||
| 58 | if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
||
| 59 | if (is_object($value)) { |
||
| 60 | $tmp = self::build($value, $parent) ?? $parent; |
||
| 61 | } else { |
||
| 62 | $tmp = $node->getPhpValue(); |
||
| 63 | } |
||
| 64 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
||
| 65 | return self::$_root->getReference($identifier); |
||
| 66 | } |
||
| 67 | $typesActions = [Y::COMMENT => 'buildComment', |
||
| 68 | Y::DIRECTIVE => 'buildDirective', |
||
| 69 | Y::ITEM => 'buildItem', |
||
| 70 | Y::KEY => 'buildKey', |
||
| 71 | Y::SET_KEY => 'buildSetKey', |
||
| 72 | Y::SET_VALUE => 'buildSetValue', |
||
| 73 | Y::TAG => 'buildTag', |
||
| 74 | ]; |
||
| 75 | if (isset($typesActions[$type])) { |
||
| 76 | return self::{$typesActions[$type]}($node, $parent); |
||
| 77 | } |
||
| 78 | return is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Builds a key and set the property + value to the given parent |
||
| 83 | * |
||
| 84 | * @param Node $node The node |
||
|
2 ignored issues
–
show
|
|||
| 85 | * @param object|array $parent The parent |
||
|
1 ignored issue
–
show
|
|||
| 86 | * |
||
| 87 | * @throws \ParseError if Key has no name(identifier) |
||
| 88 | * @return null |
||
| 89 | */ |
||
| 90 | private static function buildKey(Node $node, &$parent):void |
||
| 91 | { |
||
| 92 | extract((array) $node, EXTR_REFS); |
||
| 93 | if (is_null($identifier)) { |
||
| 94 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $line)); |
||
| 95 | } else { |
||
| 96 | if ($value instanceof Node && ($value->type & (Y::KEY|Y::ITEM))) { |
||
| 97 | $parent->{$identifier} = $value->type & Y::KEY ? new \StdClass : []; |
||
| 98 | self::build($value, $parent->{$identifier}); |
||
| 99 | } elseif (is_object($value)) { |
||
| 100 | $parent->{$identifier} = self::build($value, $parent->{$identifier}); |
||
| 101 | } else { |
||
| 102 | $parent->{$identifier} = $node->getPhpValue(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | private static function buildItem(Node $node, &$parent):void |
||
| 108 | { |
||
| 109 | if (!is_array($parent) && !($parent instanceof \ArrayIterator)) { |
||
| 110 | throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1); |
||
| 111 | } |
||
| 112 | if ($value instanceof Node && $value->type === Y::KEY) { |
||
| 113 | $parent[$node->value->identifier] = self::build($node->value->value, $parent[$node->value->identifier]); |
||
| 114 | } else { |
||
| 115 | $index = count($parent); |
||
| 116 | $parent[$index] = self::build($node->value, $parent[$index]); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Builds a file. check multiple documents & split if more than one documents |
||
| 122 | * |
||
| 123 | * @param Node $_root The root node |
||
|
3 ignored issues
–
show
|
|||
| 124 | * @param int $_debug the level of debugging requested |
||
|
3 ignored issues
–
show
|
|||
| 125 | * |
||
| 126 | * @return array|YamlObject list of documents or juste one. |
||
| 127 | */ |
||
| 128 | public static function buildContent(Node $_root, int $_debug) |
||
| 129 | { |
||
| 130 | self::$_debug = $_debug; |
||
| 131 | $totalDocStart = 0; |
||
| 132 | $documents = []; |
||
| 133 | if ($_root->value instanceof Node) { |
||
| 134 | $q = new NodeList; |
||
| 135 | $q->push($_root->value); |
||
| 136 | return self::buildDocument($q, 0); |
||
| 137 | } |
||
| 138 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
||
| 139 | foreach ($_root->value as $child) { |
||
| 140 | if ($child->type & Y::DOC_START) $totalDocStart++; |
||
| 141 | //if 0 or 1 DOC_START = we are still in first document |
||
| 142 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
||
| 143 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
||
| 144 | $documents[$currentDoc]->push($child); |
||
| 145 | } |
||
| 146 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
||
| 147 | return count($content) === 1 ? $content[0] : $content; |
||
| 148 | } |
||
| 149 | |||
| 150 | private static function buildDocument(NodeList $list, int $key):YamlObject |
||
| 151 | { |
||
| 152 | self::$_root = new YamlObject(); |
||
| 153 | $childTypes = $list->getTypes(); |
||
| 154 | $isaMapping = (bool) (Y::KEY | Y::MAPPING) & $childTypes; |
||
| 155 | $isaSequence = (bool) Y::ITEM & $childTypes; |
||
| 156 | $isaSet = (bool) Y::SET_VALUE & $childTypes; |
||
| 157 | if ($isaMapping && $isaSequence) { |
||
| 158 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
||
| 159 | } else { |
||
| 160 | switch (true) { |
||
| 161 | case $isaSequence: $list->type = Y::SEQUENCE;break; |
||
| 162 | case $isaSet: $list->type = Y::SET;break; |
||
| 163 | default: $list->type = Y::MAPPING; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | $string = ''; |
||
| 167 | foreach ($list as $child) { |
||
| 168 | $result = self::build($child, self::$_root); |
||
| 169 | if (is_string($result)) { |
||
| 170 | $string .= $result.' '; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | if (!empty($string)) { |
||
| 174 | self::$_root->setText(rtrim($string)); |
||
| 175 | } |
||
| 176 | return self::$_root; |
||
| 177 | } |
||
| 178 | |||
| 179 | private static function buildLitteral(NodeList $children, int $type):string |
||
| 180 | { |
||
| 181 | $lines = []; |
||
| 182 | $children->rewind(); |
||
| 183 | $refIndent = $children->current()->indent; |
||
| 184 | foreach ($children as $child) { |
||
| 185 | if ($child->value instanceof NodeList) { |
||
| 186 | $lines[] = self::buildLitteral($child->value, $type); |
||
| 187 | } else { |
||
| 188 | $prefix = ''; |
||
| 189 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
||
| 190 | $prefix = "\n"; |
||
| 191 | } |
||
| 192 | $lines[] = $prefix.$child->value; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | if ($type & Y::RAW) return implode('', $lines); |
||
| 196 | if ($type & Y::LITT) return implode("\n", $lines); |
||
| 197 | if ($type & Y::LITT_FOLDED) return implode(' ', $lines); |
||
| 198 | } |
||
| 199 | |||
| 200 | private function buildSetKey(Node $node, $parent):void |
||
| 201 | { |
||
| 202 | $key = json_encode(self::build($node->value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
| 203 | if (empty($key)) |
||
| 204 | throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
||
| 205 | $parent->{$key} = null; |
||
| 206 | } |
||
| 207 | |||
| 208 | private function buildSetValue(Node $node, $parent):void |
||
| 209 | { |
||
| 210 | $prop = array_keys(get_object_vars($parent)); |
||
| 211 | $key = end($prop); |
||
| 212 | if ($node->value->type & (Y::ITEM|Y::MAPPING)) { |
||
| 213 | $p = $node->value->type === Y::ITEM ? [] : new \StdClass; |
||
| 214 | self::build($node->value, $p); |
||
| 215 | } else { |
||
| 216 | $p = self::build($node->value, $parent->{$key}); |
||
| 217 | } |
||
| 218 | $parent->{$key} = $p; |
||
| 219 | } |
||
| 220 | |||
| 221 | private function buildTag(Node $node, $parent) |
||
| 222 | { |
||
| 223 | if ($parent === self::$_root) { |
||
| 224 | $parent->addTag($node->identifier); |
||
| 225 | return; |
||
| 226 | } |
||
| 227 | //TODO: have somewhere a list of common tags and their treatment |
||
| 228 | if (in_array($node->identifier, ['!binary', '!str'])) { |
||
| 229 | if ($node->value->value instanceof NodeList) $node->value->value->type = Y::RAW; |
||
| 230 | else $node->value->type = Y::RAW; |
||
| 231 | } |
||
| 232 | $val = is_null($node->value) ? null : self::build(/** @scrutinizer ignore-type */ $node->value, $node); |
||
|
2 ignored issues
–
show
|
|||
| 233 | return new Tag($node->identifier, $val); |
||
| 234 | } |
||
| 235 | |||
| 236 | private function buildComment(Node $node, $parent):void |
||
| 239 | } |
||
| 240 | |||
| 241 | private function buildDirective($node, $parent) |
||
| 242 | { |
||
| 243 | # TODO : implement |
||
| 244 | } |
||
| 245 | } |
||
| 246 |