| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 96 |
| Code Lines | 55 |
| 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 |
||
| 77 | private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = false) |
||
| 78 | { |
||
| 79 | $comments = array(); |
||
| 80 | $default = ''; |
||
| 81 | $defaultArray = null; |
||
| 82 | $children = null; |
||
| 83 | $example = $node->getExample(); |
||
| 84 | |||
| 85 | // defaults |
||
| 86 | if ($node instanceof ArrayNode) { |
||
| 87 | $children = $node->getChildren(); |
||
| 88 | |||
| 89 | if ($node instanceof PrototypedArrayNode) { |
||
| 90 | $children = $this->getPrototypeChildren($node); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!$children) { |
||
| 94 | if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) { |
||
| 95 | $default = ''; |
||
| 96 | } elseif (!is_array($example)) { |
||
| 97 | $default = '[]'; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } elseif ($node instanceof EnumNode) { |
||
| 101 | $comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues())); |
||
| 102 | $default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~'; |
||
| 103 | } else { |
||
| 104 | $default = '~'; |
||
| 105 | |||
| 106 | if ($node->hasDefaultValue()) { |
||
| 107 | $default = $node->getDefaultValue(); |
||
| 108 | |||
| 109 | if (is_array($default)) { |
||
| 110 | if (count($defaultArray = $node->getDefaultValue())) { |
||
| 111 | $default = ''; |
||
| 112 | } elseif (!is_array($example)) { |
||
| 113 | $default = '[]'; |
||
| 114 | } |
||
| 115 | } else { |
||
| 116 | $default = Inline::dump($default); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // required? |
||
| 122 | if ($node->isRequired()) { |
||
| 123 | $comments[] = 'Required'; |
||
| 124 | } |
||
| 125 | |||
| 126 | // example |
||
| 127 | if ($example && !is_array($example)) { |
||
| 128 | $comments[] = 'Example: '.$example; |
||
| 129 | } |
||
| 130 | |||
| 131 | $default = (string) $default != '' ? ' '.$default : ''; |
||
| 132 | $comments = count($comments) ? '# '.implode(', ', $comments) : ''; |
||
| 133 | |||
| 134 | $key = $prototypedArray ? '-' : $node->getName().':'; |
||
| 135 | $text = rtrim(sprintf('%-21s%s %s', $key, $default, $comments), ' '); |
||
| 136 | |||
| 137 | if ($info = $node->getInfo()) { |
||
| 138 | $this->writeLine(''); |
||
| 139 | // indenting multi-line info |
||
| 140 | $info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info); |
||
| 141 | $this->writeLine('# '.$info, $depth * 4); |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->writeLine($text, $depth * 4); |
||
| 145 | |||
| 146 | // output defaults |
||
| 147 | if ($defaultArray) { |
||
| 148 | $this->writeLine(''); |
||
| 149 | |||
| 150 | $message = count($defaultArray) > 1 ? 'Defaults' : 'Default'; |
||
| 151 | |||
| 152 | $this->writeLine('# '.$message.':', $depth * 4 + 4); |
||
| 153 | |||
| 154 | $this->writeArray($defaultArray, $depth + 1); |
||
| 155 | } |
||
| 156 | |||
| 157 | if (is_array($example)) { |
||
| 158 | $this->writeLine(''); |
||
| 159 | |||
| 160 | $message = count($example) > 1 ? 'Examples' : 'Example'; |
||
| 161 | |||
| 162 | $this->writeLine('# '.$message.':', $depth * 4 + 4); |
||
| 163 | |||
| 164 | $this->writeArray($example, $depth + 1); |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($children) { |
||
| 168 | foreach ($children as $childNode) { |
||
| 169 | $this->writeNode($childNode, $depth + 1, $node instanceof PrototypedArrayNode && !$node->getKeyAttribute()); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 251 |