| Total Complexity | 68 |
| Total Lines | 209 |
| 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 |
||
| 11 | class Builder |
||
| 12 | { |
||
| 13 | private static $root; |
||
| 14 | private static $debug; |
||
| 15 | |||
| 16 | const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
||
| 17 | const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence"; |
||
| 18 | |||
| 19 | |||
| 20 | private static function build(object $node, &$parent = null) |
||
| 24 | } |
||
| 25 | |||
| 26 | private static function buildDLL(DLL $node, &$parent) |
||
| 51 | } |
||
| 52 | |||
| 53 | private static function buildNode(Node $node, &$parent) |
||
| 54 | { |
||
| 55 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
||
| 56 | switch ($type) { |
||
| 57 | case T::COMMENT: self::$root->addComment($line, $value);return; |
||
|
1 ignored issue
–
show
|
|||
| 58 | case T::DIRECTIVE: return;//TODO |
||
|
1 ignored issue
–
show
|
|||
| 59 | case T::ITEM: self::buildItem($value, $parent);return; |
||
|
1 ignored issue
–
show
|
|||
| 60 | case T::KEY: self::buildKey($node, $parent);return; |
||
|
1 ignored issue
–
show
|
|||
| 61 | case T::REF_DEF: //fall through |
||
|
1 ignored issue
–
show
|
|||
| 62 | case T::REF_CALL://TODO: self::build returns what ? |
||
|
1 ignored issue
–
show
|
|||
| 63 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
| 64 | if ($type === T::REF_DEF) self::$root->addReference($identifier, $tmp); |
||
|
1 ignored issue
–
show
|
|||
| 65 | return self::$root->getReference($identifier); |
||
| 66 | case T::SET_KEY: |
||
|
1 ignored issue
–
show
|
|||
| 67 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||
| 68 | if (empty($key)) |
||
|
1 ignored issue
–
show
|
|||
| 69 | throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
||
| 70 | $parent->{$key} = null; |
||
| 71 | return; |
||
| 72 | case T::SET_VALUE: |
||
|
1 ignored issue
–
show
|
|||
| 73 | $prop = array_keys(get_object_vars($parent)); |
||
| 74 | $key = end($prop); |
||
| 75 | if (property_exists($value, "type") && in_array($value->type, [T::ITEM, T::MAPPING])) { |
||
|
1 ignored issue
–
show
|
|||
| 76 | $p = $value->type === T::ITEM ? [] : new \StdClass; |
||
| 77 | self::build($value, $p); |
||
| 78 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 79 | $p = self::build($value, $parent->{$key}); |
||
| 80 | } |
||
|
1 ignored issue
–
show
|
|||
| 81 | $parent->{$key} = $p; |
||
| 82 | return; |
||
| 83 | case T::TAG: |
||
|
1 ignored issue
–
show
|
|||
| 84 | if ($parent === self::$root) { |
||
|
1 ignored issue
–
show
|
|||
| 85 | $parent->addTag($identifier);return; |
||
| 86 | } else { |
||
|
1 ignored issue
–
show
|
|||
| 87 | if (in_array($identifier, ['!binary', '!str'])) { |
||
|
1 ignored issue
–
show
|
|||
| 88 | if ($value->value instanceof DLL) $value->value->type = T::RAW; |
||
|
1 ignored issue
–
show
|
|||
| 89 | else $value->type = T::RAW; |
||
|
2 ignored issues
–
show
|
|||
| 90 | } |
||
|
1 ignored issue
–
show
|
|||
| 91 | $val = is_null($value) ? null : self::build($value, $node); |
||
| 92 | return new Tag($identifier, $val); |
||
| 93 | } |
||
|
1 ignored issue
–
show
|
|||
| 94 | default: |
||
|
1 ignored issue
–
show
|
|||
| 95 | return is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Builds a key and set the property + value to the parent given |
||
| 101 | * |
||
| 102 | * @param Node $node The node |
||
|
3 ignored issues
–
show
|
|||
| 103 | * @param object|array $parent The parent |
||
|
3 ignored issues
–
show
|
|||
| 104 | * @throws \ParseError if Key has no name(identifier) |
||
| 105 | */ |
||
| 106 | private static function buildKey($node, &$parent):void |
||
| 107 | { |
||
| 108 | list($identifier, $value) = [$node->identifier, $node->value]; |
||
| 109 | if (is_null($identifier)) { |
||
| 110 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
||
| 111 | } else { |
||
| 112 | if ($value instanceof Node && in_array($value->type, [T::KEY, T::ITEM])) { |
||
| 113 | $parent->{$identifier} = $value->type === T::KEY ? new \StdClass : []; |
||
| 114 | self::build($value, $parent->{$identifier}); |
||
| 115 | } elseif (is_object($value)) { |
||
| 116 | $parent->{$identifier} = self::build($value, $parent->{$identifier}); |
||
| 117 | } else { |
||
| 118 | $parent->{$identifier} = $node->getPhpValue(); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | private static function buildItem($value, &$parent):void |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Builds a file. check multiple documents & split if more than one documents |
||
| 135 | * |
||
| 136 | * @param Node $root The root node |
||
|
3 ignored issues
–
show
|
|||
| 137 | * @return array|YamlObject list of documents or juste one. |
||
|
1 ignored issue
–
show
|
|||
| 138 | */ |
||
| 139 | public static function buildContent(Node $root, int $debug) |
||
| 140 | { |
||
| 141 | self::$debug = $debug; |
||
| 142 | $totalDocStart = 0; |
||
| 143 | $documents = []; |
||
| 144 | if ($root->value instanceof Node) { |
||
| 145 | $q = new DLL; |
||
| 146 | $q->push($root->value); |
||
| 147 | return [self::buildDocument($q, 0)]; |
||
| 148 | } |
||
| 149 | $root->value->setIteratorMode(DLL::IT_MODE_DELETE); |
||
| 150 | foreach ($root->value as $child) { |
||
| 151 | if ($child->type === T::DOC_START) $totalDocStart++; |
||
|
1 ignored issue
–
show
|
|||
| 152 | //if 0 or 1 DOC_START = we are still in first document |
||
| 153 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
||
| 154 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new DLL(); |
||
|
1 ignored issue
–
show
|
|||
| 155 | $documents[$currentDoc]->push($child); |
||
| 156 | } |
||
| 157 | $debug >= 2 && var_dump($documents); |
||
| 158 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
||
| 159 | return count($content) === 1 ? $content[0] : $content; |
||
| 160 | } |
||
| 161 | |||
| 162 | private static function buildDocument(DLL $list, int $key):YamlObject |
||
| 163 | { |
||
| 164 | self::$root = new YamlObject(); |
||
| 165 | $childTypes = self::getChildrenTypes($list); |
||
| 166 | $isMapping = count(array_intersect($childTypes, [T::KEY, T::MAPPING])) > 0; |
||
| 167 | $isSequence = in_array(T::ITEM, $childTypes); |
||
| 168 | $isSet = in_array(T::SET_VALUE, $childTypes); |
||
| 169 | if ($isMapping && $isSequence) { |
||
| 170 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
||
| 171 | } else { |
||
| 172 | switch (true) { |
||
| 173 | case $isSequence: $list->type = T::SEQUENCE;break; |
||
|
1 ignored issue
–
show
|
|||
| 174 | case $isSet: $list->type = T::SET;break; |
||
|
1 ignored issue
–
show
|
|||
| 175 | case $isMapping://fall through |
||
|
1 ignored issue
–
show
|
|||
| 176 | default:$list->type = T::MAPPING; |
||
|
1 ignored issue
–
show
|
|||
| 177 | } |
||
| 178 | } |
||
| 179 | self::$debug >= 3 && var_dump(self::$root, $list); |
||
| 180 | $string = ''; |
||
| 181 | foreach ($list as $child) { |
||
| 182 | $result = self::build($child, self::$root); |
||
| 183 | if (is_string($result)) { |
||
| 184 | $string .= $result.' '; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | if (!empty($string)) { |
||
| 188 | self::$root->setText(rtrim($string)); |
||
| 189 | } |
||
| 190 | return self::$root; |
||
| 191 | } |
||
| 192 | |||
| 193 | private static function litteral(DLL $children, $type):string |
||
| 211 | } |
||
| 212 | |||
| 213 | private static function getChildrenTypes(DLL $children):array |
||
| 214 | { |
||
| 220 | } |
||
| 221 | } |
||
| 222 |