| Total Complexity | 90 |
| Total Lines | 387 |
| 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) |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Generic function to distinguish between Node and NodeList |
||
| 70 | * |
||
| 71 | * @param Node|NodeList $node The node. |
||
| 72 | * @param mixed $parent The parent |
||
| 73 | * |
||
| 74 | * @return mixed ( description_of_the_return_value ) |
||
| 75 | */ |
||
| 76 | private static function build(object $node, &$parent = null) |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Builds a node list. |
||
| 84 | * |
||
| 85 | * @param NodeList $node The node |
||
| 86 | * @param mixed $parent The parent |
||
| 87 | * |
||
| 88 | * @return mixed The parent (object|array) or a string representing the NodeList. |
||
| 89 | */ |
||
| 90 | private static function buildNodeList(NodeList $node, &$parent=null) |
||
| 91 | { |
||
| 92 | if (is_null($node->type)) { |
||
| 93 | $childTypes = $node->getTypes(); |
||
| 94 | if ($childTypes & (Y::KEY|Y::SET_KEY)) { |
||
| 95 | if ($childTypes & Y::ITEM) { |
||
| 96 | // TODO: replace the document index in HERE ----------v |
||
| 97 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, 0)); |
||
| 98 | } else { |
||
| 99 | $node->type = Y::MAPPING; |
||
| 100 | } |
||
| 101 | } else { |
||
| 102 | if ($childTypes & Y::ITEM) { |
||
| 103 | $node->type = Y::SEQUENCE; |
||
| 104 | } elseif (!($childTypes & Y::COMMENT)) { |
||
| 105 | $node->type = Y::LITT_FOLDED; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | // var_dump('nodetype:'.Y::getName($node->type) ); |
||
| 110 | if ($node->type & (Y::RAW | Y::LITTERALS)) { |
||
| 111 | return self::buildLitteral($node, $node->type); |
||
| 112 | } |
||
| 113 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
||
| 114 | $out = $parent ?? new \StdClass;//var_dump("PAS LA"); |
||
| 115 | foreach ($node as $key => $child) { |
||
| 116 | if ($child->type & (Y::KEY)) { |
||
| 117 | self::buildKey($child, $out); |
||
| 118 | } else { |
||
| 119 | self::build($child, $out); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | if ($node->type & Y::COMPACT_MAPPING) { |
||
| 123 | $out = new Compact($out); |
||
| 124 | } |
||
| 125 | } elseif ($node->type & (Y::COMPACT_SEQUENCE|Y::SEQUENCE)) { |
||
| 126 | $out = $parent ?? [];//var_dump("HERE"); |
||
| 127 | foreach ($node as $key => $child) { |
||
| 128 | if ($child->type & Y::ITEM) { |
||
| 129 | self::buildItem($child, $out); |
||
| 130 | } else { |
||
| 131 | self::build($child); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | if ($node->type & Y::COMPACT_SEQUENCE) { |
||
| 135 | $out = new Compact($out); |
||
| 136 | } |
||
| 137 | } else { |
||
| 138 | $tmpString = null;//var_dump("PAS ICI"); |
||
| 139 | foreach ($node as $key => $child) { |
||
| 140 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
||
| 141 | if ($parent) { |
||
| 142 | $parent->setText(self::build($child, $parent)); |
||
| 143 | } else { |
||
| 144 | $tmpString .= self::build($child, $parent); |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | self::build($child, $parent); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | $out = is_null($tmpString) ? $parent : $tmpString; |
||
| 151 | } |
||
| 152 | return $out; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Builds a node. |
||
| 157 | * |
||
| 158 | * @param Node $node The node of any Node->type |
||
| 159 | * @param mixed $parent The parent |
||
| 160 | * |
||
| 161 | * @return mixed The node value as scalar, array or object or null to otherwise. |
||
| 162 | */ |
||
| 163 | private static function buildNode(Node $node, &$parent) |
||
| 164 | { |
||
| 165 | extract((array) $node, EXTR_REFS); |
||
| 166 | if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
||
| 167 | if (is_object($value)) { |
||
| 168 | $tmp = self::build($value, $parent) ?? $parent; |
||
| 169 | } else { |
||
| 170 | $tmp = Node2PHP::get($node); |
||
| 171 | } |
||
| 172 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
||
| 173 | return self::$_root->getReference($identifier); |
||
| 174 | } |
||
| 175 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
||
| 176 | return self::buildNodeList($node->value, $parent); |
||
| 177 | } |
||
| 178 | if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
||
| 179 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
||
| 180 | Y::ITEM => 'buildItem', |
||
| 181 | Y::KEY => 'buildKey', |
||
| 182 | Y::SET_KEY => 'buildSetKey', |
||
| 183 | Y::SET_VALUE => 'buildSetValue', |
||
| 184 | Y::TAG => 'buildTag', |
||
| 185 | ]; |
||
| 186 | if (isset($typesActions[$type])) { |
||
| 187 | return self::{$typesActions[$type]}($node, $parent); |
||
| 188 | } |
||
| 189 | return is_object($value) ? self::build($value, $parent) : Node2PHP::get($node); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Builds a key and set the property + value to the given parent |
||
| 194 | * |
||
| 195 | * @param Node $node The node with type YAML::KEY |
||
| 196 | * @param object|array $parent The parent |
||
| 197 | * |
||
| 198 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
||
| 199 | * @return null |
||
| 200 | */ |
||
| 201 | private static function buildKey(Node $node, &$parent=null) |
||
| 202 | { |
||
| 203 | extract((array) $node, EXTR_REFS); |
||
| 204 | if (is_null($identifier)) { |
||
| 205 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $line)); |
||
| 206 | } else { |
||
| 207 | if ($value instanceof Node) { |
||
| 208 | if ($value->type & (Y::ITEM|Y::KEY)) { |
||
| 209 | $list = new NodeList(); |
||
| 210 | $list->push($value); |
||
| 211 | $list->type = $value->type & Y::ITEM ? Y::SEQUENCE : Y::MAPPING; |
||
| 212 | $value = $list; |
||
| 213 | } else { |
||
| 214 | $result = self::build($value); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | if ($value instanceof NodeList) { |
||
| 218 | $childTypes = $value->getTypes(); |
||
| 219 | if (is_null($value->type) && $childTypes & Y::SCALAR && !($childTypes & Y::COMMENT)) { |
||
| 220 | $result = self::buildLitteral($value, Y::LITT_FOLDED); |
||
| 221 | } else { |
||
| 222 | $result = self::buildNodeList($value); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | if (is_null($parent)) { |
||
| 226 | return $result; |
||
| 227 | } else { |
||
| 228 | if (is_array($parent)) { |
||
| 229 | $parent[$identifier] = $result; |
||
| 230 | } else { |
||
| 231 | $parent->{$identifier} = $result; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Builds an item. Adds the item value to the parent array|Iterator |
||
| 239 | * |
||
| 240 | * @param Node $node The node with type YAML::ITEM |
||
| 241 | * @param array|\Iterator $parent The parent |
||
| 242 | * |
||
| 243 | * @throws \Exception if parent is another type than array or object Iterator |
||
| 244 | * @return null |
||
| 245 | */ |
||
| 246 | private static function buildItem(Node $node, &$parent) |
||
| 271 | } |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * Builds a litteral (folded or not) or any NodeList that has YAML::RAW type (like a multiline value) |
||
| 276 | * |
||
| 277 | * @param NodeList $children The children |
||
| 278 | * @param integer $type The type |
||
| 279 | * |
||
| 280 | * @return string The litteral. |
||
| 281 | */ |
||
| 282 | private static function buildLitteral(NodeList $children, int $type):string |
||
| 283 | { |
||
| 284 | $lines = []; |
||
| 285 | $children->rewind(); |
||
| 286 | $refIndent = $children->current()->indent; |
||
| 287 | //remove trailing blank nodes |
||
| 288 | $max = $children->count() - 1; |
||
| 289 | while ($children->offsetGet($max)->type & Y::BLANK) { |
||
| 290 | $children->offsetUnset($max); |
||
| 291 | $max = $children->count() - 1; |
||
| 292 | } |
||
| 293 | $children->rewind(); |
||
| 294 | // TODO : Example 6.1. Indentation Spaces spaces must be considered as content |
||
| 295 | foreach ($children as $child) { |
||
| 296 | if ($child->value instanceof NodeList) { |
||
| 297 | $lines[] = self::buildLitteral($child->value, $type); |
||
| 298 | } else { |
||
| 299 | $prefix = ''; |
||
| 300 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
||
| 301 | $prefix = "\n"; |
||
| 302 | } |
||
| 303 | if (!($child->type & (Y::SCALAR|Y::BLANK))) { |
||
| 304 | switch ($child->type) { |
||
| 305 | case Y::ITEM: $child->value = '- '.$child->value;break; |
||
| 306 | case Y::COMMENT: $child->value = '# '.$child->value;break; |
||
| 307 | default: //die(__METHOD__.Y::getName($child->type)); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | $val = $child->value; |
||
| 311 | while (is_object($val)) { |
||
| 312 | $val = $val->value; |
||
| 313 | } |
||
| 314 | $lines[] = $prefix.$val; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | if ($type & Y::RAW) return implode('', $lines); |
||
| 318 | if ($type & Y::LITT) return implode("\n", $lines); |
||
| 319 | if ($type & Y::LITT_FOLDED) return preg_replace(['/ +(\n)/','/(\n+) +/'], "$1", implode(' ', $lines)); |
||
| 320 | return ''; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Builds a set key. |
||
| 325 | * |
||
| 326 | * @param Node $node The node of type YAML::SET_KEY. |
||
| 327 | * @param object $parent The parent |
||
| 328 | * |
||
| 329 | * @throws \Exception if a problem occurs during serialisation (json format) of the key |
||
| 330 | */ |
||
| 331 | private function buildSetKey(Node $node, &$parent) |
||
| 332 | { |
||
| 333 | $built = is_object($node->value) ? self::build($node->value) : null; |
||
| 334 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
||
| 335 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
| 336 | // if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
||
| 337 | $parent->{trim($key, '\'" ')} = null; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Builds a set value. |
||
| 342 | * |
||
| 343 | * @param Node $node The node of type YAML::SET_VALUE |
||
| 344 | * @param object $parent The parent (the document object or any previous object created through a mapping key) |
||
| 345 | */ |
||
| 346 | private function buildSetValue(Node $node, &$parent) |
||
| 347 | { |
||
| 348 | $prop = array_keys(get_object_vars($parent)); |
||
| 349 | $key = end($prop); |
||
| 350 | if ($node->value->type & (Y::ITEM|Y::KEY|Y::SEQUENCE|Y::MAPPING)) { |
||
| 351 | $p = $node->value->type === Y::ITEM ? [] : new \StdClass; |
||
| 352 | self::build($node->value, $p); |
||
| 353 | } else { |
||
| 354 | $p = self::build($node->value, $parent->{$key}); |
||
| 355 | } |
||
| 356 | $parent->{$key} = $p; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Builds a tag and its value (also built) and encapsulates them in a Tag object. |
||
| 361 | * |
||
| 362 | * @param Node $node The node of type YAML::TAG |
||
| 363 | * @param mixed $parent The parent |
||
| 364 | * |
||
| 365 | * @return Tag|null The tag object of class Dallgoot\Yaml\Tag. |
||
| 366 | */ |
||
| 367 | private static function buildTag(Node $node, &$parent) |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Builds a directive. NOT IMPLEMENTED YET |
||
| 395 | * |
||
| 396 | * @param Node $node The node |
||
| 397 | * @param mixed $parent The parent |
||
| 398 | */ |
||
| 399 | private function buildDirective(Node $node, $parent) |
||
| 401 | // TODO : implement |
||
| 402 | } |
||
| 403 | } |
||
| 404 |