| Total Complexity | 67 |
| Total Lines | 214 |
| 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 | $type = &$node->type; |
||
| 32 | if ($type & (Y\RAW | Y\LITTERALS)) { |
||
| 33 | return self::litteral($node, $type); |
||
| 34 | } |
||
| 35 | $p = $parent; |
||
| 36 | switch ($type) { |
||
| 37 | case Y\MAPPING: //fall through |
||
|
1 ignored issue
–
show
|
|||
| 38 | case Y\SET: $p = new \StdClass; break; |
||
|
1 ignored issue
–
show
|
|||
| 39 | case Y\SEQUENCE: $p = []; break; |
||
|
1 ignored issue
–
show
|
|||
| 40 | // case Y\KEY: $p = $parent;break; |
||
| 41 | } |
||
| 42 | $out = null; |
||
| 43 | foreach ($node as $child) { |
||
| 44 | $result = self::build($child, $p); |
||
| 45 | if (!is_null($result)) { |
||
| 46 | if (is_string($result)) { |
||
| 47 | $out .= $result.' '; |
||
| 48 | } else { |
||
| 49 | return $result; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | return is_null($out) ? $p : rtrim($out); |
||
| 54 | } |
||
| 55 | |||
| 56 | private static function buildNode(Node $node, &$parent) |
||
| 57 | { |
||
| 58 | extract((array) $node, EXTR_REFS); |
||
| 59 | switch ($type) { |
||
| 60 | case Y\COMMENT: self::$_root->addComment($line, $value); return; |
||
|
1 ignored issue
–
show
|
|||
| 61 | case Y\DIRECTIVE: return; //TODO |
||
|
1 ignored issue
–
show
|
|||
| 62 | case Y\ITEM: self::buildItem($value, $parent); return; |
||
|
1 ignored issue
–
show
|
|||
| 63 | case Y\KEY: self::buildKey($node, $parent); return; |
||
|
1 ignored issue
–
show
|
|||
| 64 | case Y\REF_DEF: //fall through |
||
|
1 ignored issue
–
show
|
|||
| 65 | case Y\REF_CALL://TODO: self::build returns what ? |
||
|
1 ignored issue
–
show
|
|||
| 66 | if (is_object($value)) { |
||
|
1 ignored issue
–
show
|
|||
| 67 | $result = self::build($value, $parent); |
||
| 68 | if (is_null($result)) { |
||
|
1 ignored issue
–
show
|
|||
| 69 | $tmp = $parent; |
||
| 70 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 71 | $tmp = $result; |
||
| 72 | } |
||
|
1 ignored issue
–
show
|
|||
| 73 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 74 | $tmp = $node->getPhpValue(); |
||
| 75 | } |
||
|
1 ignored issue
–
show
|
|||
| 76 | // $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
| 77 | if ($type === Y\REF_DEF) self::$_root->addReference($identifier, $tmp); |
||
| 78 | return self::$_root->getReference($identifier); |
||
| 79 | case Y\SET_KEY: |
||
|
1 ignored issue
–
show
|
|||
| 80 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
| 81 | if (empty($key)) |
||
| 82 | throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
||
| 83 | $parent->{$key} = null; |
||
| 84 | return; |
||
| 85 | case Y\SET_VALUE: |
||
|
1 ignored issue
–
show
|
|||
| 86 | $prop = array_keys(get_object_vars($parent)); |
||
| 87 | $key = end($prop); |
||
| 88 | if (property_exists($value, "type") && ($value->type & (Y\ITEM|Y\MAPPING))) { |
||
|
1 ignored issue
–
show
|
|||
| 89 | $p = $value->type === Y\ITEM ? [] : new \StdClass; |
||
| 90 | self::build($value, $p); |
||
| 91 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 92 | $p = self::build($value, $parent->{$key}); |
||
| 93 | } |
||
|
1 ignored issue
–
show
|
|||
| 94 | $parent->{$key} = $p; |
||
| 95 | return; |
||
| 96 | case Y\TAG: |
||
|
1 ignored issue
–
show
|
|||
| 97 | if ($parent === self::$_root) { |
||
|
1 ignored issue
–
show
|
|||
| 98 | $parent->addTag($identifier); return; |
||
| 99 | } else {//TODO: have somewhere a list of common tags and their treatment |
||
|
1 ignored issue
–
show
|
|||
| 100 | if (in_array($identifier, ['!binary', '!str'])) { |
||
|
1 ignored issue
–
show
|
|||
| 101 | if ($value->value instanceof NodeList) $value->value->type = Y\RAW; |
||
| 102 | else $value->type = Y\RAW; |
||
|
1 ignored issue
–
show
|
|||
| 103 | } |
||
|
1 ignored issue
–
show
|
|||
| 104 | $val = is_null($value) ? null : self::build(/** @scrutinizer ignore-type */ $value, $node); |
||
|
2 ignored issues
–
show
|
|||
| 105 | return new Tag($identifier, $val); |
||
| 106 | } |
||
|
1 ignored issue
–
show
|
|||
| 107 | default: |
||
|
1 ignored issue
–
show
|
|||
| 108 | return is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Builds a key and set the property + value to the given parent |
||
| 114 | * |
||
| 115 | * @param Node $node The node |
||
|
2 ignored issues
–
show
|
|||
| 116 | * @param object|array $parent The parent |
||
|
1 ignored issue
–
show
|
|||
| 117 | * |
||
| 118 | * @throws \ParseError if Key has no name(identifier) |
||
| 119 | * @return null |
||
| 120 | */ |
||
| 121 | private static function buildKey($node, &$parent):void |
||
| 122 | { |
||
| 123 | extract((array) $node, EXTR_REFS); |
||
| 124 | if (is_null($identifier)) { |
||
| 125 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $line)); |
||
| 126 | } else { |
||
| 127 | if ($value instanceof Node && ($value->type & (Y\KEY|Y\ITEM))) { |
||
| 128 | $parent->{$identifier} = $value->type & Y\KEY ? new \StdClass : []; |
||
| 129 | self::build($value, $parent->{$identifier}); |
||
| 130 | } elseif (is_object($value)) { |
||
| 131 | $parent->{$identifier} = self::build($value, $parent->{$identifier}); |
||
| 132 | } else { |
||
| 133 | $parent->{$identifier} = $node->getPhpValue(); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | private static function buildItem($value, &$parent):void |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Builds a file. check multiple documents & split if more than one documents |
||
| 153 | * |
||
| 154 | * @param Node $_root The root node |
||
|
3 ignored issues
–
show
|
|||
| 155 | * @param int $_debug the level of debugging requested |
||
|
3 ignored issues
–
show
|
|||
| 156 | * |
||
| 157 | * @return array|YamlObject list of documents or juste one. |
||
| 158 | */ |
||
| 159 | public static function buildContent(Node $_root, int $_debug) |
||
| 160 | { |
||
| 161 | self::$_debug = $_debug; |
||
| 162 | $totalDocStart = 0; |
||
| 163 | $documents = []; |
||
| 164 | if ($_root->value instanceof Node) { |
||
| 165 | $q = new NodeList; |
||
| 166 | $q->push($_root->value); |
||
| 167 | return self::buildDocument($q, 0); |
||
| 168 | } |
||
| 169 | $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
||
| 170 | foreach ($_root->value as $child) { |
||
| 171 | if ($child->type & Y\DOC_START) $totalDocStart++; |
||
| 172 | //if 0 or 1 DOC_START = we are still in first document |
||
| 173 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
||
| 174 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
||
| 175 | $documents[$currentDoc]->push($child); |
||
| 176 | } |
||
| 177 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
||
| 178 | return count($content) === 1 ? $content[0] : $content; |
||
| 179 | } |
||
| 180 | |||
| 181 | private static function buildDocument(NodeList $list, int $key):YamlObject |
||
| 182 | { |
||
| 183 | self::$_root = new YamlObject(); |
||
| 184 | $childTypes = $list->getTypes(); |
||
| 185 | $isMapping = (Y\KEY | Y\MAPPING) & $childTypes; |
||
| 186 | $isSequence = Y\ITEM & $childTypes; |
||
| 187 | $isSet = Y\SET_VALUE & $childTypes; |
||
| 188 | if ($isMapping && $isSequence) { |
||
| 189 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
||
| 190 | } else { |
||
| 191 | switch (true) { |
||
| 192 | case $isSequence: $list->type = Y\SEQUENCE;break; |
||
|
1 ignored issue
–
show
|
|||
| 193 | case $isSet: $list->type = Y\SET;break; |
||
|
1 ignored issue
–
show
|
|||
| 194 | default: $list->type = Y\MAPPING; |
||
|
1 ignored issue
–
show
|
|||
| 195 | } |
||
| 196 | } |
||
| 197 | $string = ''; |
||
| 198 | foreach ($list as $child) { |
||
| 199 | $result = self::build($child, self::$_root); |
||
| 200 | if (is_string($result)) { |
||
| 201 | $string .= $result.' '; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | if (!empty($string)) { |
||
| 205 | self::$_root->setText(rtrim($string)); |
||
| 206 | } |
||
| 207 | return self::$_root; |
||
| 208 | } |
||
| 209 | |||
| 210 | private static function litteral(NodeList $children, $type):string |
||
| 228 | } |
||
| 229 | } |
||
| 230 |