| Conditions | 11 |
| Paths | 15 |
| Total Lines | 33 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 12 | function one_preprocess_html(&$variables) { |
||
| 13 | /* @var Drupal\Core\Language\LanguageInterface */ |
||
| 14 | $language = \Drupal::languageManager()->getCurrentLanguage(); |
||
| 15 | $site_language = $language->getId(); |
||
| 16 | $request = \Drupal::request(); |
||
| 17 | |||
| 18 | // Add language body class. |
||
| 19 | $variables['attributes']['class'][] = 'lang-' . $site_language; |
||
| 20 | |||
| 21 | // Classes for body element. Allows advanced theming based on context |
||
| 22 | $is_front_page = \Drupal::service('path.matcher')->isFrontPage(); |
||
| 23 | if (!$is_front_page) { |
||
| 24 | $path = trim($request->getRequestUri(), '/'); |
||
| 25 | // Add unique class for each website section. |
||
| 26 | $arg = explode('/', $path); |
||
| 27 | $section = $arg[0]; |
||
| 28 | if ($arg[0] == 'node' && isset($arg[1])) { |
||
| 29 | if ($arg[1] == 'add') { |
||
| 30 | $section = 'node-add'; |
||
| 31 | } |
||
| 32 | elseif (isset($arg[2]) && is_numeric($arg[1]) && ($arg[2] == 'edit' || $arg[2] == 'delete')) { |
||
| 33 | $section = 'node-' . $arg[2]; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | $variables['attributes']['class'][] = \Drupal\Component\Utility\Html::getClass('section-' . $section); |
||
| 37 | } |
||
| 38 | |||
| 39 | // Store the menu item since it has some useful information. |
||
| 40 | if ($request->attributes->get('view_id')) { |
||
| 41 | $variables['attributes']['class'][] = 'views-page'; |
||
| 42 | } |
||
| 43 | elseif ($request->attributes->get('panel')) { |
||
| 44 | $variables['attributes']['class'][] = 'panels-page'; |
||
| 45 | } |
||
| 47 |