Completed
Push — 2762893-mongodb_tests ( a0e627...86bd77 )
by Frédéric G.
03:18
created

MongoDebugCollection   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 50
rs 10
wmc 3
lcom 1
cbo 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains the main module connecting Drupal to MongoDB.
6
 */
7
8
use Drupal\Core\Routing\RouteMatchInterface;
9
10
/**
11
 * Implements hook_help().
12
 */
13
function mongodb_help($route_name, RouteMatchInterface $route_match) {
0 ignored issues
show
Unused Code introduced by
The parameter $route_match is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
  switch ($route_name) {
15
    case 'help.page.mongodb':
16
      return '<p>' . t('The Drupal <a href=":project">MongoDB</a> project implements a generic interface to the <a href=":mongo">MongoDB</a> database server.', [
17
        ':project' => 'http://drupal.org/project/mongodb',
18
        ':mongo' => 'http://www.mongodb.org/',
19
      ]);
20
  }
21
}
22
23
/* ==== Highly suspicious below this line =================================== */
24
25
/**
26
 * Implements hook_test_group_finished().
27
 *
28
 * @throws \InvalidArgumentException
29
 *   If the database cannot be selected.
30
 *
31
 * @throws \MongoDB\Driver\Exception\ConnectionException
32
 *   If the connection cannot be established.
33
 */
34
function mongodb_test_group_finished() {
35
  $aliases = variable_get('mongodb_connections', array());
36
  $aliases['default'] = TRUE;
37
  foreach (array_keys($aliases) as $alias) {
38
    $db = mongodb($alias);
39
    foreach ($db->listCollections() as $collection) {
40
      if (preg_match('/\.simpletest\d+/', $collection)) {
41
        $db->dropCollection($collection);
42
      }
43
    }
44
  }
45
}
46
47
/**
48
 * Return the next id in a sequence.
49
 *
50
 * @param string $name
51
 *   The name of the sequence.
52
 * @param int $existing_id
53
 *   An existing id.
54
 *
55
 * @return int
56
 *   The next id in the sequence.
57
 *
58
 * @throws \MongoConnectionException
59
 *   If the connection cannot be established.
60
 */
61
function mongodb_next_id($name, $existing_id = 0) {
62
  // Atomically get the next id in the sequence.
63
  $mongo = mongodb();
64
  $cmd = array(
65
    'findandmodify' => mongodb_collection_name('sequence'),
66
    'query' => array('_id' => $name),
67
    'update' => array('$inc' => array('value' => 1)),
68
    'new' => TRUE,
69
  );
70
  // It's very likely that this is not necessary as command returns an array
71
  // not an exception. The increment will, however, will fix the problem of
72
  // the sequence not existing. Still, better safe than sorry.
73
  try {
74
    $sequence = $mongo->command($cmd);
75
    $value = isset($sequence['value']['value']) ? $sequence['value']['value'] : 0;
76
  }
77
  catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
  }
79
  if (0 < $existing_id - $value + 1) {
80
    $cmd = array(
81
      'findandmodify' => mongodb_collection_name('sequence'),
82
      'query' => array('_id' => $name),
83
      'update' => array('$inc' => array('value' => $existing_id - $value + 1)),
0 ignored issues
show
Bug introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
84
      'upsert' => TRUE,
85
      'new' => TRUE,
86
    );
87
    $sequence = $mongo->command($cmd);
88
    $value = isset($sequence['value']['value']) ? $sequence['value']['value'] : 0;
89
  }
90
  return $value;
91
}
92
93
/**
94
 * Returns default options for MongoDB write operations.
95
 *
96
 * @param bool $safe
97
 *   Set it to FALSE for "fire and forget" write operation.
98
 *
99
 * @return array
100
 *   Default options for Mongo write operations.
101
 */
102
function mongodb_default_write_options($safe = TRUE) {
103
  if ($safe) {
104 View Code Duplication
    if (version_compare(phpversion('mongo'), '1.5.0') == -1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
      return array('safe' => TRUE);
106
    }
107
    else {
108
      return variable_get('mongodb_write_safe_options', array('w' => 1));
109
    }
110
  }
111 View Code Duplication
  else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    if (version_compare(phpversion('mongo'), '1.3.0') == -1) {
113
      return array();
114
    }
115
    else {
116
      return variable_get('mongodb_write_nonsafe_options', array('w' => 0));
117
    }
118
  }
119
}
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...
120