| Total Complexity | 88 |
| Total Lines | 424 |
| 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 | * Builds a file. check multiple documents & split if more than one documents |
||
| 24 | * |
||
| 25 | * @param Node $_root The root node : Node with Node->type === YAML::ROOT |
||
| 26 | * @param int $_debug the level of debugging requested |
||
| 27 | * |
||
| 28 | * @return array|YamlObject list of documents or just one. |
||
| 29 | * @todo implement splitting on YAML::DOC_END also |
||
| 30 | */ |
||
| 31 | public static function buildContent(Node $_root, int $_debug) |
||
| 32 | { |
||
| 33 | self::$_debug = $_debug; |
||
| 34 | $totalDocStart = 0; |
||
| 35 | $documents = []; |
||
| 36 | if ($_root->value instanceof Node) { |
||
| 37 | $q = new NodeList; |
||
| 38 | $q->push($_root->value); |
||
| 39 | // return self::buildNodeList($q, new YamlObject); |
||
| 40 | self::$_root = new YamlObject; |
||
| 41 | $tmp = self::buildNodeList($q, self::$_root); |
||
| 42 | // var_dump('alone', $tmp); |
||
| 43 | return $tmp; |
||
| 44 | } |
||
| 45 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
||
| 46 | foreach ($_root->value as $child) { |
||
| 47 | if ($child->type & Y::DOC_START) $totalDocStart++; |
||
| 48 | //if 0 or 1 DOC_START = we are still in first document |
||
| 49 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
||
| 50 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
||
| 51 | $documents[$currentDoc]->push($child); |
||
| 52 | } |
||
| 53 | // $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
||
| 54 | $content = []; |
||
| 55 | foreach ($documents as $num => $list) { |
||
| 56 | try { |
||
| 57 | self::$_root = new YamlObject; |
||
| 58 | // $tmp = var_dump('insideforeach'.$tmp); |
||
| 59 | $content[] = self::buildNodeList($list, self::$_root); |
||
| 60 | } catch (Exception $e) { |
||
| 61 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $num)); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | // $content = array_map([self::class, 'buildNodeList'], $documents, array_keys($documents)); |
||
| 65 | return count($content) === 1 ? $content[0] : $content; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Builds a document. Basically a NodeList of children |
||
| 70 | * |
||
| 71 | * @param NodeList $list The list |
||
| 72 | * @param integer $key The key |
||
| 73 | * |
||
| 74 | * @throws \ParseError (description) |
||
| 75 | * |
||
| 76 | * @return YamlObject The document as the separated part (by YAML::DOC_START) inside a whole YAML content |
||
| 77 | */ |
||
| 78 | private static function buildDocument(NodeList $list, int $key):YamlObject |
||
| 79 | { |
||
| 80 | // $childTypes = $list->getTypes(); |
||
| 81 | // $isaMapping = (bool) (Y::KEY|Y::MAPPING) & $childTypes; |
||
| 82 | // $isaSequence = (bool) Y::ITEM & $childTypes; |
||
| 83 | // $isaSet = (bool) Y::SET_VALUE & $childTypes; |
||
| 84 | // if ($isaMapping && $isaSequence) { |
||
| 85 | // throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
||
| 86 | // } else { |
||
| 87 | // switch (true) { |
||
| 88 | // case $isaSequence: $list->type = Y::SEQUENCE;break; |
||
| 89 | // case $isaSet: $list->type = Y::SET;break; |
||
| 90 | // default: $list->type = Y::MAPPING; |
||
| 91 | // } |
||
| 92 | // } |
||
| 93 | // self::$_root = new YamlObject(); |
||
| 94 | // $string = ''; |
||
| 95 | // foreach ($list as $child) { |
||
| 96 | // $result = self::build($child, self::$_root); |
||
| 97 | // if (is_string($result)) { |
||
| 98 | // $string .= $result.' '; |
||
| 99 | // } |
||
| 100 | // } |
||
| 101 | // if (!empty($string)) { |
||
| 102 | // self::$_root->setText(rtrim($string)); |
||
| 103 | // } |
||
| 104 | // if () { |
||
| 105 | // # code... |
||
| 106 | // } |
||
| 107 | // return self::$_root; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Generic function to distinguish between Node and NodeList |
||
| 112 | * |
||
| 113 | * @param Node|NodeList $node The node. |
||
| 114 | * @param mixed $parent The parent |
||
| 115 | * |
||
| 116 | * @return mixed ( description_of_the_return_value ) |
||
| 117 | */ |
||
| 118 | private static function build(object $node, &$parent = null) |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Builds a node list. |
||
| 126 | * |
||
| 127 | * @param NodeList $node The node |
||
| 128 | * @param mixed $parent The parent |
||
| 129 | * |
||
| 130 | * @return mixed The parent (object|array) or a string representing the NodeList. |
||
| 131 | */ |
||
| 132 | private static function buildNodeList(NodeList $node, &$parent=null) |
||
| 133 | { |
||
| 134 | if (is_null($node->type)) { |
||
| 135 | $childTypes = $node->getTypes(); |
||
| 136 | if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
||
| 137 | if ($childTypes & Y::ITEM) { |
||
| 138 | // TODO: replace the document index in HERE ----------v |
||
| 139 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, 0)); |
||
| 140 | } else { |
||
| 141 | $node->type = Y::MAPPING; |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | if ($childTypes & Y::ITEM) { |
||
| 145 | $node->type = Y::SEQUENCE; |
||
| 146 | } elseif (!($childTypes & Y::COMMENT)) |
||
| 147 | { |
||
| 148 | $node->type = Y::LITT_FOLDED; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | // var_dump('nodetype:'.Y::getName($node->type) ); |
||
| 153 | if ($node->type & (Y::RAW | Y::LITTERALS)) { |
||
| 154 | return self::buildLitteral($node, $node->type); |
||
| 155 | } |
||
| 156 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
||
| 157 | $out = $parent ?? new \StdClass;//var_dump("PAS LA"); |
||
| 158 | foreach ($node as $key => $child) { |
||
| 159 | if ($child->type & (Y::KEY)) { |
||
| 160 | self::buildKey($child, $out); |
||
| 161 | } else { |
||
| 162 | self::build($child, $out); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } elseif ($node->type & (Y::COMPACT_SEQUENCE|Y::SEQUENCE)) { |
||
| 166 | $out = $parent ?? [];//var_dump("HERE"); |
||
| 167 | foreach ($node as $key => $child) { |
||
| 168 | if ($child->type & Y::ITEM) { |
||
| 169 | self::buildItem($child, $out); |
||
| 170 | } else { |
||
| 171 | self::build($child); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } else { |
||
| 175 | $tmpString = null;//var_dump("PAS ICI"); |
||
| 176 | foreach ($node as $key => $child) { |
||
| 177 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
||
| 178 | if ($parent) { |
||
| 179 | $parent->setText(self::build($child, $parent)); |
||
| 180 | } else { |
||
| 181 | $tmpString .= self::build($child, $parent); |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | self::build($child, $parent); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $out = is_null($tmpString) ? $parent : $tmpString; |
||
| 188 | } |
||
| 189 | return $out; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Builds a node. |
||
| 194 | * |
||
| 195 | * @param Node $node The node of any Node->type |
||
| 196 | * @param mixed $parent The parent |
||
| 197 | * |
||
| 198 | * @return mixed The node value as scalar, array or object or null to otherwise. |
||
| 199 | */ |
||
| 200 | private static function buildNode(Node $node, &$parent) |
||
| 201 | { |
||
| 202 | extract((array) $node, EXTR_REFS); |
||
| 203 | if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
||
| 204 | if (is_object($value)) { |
||
| 205 | $tmp = self::build($value, $parent) ?? $parent; |
||
| 206 | } else { |
||
| 207 | $tmp = Node2PHP::get($node); |
||
| 208 | } |
||
| 209 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
||
| 210 | return self::$_root->getReference($identifier); |
||
| 211 | } |
||
| 212 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
||
| 213 | return self::buildNodeList($node->value, $parent); |
||
| 214 | } |
||
| 215 | if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
||
| 216 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
||
| 217 | Y::ITEM => 'buildItem', |
||
| 218 | Y::KEY => 'buildKey', |
||
| 219 | Y::SET_KEY => 'buildSetKey', |
||
| 220 | Y::SET_VALUE => 'buildSetValue', |
||
| 221 | Y::TAG => 'buildTag', |
||
| 222 | ]; |
||
| 223 | if (isset($typesActions[$type])) { |
||
| 224 | return self::{$typesActions[$type]}($node, $parent); |
||
| 225 | } |
||
| 226 | return is_object($value) ? self::build($value, $parent) : Node2PHP::get($node); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Builds a key and set the property + value to the given parent |
||
| 231 | * |
||
| 232 | * @param Node $node The node with type YAML::KEY |
||
| 233 | * @param object|array $parent The parent |
||
| 234 | * |
||
| 235 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
||
| 236 | * @return null |
||
| 237 | */ |
||
| 238 | private static function buildKey(Node $node, &$parent=null) |
||
| 239 | { |
||
| 240 | extract((array) $node, EXTR_REFS); |
||
| 241 | if (is_null($identifier)) { |
||
| 242 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $line)); |
||
| 243 | } else { |
||
| 244 | if ($value instanceof Node) { |
||
| 245 | if ($value->type & (Y::ITEM|Y::KEY)) { |
||
| 246 | $list = new NodeList(); |
||
| 247 | $list->push($value); |
||
| 248 | $list->type = $value->type & Y::ITEM ? Y::SEQUENCE : Y::MAPPING; |
||
| 249 | $value = $list; |
||
| 250 | } else { |
||
| 251 | $result = self::build($value); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | if ($value instanceof NodeList) { |
||
| 255 | $childTypes = $value->getTypes(); |
||
| 256 | if (is_null($value->type) && $childTypes & Y::SCALAR && !($childTypes & Y::COMMENT)) { |
||
| 257 | $result = self::buildLitteral($value, Y::LITT_FOLDED); |
||
| 258 | } else { |
||
| 259 | $result = self::buildNodeList($value); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | if (is_null($parent)) { |
||
| 263 | return $result; |
||
| 264 | } else { |
||
| 265 | if (is_array($parent)) { |
||
| 266 | $parent[$identifier] = $result; |
||
| 267 | } else { |
||
| 268 | $parent->{$identifier} = $result; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Builds an item. Adds the item value to the parent array|Iterator |
||
| 276 | * |
||
| 277 | * @param Node $node The node with type YAML::ITEM |
||
| 278 | * @param array|\Iterator $parent The parent |
||
| 279 | * |
||
| 280 | * @throws \Exception if parent is another type than array or object Iterator |
||
| 281 | * @return null |
||
| 282 | */ |
||
| 283 | private static function buildItem(Node $node, &$parent) |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Builds a litteral (folded or not) or any NodeList that has YAML::RAW type (like a multiline value) |
||
| 313 | * |
||
| 314 | * @param NodeList $children The children |
||
| 315 | * @param integer $type The type |
||
| 316 | * |
||
| 317 | * @return string The litteral. |
||
| 318 | */ |
||
| 319 | private static function buildLitteral(NodeList $children, int $type):string |
||
| 320 | { |
||
| 321 | $lines = []; |
||
| 322 | $children->rewind(); |
||
| 323 | $refIndent = $children->current()->indent; |
||
| 324 | //remove trailing blank nodes |
||
| 325 | $max = $children->count() - 1; |
||
| 326 | while ($children->offsetGet($max)->type & Y::BLANK) { |
||
| 327 | $children->offsetUnset($max); |
||
| 328 | $max = $children->count() - 1; |
||
| 329 | } |
||
| 330 | $children->rewind(); |
||
| 331 | // TODO : Example 6.1. Indentation Spaces spaces must be considered as content |
||
| 332 | foreach ($children as $child) { |
||
| 333 | if ($child->value instanceof NodeList) { |
||
| 334 | $lines[] = self::buildLitteral($child->value, $type); |
||
| 335 | } else { |
||
| 336 | $prefix = ''; |
||
| 337 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
||
| 338 | $prefix = "\n"; |
||
| 339 | } |
||
| 340 | if (!($child->type & (Y::SCALAR|Y::BLANK))) { |
||
| 341 | switch ($child->type) { |
||
| 342 | case Y::ITEM: $child->value = '- '.$child->value;break; |
||
| 343 | case Y::COMMENT: $child->value = '# '.$child->value;break; |
||
| 344 | default: //die(__METHOD__.Y::getName($child->type)); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | $val = $child->value; |
||
| 348 | while (is_object($val)) { |
||
| 349 | $val = $val->value; |
||
| 350 | } |
||
| 351 | $lines[] = $prefix.$val; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | if ($type & Y::RAW) return implode('', $lines); |
||
| 355 | if ($type & Y::LITT) return implode("\n", $lines); |
||
| 356 | if ($type & Y::LITT_FOLDED) return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
||
| 357 | return ''; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Builds a set key. |
||
| 362 | * |
||
| 363 | * @param Node $node The node of type YAML::SET_KEY. |
||
| 364 | * @param object $parent The parent |
||
| 365 | * |
||
| 366 | * @throws \Exception if a problem occurs during serialisation (json format) of the key |
||
| 367 | */ |
||
| 368 | private function buildSetKey(Node $node, &$parent) |
||
| 369 | { |
||
| 370 | $built = self::build($node->value); |
||
| 371 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
||
| 372 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
| 373 | // if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
||
| 374 | $parent->{trim($key, '\'" ')} = null; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Builds a set value. |
||
| 379 | * |
||
| 380 | * @param Node $node The node of type YAML::SET_VALUE |
||
| 381 | * @param object $parent The parent (the document object or any previous object created through a mapping key) |
||
| 382 | */ |
||
| 383 | private function buildSetValue(Node $node, &$parent) |
||
| 384 | { |
||
| 385 | $prop = array_keys(get_object_vars($parent)); |
||
| 386 | $key = end($prop); |
||
| 387 | if ($node->value->type & (Y::ITEM|Y::KEY|Y::SEQUENCE|Y::MAPPING)) { |
||
| 388 | $p = $node->value->type === Y::ITEM ? [] : new \StdClass; |
||
| 389 | self::build($node->value, $p); |
||
| 390 | } else { |
||
| 391 | $p = self::build($node->value, $parent->{$key}); |
||
| 392 | } |
||
| 393 | $parent->{$key} = $p; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Builds a tag and its value (also built) and encapsulates them in a Tag object. |
||
| 398 | * |
||
| 399 | * @param Node $node The node of type YAML::TAG |
||
| 400 | * @param mixed $parent The parent |
||
| 401 | * |
||
| 402 | * @return Tag|null The tag object of class Dallgoot\Yaml\Tag. |
||
| 403 | */ |
||
| 404 | private function buildTag(Node $node, &$parent) |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Builds a directive. NOT IMPLEMENTED YET |
||
| 432 | * |
||
| 433 | * @param Node $node The node |
||
| 434 | * @param mixed $parent The parent |
||
| 435 | */ |
||
| 436 | private function buildDirective(Node $node, $parent) |
||
| 438 | // TODO : implement |
||
| 439 | } |
||
| 440 | } |
||
| 441 |