Conditions | 55 |
Paths | 8820 |
Total Lines | 116 |
Code Lines | 89 |
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 |
||
123 | protected static function normalizeElementContent($element, $whitespace_preserve) { |
||
124 | # |
||
125 | # Normalize content of HTML DOM $element. The $whitespace_preserve |
||
126 | # argument indicates that whitespace is significant and shouldn't be |
||
127 | # normalized; it should be used for the content of certain elements like |
||
128 | # <pre> or <script>. |
||
129 | # |
||
130 | $node_list = $element->childNodes; |
||
131 | switch (strtolower($element->nodeName)) { |
||
132 | case 'body': |
||
133 | case 'div': |
||
134 | case 'blockquote': |
||
135 | case 'ul': |
||
136 | case 'ol': |
||
137 | case 'dl': |
||
138 | case 'h1': |
||
139 | case 'h2': |
||
140 | case 'h3': |
||
141 | case 'h4': |
||
142 | case 'h5': |
||
143 | case 'h6': |
||
144 | $whitespace = "\n\n"; |
||
145 | break; |
||
146 | |||
147 | case 'table': |
||
148 | $whitespace = "\n"; |
||
149 | break; |
||
150 | |||
151 | case 'pre': |
||
152 | case 'script': |
||
153 | case 'style': |
||
154 | case 'title': |
||
155 | $whitespace_preserve = true; |
||
156 | $whitespace = ""; |
||
157 | break; |
||
158 | |||
159 | default: |
||
160 | $whitespace = ""; |
||
161 | break; |
||
162 | } |
||
163 | foreach ($node_list as $node) { |
||
164 | switch ($node->nodeType) { |
||
165 | case XML_ELEMENT_NODE: |
||
166 | static::normalizeElementContent($node, $whitespace_preserve); |
||
167 | static::normalizeElementAttributes($node); |
||
168 | |||
169 | switch (strtolower($node->nodeName)) { |
||
170 | case 'p': |
||
171 | case 'div': |
||
172 | case 'hr': |
||
173 | case 'blockquote': |
||
174 | case 'ul': |
||
175 | case 'ol': |
||
176 | case 'dl': |
||
177 | case 'li': |
||
178 | case 'address': |
||
179 | case 'table': |
||
180 | case 'dd': |
||
181 | case 'pre': |
||
182 | case 'h1': |
||
183 | case 'h2': |
||
184 | case 'h3': |
||
185 | case 'h4': |
||
186 | case 'h5': |
||
187 | case 'h6': |
||
188 | $whitespace = "\n\n"; |
||
189 | break; |
||
190 | |||
191 | case 'tr': |
||
192 | case 'td': |
||
193 | case 'dt': |
||
194 | $whitespace = "\n"; |
||
195 | break; |
||
196 | |||
197 | default: |
||
198 | $whitespace = ""; |
||
199 | break; |
||
200 | } |
||
201 | |||
202 | if (($whitespace === "\n\n" || $whitespace === "\n") && |
||
203 | $node->nextSibling && |
||
204 | $node->nextSibling->nodeType != XML_TEXT_NODE) { |
||
205 | $element->insertBefore(new DOMText($whitespace), $node->nextSibling); |
||
206 | } |
||
207 | break; |
||
208 | |||
209 | case XML_TEXT_NODE: |
||
210 | if (!$whitespace_preserve) { |
||
211 | if (trim($node->data) === "") { |
||
212 | $node->data = $whitespace; |
||
213 | } |
||
214 | else { |
||
215 | $node->data = preg_replace('{\s+}', ' ', $node->data); |
||
216 | } |
||
217 | } |
||
218 | break; |
||
219 | } |
||
220 | } |
||
221 | if (!$whitespace_preserve && |
||
222 | ($whitespace === "\n\n" || $whitespace === "\n")) { |
||
223 | if ($element->firstChild) { |
||
224 | if ($element->firstChild->nodeType == XML_TEXT_NODE) { |
||
225 | $element->firstChild->data = |
||
|
|||
226 | preg_replace('{^\s+}', "\n", $element->firstChild->data); |
||
227 | } |
||
228 | else { |
||
229 | $element->insertBefore(new DOMText("\n"), $element->firstChild); |
||
230 | } |
||
231 | } |
||
232 | if ($element->lastChild) { |
||
233 | if ($element->lastChild->nodeType == XML_TEXT_NODE) { |
||
234 | $element->lastChild->data = |
||
235 | preg_replace('{\s+$}', "\n", $element->lastChild->data); |
||
236 | } |
||
237 | else { |
||
238 | $element->insertBefore(new DOMText("\n"), null); |
||
239 | } |
||
268 |