| Conditions | 9 |
| Paths | 28 |
| Total Lines | 84 |
| Code Lines | 57 |
| 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 |
||
| 13 | function mongodb_requirements() { |
||
| 14 | $extension_name = 'mongodb'; |
||
|
1 ignored issue
–
show
|
|||
| 15 | $minimum_version = '1.1.7'; |
||
| 16 | |||
| 17 | $ret['mongodb'] = [ |
||
| 18 | 'title' => t('Mongodb'), |
||
| 19 | 'severity' => REQUIREMENT_OK, |
||
| 20 | ]; |
||
| 21 | $description = []; |
||
|
1 ignored issue
–
show
|
|||
| 22 | |||
| 23 | if (!extension_loaded($extension_name)) { |
||
| 24 | $ret['mongodb'] += array( |
||
| 25 | 'value' => t('Extension not loaded'), |
||
| 26 | 'description' => t('Mongodb requires the non-legacy PHP MongoDB extension (@name) to be installed.', [ |
||
| 27 | '@name' => $extension_name, |
||
| 28 | ]), |
||
| 29 | 'severity' => REQUIREMENT_ERROR, |
||
| 30 | ); |
||
| 31 | return $ret; |
||
| 32 | } |
||
| 33 | |||
| 34 | $extension_version = phpversion($extension_name); |
||
| 35 | $version_status = version_compare($extension_version, $minimum_version); |
||
|
1 ignored issue
–
show
|
|||
| 36 | |||
| 37 | if ($version_status < 0) { |
||
| 38 | $ret['mongodb'] += [ |
||
| 39 | 'severity' => REQUIREMENT_ERROR, |
||
| 40 | 'description' => t('Module needs extension @name @minimum_version or later, found @version.', [ |
||
| 41 | '@name' => $extension_name, |
||
| 42 | '@minimum_version' => $minimum_version, |
||
| 43 | '@version' => $extension_version, |
||
| 44 | ]), |
||
| 45 | ]; |
||
| 46 | return $ret; |
||
| 47 | } |
||
| 48 | $description[] = t('Extension version @version found.', ['@version' => $extension_version]); |
||
| 49 | |||
| 50 | $settings = Settings::get('mongodb'); |
||
|
1 ignored issue
–
show
|
|||
| 51 | $databases = isset($settings['databases']) ? $settings['databases'] : []; |
||
| 52 | if (empty($databases)) { |
||
| 53 | $ret['mongodb'] += [ |
||
| 54 | 'severity' => REQUIREMENT_WARNING, |
||
| 55 | 'value' => t('No database aliases found in settings. Did you actually configure your settings ?'), |
||
| 56 | 'description' => [ |
||
| 57 | '#theme' => 'item_list', |
||
| 58 | '#items' => $description, |
||
| 59 | ]]; |
||
| 60 | return $ret; |
||
| 61 | } |
||
| 62 | |||
| 63 | $client_aliases = isset($settings['clients']) ? $settings['clients'] : []; |
||
| 64 | $warnings = []; |
||
|
1 ignored issue
–
show
|
|||
| 65 | foreach ($databases as $database => list($client, $name)) { |
||
| 66 | if (empty($client_aliases[$client])) { |
||
| 67 | $warnings[] = t('Database "@db" references undefined client "@client".', [ |
||
| 68 | '@db' => $database, |
||
| 69 | '@client' => $client, |
||
| 70 | ]); |
||
| 71 | } |
||
| 72 | else { |
||
| 73 | $warnings = [t('Databases and clients are consistent.')]; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $description = [ |
||
| 78 | '#theme' => 'item_list', |
||
| 79 | '#items' => array_merge($description, $warnings), |
||
| 80 | ]; |
||
| 81 | |||
| 82 | if (!empty($warnings)) { |
||
| 83 | $ret['mongodb'] += [ |
||
| 84 | 'value' => t('Inconsistent database/client settings.'), |
||
| 85 | 'severity' => REQUIREMENT_ERROR, |
||
| 86 | 'description' => $description, |
||
| 87 | ]; |
||
| 88 | return $ret; |
||
| 89 | } |
||
| 90 | |||
| 91 | $ret['mongodb'] += [ |
||
| 92 | 'value' => t('Valid configuration'), |
||
| 93 | 'description' => $description, |
||
| 94 | ]; |
||
| 95 | return $ret; |
||
| 96 | } |
||
| 97 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.