Conditions | 26 |
Paths | 72 |
Total Lines | 108 |
Code Lines | 68 |
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 |
||
115 | protected function drupalProcessAjaxResponse($content, array $ajax_response, array $ajax_settings, array $drupal_settings) { |
||
116 | // ajax.js applies some defaults to the settings object, so do the same |
||
117 | // for what's used by this function. |
||
118 | $ajax_settings += array( |
||
119 | 'method' => 'replaceWith', |
||
120 | ); |
||
121 | // DOM can load HTML soup. But, HTML soup can throw warnings, suppress |
||
122 | // them. |
||
123 | $dom = new \DOMDocument(); |
||
124 | @$dom->loadHTML($content); |
||
125 | // XPath allows for finding wrapper nodes better than DOM does. |
||
126 | $xpath = new \DOMXPath($dom); |
||
127 | foreach ($ajax_response as $command) { |
||
128 | // Error messages might be not commands. |
||
129 | if (!is_array($command)) { |
||
130 | continue; |
||
131 | } |
||
132 | switch ($command['command']) { |
||
133 | case 'settings': |
||
134 | $drupal_settings = NestedArray::mergeDeepArray([$drupal_settings, $command['settings']], TRUE); |
||
135 | break; |
||
136 | |||
137 | case 'insert': |
||
138 | $wrapperNode = NULL; |
||
139 | // When a command specifies a specific selector, use it. |
||
140 | if (!empty($command['selector']) && strpos($command['selector'], '#') === 0) { |
||
141 | $wrapperNode = $xpath->query('//*[@id="' . substr($command['selector'], 1) . '"]')->item(0); |
||
142 | } |
||
143 | // When a command doesn't specify a selector, use the |
||
144 | // #ajax['wrapper'] which is always an HTML ID. |
||
145 | elseif (!empty($ajax_settings['wrapper'])) { |
||
146 | $wrapperNode = $xpath->query('//*[@id="' . $ajax_settings['wrapper'] . '"]')->item(0); |
||
147 | } |
||
148 | // @todo Ajax commands can target any jQuery selector, but these are |
||
149 | // hard to fully emulate with XPath. For now, just handle 'head' |
||
150 | // and 'body', since these are used by |
||
151 | // \Drupal\Core\Ajax\AjaxResponse::ajaxRender(). |
||
152 | elseif (in_array($command['selector'], array('head', 'body'))) { |
||
153 | $wrapperNode = $xpath->query('//' . $command['selector'])->item(0); |
||
154 | } |
||
155 | if ($wrapperNode) { |
||
156 | // ajax.js adds an enclosing DIV to work around a Safari bug. |
||
157 | $newDom = new \DOMDocument(); |
||
158 | // DOM can load HTML soup. But, HTML soup can throw warnings, |
||
159 | // suppress them. |
||
160 | @$newDom->loadHTML('<div>' . $command['data'] . '</div>'); |
||
161 | // Suppress warnings thrown when duplicate HTML IDs are encountered. |
||
162 | // This probably means we are replacing an element with the same ID. |
||
163 | $newNode = @$dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE); |
||
164 | $method = isset($command['method']) ? $command['method'] : $ajax_settings['method']; |
||
165 | // The "method" is a jQuery DOM manipulation function. Emulate |
||
166 | // each one using PHP's DOMNode API. |
||
167 | switch ($method) { |
||
168 | case 'replaceWith': |
||
169 | $wrapperNode->parentNode->replaceChild($newNode, $wrapperNode); |
||
170 | break; |
||
171 | case 'append': |
||
172 | $wrapperNode->appendChild($newNode); |
||
173 | break; |
||
174 | case 'prepend': |
||
175 | // If no firstChild, insertBefore() falls back to |
||
176 | // appendChild(). |
||
177 | $wrapperNode->insertBefore($newNode, $wrapperNode->firstChild); |
||
178 | break; |
||
179 | case 'before': |
||
180 | $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode); |
||
181 | break; |
||
182 | case 'after': |
||
183 | // If no nextSibling, insertBefore() falls back to |
||
184 | // appendChild(). |
||
185 | $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode->nextSibling); |
||
186 | break; |
||
187 | case 'html': |
||
188 | foreach ($wrapperNode->childNodes as $childNode) { |
||
189 | $wrapperNode->removeChild($childNode); |
||
190 | } |
||
191 | $wrapperNode->appendChild($newNode); |
||
192 | break; |
||
193 | } |
||
194 | } |
||
195 | break; |
||
196 | |||
197 | // @todo Add suitable implementations for these commands in order to |
||
198 | // have full test coverage of what ajax.js can do. |
||
199 | case 'remove': |
||
200 | break; |
||
201 | case 'changed': |
||
202 | break; |
||
203 | case 'css': |
||
204 | break; |
||
205 | case 'data': |
||
206 | break; |
||
207 | case 'restripe': |
||
208 | break; |
||
209 | case 'add_css': |
||
210 | break; |
||
211 | case 'update_build_id': |
||
212 | $buildId = $xpath->query('//input[@name="form_build_id" and @value="' . $command['old'] . '"]')->item(0); |
||
213 | if ($buildId) { |
||
214 | $buildId->setAttribute('value', $command['new']); |
||
215 | } |
||
216 | break; |
||
217 | } |
||
218 | } |
||
219 | $content = $dom->saveHTML(); |
||
220 | $this->setRawContent($content); |
||
221 | $this->setDrupalSettings($drupal_settings); |
||
222 | } |
||
223 | |||
225 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.