| Conditions | 45 |
| Paths | > 20000 |
| Total Lines | 205 |
| Code Lines | 115 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 50 | private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null) |
||
| 51 | { |
||
| 52 | $rootName = ($root ? 'config' : $node->getName()); |
||
| 53 | $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null)); |
||
| 54 | |||
| 55 | // xml remapping |
||
| 56 | if ($node->getParent()) { |
||
| 57 | $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) { |
||
| 58 | return $rootName === $mapping[1]; |
||
| 59 | }); |
||
| 60 | |||
| 61 | if (count($remapping)) { |
||
| 62 | list($singular) = current($remapping); |
||
| 63 | $rootName = $singular; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | $rootName = str_replace('_', '-', $rootName); |
||
| 67 | |||
| 68 | $rootAttributes = array(); |
||
| 69 | $rootAttributeComments = array(); |
||
| 70 | $rootChildren = array(); |
||
| 71 | $rootComments = array(); |
||
| 72 | |||
| 73 | if ($node instanceof ArrayNode) { |
||
| 74 | $children = $node->getChildren(); |
||
| 75 | |||
| 76 | // comments about the root node |
||
| 77 | if ($rootInfo = $node->getInfo()) { |
||
| 78 | $rootComments[] = $rootInfo; |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($rootNamespace) { |
||
| 82 | $rootComments[] = 'Namespace: '.$rootNamespace; |
||
| 83 | } |
||
| 84 | |||
| 85 | // render prototyped nodes |
||
| 86 | if ($node instanceof PrototypedArrayNode) { |
||
| 87 | $prototype = $node->getPrototype(); |
||
| 88 | |||
| 89 | $info = 'prototype'; |
||
| 90 | if (null !== $prototype->getInfo()) { |
||
| 91 | $info .= ': '.$prototype->getInfo(); |
||
| 92 | } |
||
| 93 | array_unshift($rootComments, $info); |
||
| 94 | |||
| 95 | if ($key = $node->getKeyAttribute()) { |
||
| 96 | $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key; |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($prototype instanceof PrototypedArrayNode) { |
||
| 100 | $prototype->setName($key); |
||
| 101 | $children = array($key => $prototype); |
||
| 102 | } elseif ($prototype instanceof ArrayNode) { |
||
| 103 | $children = $prototype->getChildren(); |
||
| 104 | } else { |
||
| 105 | if ($prototype->hasDefaultValue()) { |
||
| 106 | $prototypeValue = $prototype->getDefaultValue(); |
||
| 107 | } else { |
||
| 108 | switch (get_class($prototype)) { |
||
| 109 | case 'Symfony\Component\Config\Definition\ScalarNode': |
||
| 110 | $prototypeValue = 'scalar value'; |
||
| 111 | break; |
||
| 112 | |||
| 113 | case 'Symfony\Component\Config\Definition\FloatNode': |
||
| 114 | case 'Symfony\Component\Config\Definition\IntegerNode': |
||
| 115 | $prototypeValue = 'numeric value'; |
||
| 116 | break; |
||
| 117 | |||
| 118 | case 'Symfony\Component\Config\Definition\BooleanNode': |
||
| 119 | $prototypeValue = 'true|false'; |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 'Symfony\Component\Config\Definition\EnumNode': |
||
| 123 | $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues())); |
||
| 124 | break; |
||
| 125 | |||
| 126 | default: |
||
| 127 | $prototypeValue = 'value'; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | // get attributes and elements |
||
| 134 | foreach ($children as $child) { |
||
| 135 | if (!$child instanceof ArrayNode) { |
||
| 136 | // get attributes |
||
| 137 | |||
| 138 | // metadata |
||
| 139 | $name = str_replace('_', '-', $child->getName()); |
||
| 140 | $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world |
||
| 141 | |||
| 142 | // comments |
||
| 143 | $comments = array(); |
||
| 144 | if ($info = $child->getInfo()) { |
||
| 145 | $comments[] = $info; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($example = $child->getExample()) { |
||
| 149 | $comments[] = 'Example: '.$example; |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($child->isRequired()) { |
||
| 153 | $comments[] = 'Required'; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($child instanceof EnumNode) { |
||
| 157 | $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues())); |
||
| 158 | } |
||
| 159 | |||
| 160 | if (count($comments)) { |
||
| 161 | $rootAttributeComments[$name] = implode(";\n", $comments); |
||
| 162 | } |
||
| 163 | |||
| 164 | // default values |
||
| 165 | if ($child->hasDefaultValue()) { |
||
| 166 | $value = $child->getDefaultValue(); |
||
| 167 | } |
||
| 168 | |||
| 169 | // append attribute |
||
| 170 | $rootAttributes[$name] = $value; |
||
| 171 | } else { |
||
| 172 | // get elements |
||
| 173 | $rootChildren[] = $child; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | // render comments |
||
| 179 | |||
| 180 | // root node comment |
||
| 181 | if (count($rootComments)) { |
||
| 182 | foreach ($rootComments as $comment) { |
||
| 183 | $this->writeLine('<!-- '.$comment.' -->', $depth); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | // attribute comments |
||
| 188 | if (count($rootAttributeComments)) { |
||
| 189 | foreach ($rootAttributeComments as $attrName => $comment) { |
||
| 190 | $commentDepth = $depth + 4 + strlen($attrName) + 2; |
||
| 191 | $commentLines = explode("\n", $comment); |
||
| 192 | $multiline = (count($commentLines) > 1); |
||
| 193 | $comment = implode(PHP_EOL.str_repeat(' ', $commentDepth), $commentLines); |
||
| 194 | |||
| 195 | if ($multiline) { |
||
| 196 | $this->writeLine('<!--', $depth); |
||
| 197 | $this->writeLine($attrName.': '.$comment, $depth + 4); |
||
| 198 | $this->writeLine('-->', $depth); |
||
| 199 | } else { |
||
| 200 | $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | // render start tag + attributes |
||
| 206 | $rootIsVariablePrototype = isset($prototypeValue); |
||
| 207 | $rootIsEmptyTag = (0 === count($rootChildren) && !$rootIsVariablePrototype); |
||
| 208 | $rootOpenTag = '<'.$rootName; |
||
| 209 | if (1 >= ($attributesCount = count($rootAttributes))) { |
||
| 210 | if (1 === $attributesCount) { |
||
| 211 | $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes))); |
||
| 212 | } |
||
| 213 | |||
| 214 | $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>'; |
||
| 215 | |||
| 216 | if ($rootIsVariablePrototype) { |
||
| 217 | $rootOpenTag .= $prototypeValue.'</'.$rootName.'>'; |
||
| 218 | } |
||
| 219 | |||
| 220 | $this->writeLine($rootOpenTag, $depth); |
||
| 221 | } else { |
||
| 222 | $this->writeLine($rootOpenTag, $depth); |
||
| 223 | |||
| 224 | $i = 1; |
||
| 225 | |||
| 226 | foreach ($rootAttributes as $attrName => $attrValue) { |
||
| 227 | $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue)); |
||
| 228 | |||
| 229 | $this->writeLine($attr, $depth + 4); |
||
| 230 | |||
| 231 | if ($attributesCount === $i++) { |
||
| 232 | $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth); |
||
| 233 | |||
| 234 | if ($rootIsVariablePrototype) { |
||
| 235 | $rootOpenTag .= $prototypeValue.'</'.$rootName.'>'; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | // render children tags |
||
| 242 | foreach ($rootChildren as $child) { |
||
| 243 | $this->writeLine(''); |
||
| 244 | $this->writeNode($child, $depth + 4); |
||
| 245 | } |
||
| 246 | |||
| 247 | // render end tag |
||
| 248 | if (!$rootIsEmptyTag && !$rootIsVariablePrototype) { |
||
| 249 | $this->writeLine(''); |
||
| 250 | |||
| 251 | $rootEndTag = '</'.$rootName.'>'; |
||
| 252 | $this->writeLine($rootEndTag, $depth); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 308 |