Completed
Push — 8.x-2.x ( 8af97d...4c7eae )
by Frédéric G.
03:11
created

mongodb_install.php ➔ mongodb_requirements()   C

Complexity

Conditions 9
Paths 28

Size

Total Lines 84
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 57
c 1
b 0
f 0
nc 28
nop 0
dl 0
loc 84
rs 5.4349

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * @file
5
 * Install file for the MongoDB module.
6
 */
7
8
use Drupal\Core\Site\Settings;
9
10
/**
11
 * Implements hook_requirements().
12
 */
13
function mongodb_requirements() {
14
  $extension_name = 'mongodb';
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
15
  $minimum_version = '1.1.7';
16
17
  $ret['mongodb'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
18
    'title' => t('Mongodb'),
19
    'severity' => REQUIREMENT_OK,
20
  ];
21
  $description = [];
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
65
  foreach ($databases as $database => list($client, $name)) {
0 ignored issues
show
introduced by
Variable $client is undefined.
Loading history...
introduced by
Variable $name is undefined.
Loading history...
66
    if (empty($client_aliases[$client])) {
0 ignored issues
show
introduced by
Variable $client is undefined.
Loading history...
67
      $warnings[] = t('Database "@db" references undefined client "@client".', [
68
        '@db' => $database,
69
        '@client' => $client,
0 ignored issues
show
introduced by
Variable $client is undefined.
Loading history...
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
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
97