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

mongodb_drush.php ➔ mongodb_drush_help()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 9.4285
1
<?php
2
3
/**
4
 * @file
5
 * Provides drush integration for MongoDB.
6
 */
0 ignored issues
show
introduced by
There must be exactly one blank line after the file comment
Loading history...
7
/**
8
 * Implements hook_drush_command().
9
 */
10
function mongodb_drush_command() {
11
  $items['mongodb-find'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = 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...
12
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
13
    'description' => 'Execute a query against a collection.',
14
    'examples' => array(
15
      'drush mongodb-find "{}" logger' => 'Get the logger/watchdog entries.',
16
    ),
17
    'arguments' => array(
18
      'collection' => 'The collection name in the database',
19
      'alias' => 'The database alias',
20
      'selector' => 'A MongoDB find() selector in JSON format. Defaults to {}',
21
    ),
22
    'aliases' => ['mdbf', 'mdbq'],
23
  );
24
25
  $items['mongodb-settings'] = array(
26
    'description' => 'Print MongoDB settings using print_r().',
27
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
28
    'aliases' => ['mdbs'],
29
  );
30
31
  // TODO : to be ported to D8.
32
  /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
33
  $items['mongodb-connect'] = array(
34
    'description' => 'A string for connecting to the mongodb.',
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
35
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
36
//     'options' => $options,
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 2 spaces
Loading history...
37
    'arguments' => array(
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
38
       'alias' => 'The connection',
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 7
Loading history...
39
    ),
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
40
  );
41
42
  $items['mongodb-cli'] = array(
43
    'description' => "Open a mongodb command-line interface using Drupal's credentials.",
0 ignored issues
show
introduced by
Line exceeds 80 characters; contains 89 characters
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
44
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
45
//     'options' => $options,
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 2 spaces
Loading history...
46
    'examples' => array(
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
47
      '`drush mongodb-connect`' => 'Connect to the mongodb.',
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 6
Loading history...
48
    ),
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
49
    'arguments' => array(
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
50
       'alias' => 'The connection',
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 7
Loading history...
51
    ),
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
52
    'aliases' => array('mdbc'),
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 spaces, found 4
Loading history...
53
  );
54
55
  */
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 spaces, found 2
Loading history...
56
57
  return $items;
58
}
59
60
/**
61
 * Implementation of hook_drush_help().
0 ignored issues
show
introduced by
Format should be "* Implements hook_foo().", "* Implements hook_foo_BAR_ID_bar() for xyz_bar().",, "* Implements hook_foo_BAR_ID_bar() for xyz-bar.html.twig.", or "* Implements hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.".
Loading history...
62
 */
63
function mongodb_drush_help($section) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
64
  switch ($section) {
65
    case 'drush:mongodb-settings':
66
      return dt('Show MongoDB settings.');
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
67
    case 'drush:mongodb-find':
68
      return dt("Usage: drush [options] mongodb-find <query>...\n<query> is a single JSON selector.");
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
69
//    case 'drush:mongodb-connect':
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
introduced by
4 spaces found before inline comment; expected "// case 'drush:mongodb-connect':" but found "// case 'drush:mongodb-connect':"
Loading history...
introduced by
Inline comments must start with a capital letter
Loading history...
70
//      return dt('A string which connects to the current database.');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 4 spaces
Loading history...
71
//    case 'drush:mongodb-cli':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
72
//      return dt('Quickly enter the mongodb shell.');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 4 spaces
Loading history...
73
//    // TODO
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
74
//    case 'drush:mongodb-dump':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
75
//      return dt('Prints the whole database to STDOUT or save to a file.');
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 4 spaces
Loading history...
76
  }
77
}
78
79
/**
80
 * Returns the basic shell command string.
81
 */
82
function _drush_mongodb_connect($alias) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
83
  $connections = variable_get('mongodb_connections', array());
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...
84
  $connections += array('default' => array('host' => 'localhost', 'db' => 'drupal'));
85
  if (!isset($connections[$alias])) {
86
    $alias = 'default';
87
  }
88
  $connection = $connections[$alias];
89
  $host = $connection['host'];
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...
90
  $db = $connection['db'];
1 ignored issue
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 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...
91
92
  $query = $host;
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...
93
  $query .= "/$db";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $db instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
94
95
  $command = "mongo ${query}";
96
  return $command;
97
}
98
99
/**
100
 * Drush callback; Start the mongodb shell.
101
 */
102
function drush_mongodb_cli($alias = 'default') {
103
  $command = _drush_mongodb_connect($alias);
104
  drush_print(proc_close(proc_open(escapeshellcmd($command), array(0 => STDIN, 1 => STDOUT, 2 => STDERR), $pipes)));
0 ignored issues
show
introduced by
If the line declaring an array spans longer than 80 characters, each element should be broken into its own line
Loading history...
105
}
106
107
/**
108
 * Drush callback; Return the connect string.
109
 *
110
 * @param string $alias
111
 *   The alias of a database to connect to.
112
 */
113
function drush_mongodb_connect($alias = 'default') {
114
  $command = _drush_mongodb_connect($alias);
115
  drush_print($command);
116
}
117
118
/**
119
 * Drush callback; Execute a find against a Mongodb database.
120
 *
121
 * @param string $collection
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
122
 * @param string $selector
123
 *   JSON
0 ignored issues
show
introduced by
Parameter comment must end with a full stop
Loading history...
124
 * @param string $alias
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
125
 */
126
function drush_mongodb_find($collection, $selector = '{}', $alias = 'default') {
127
  /** @var \MongoDB\Database $db */
128
  $db = \Drupal::service('mongodb.database_factory')->get($alias);
1 ignored issue
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
129
  $docs = $db->selectCollection($collection)
130
    ->find(json_decode($selector), [
131
      'typeMap' => [
132
        'root' => 'array',
133
        'document' => 'array',
134
        'array' => 'array'
135
      ]])
136
    ->toArray();
137
  drush_print_r($docs);
138
}
139
140
/**
141
 * Drush callback; Print the config of the mongodb.
142
 */
143
function drush_mongodb_settings() {
144
  $settings = \Drupal::service('settings')->get('mongodb');
145
  drush_print_r($settings);
146
}
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...
147