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