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

modules/mongodb_watchdog/mongodb_watchdog.module::mongodb_watchdog_watchdog()   B

Complexity

Conditions 8
Paths 25

Size

Total Lines 55
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 41
c 3
b 0
f 0
nc 25
nop 1
dl 0
loc 55
rs 7.4033

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
 * Fires watchdog messages to mongodb.
6
 */
7
8
// ==== Everything below this line is broken ===================================
9
10
/**
11
 * Implement hook_menu().
12
 */
13
function mongodb_watchdog_menu() {
14
  $items['admin/reports/mongodb'] = 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...
15
    'title' => 'Recent log entries in MongoDB',
16
    'description' => 'View events that have recently been logged in MongoDB.',
17
    'page callback' => 'mongodb_watchdog_overview',
18
    'access arguments' => array('access site reports'),
19
    'file'  => 'mongodb_watchdog.admin.inc',
20
  );
21
  $items['admin/reports/mongodb/%mongodb_watchdog_event'] = array(
22
    'title' => 'MongoDB log details',
23
    'page callback' => 'mongodb_watchdog_event',
24
    'page arguments' => array(3),
25
    'access arguments' => array('access site reports'),
26
    'type' => MENU_CALLBACK,
27
    'file' => 'mongodb_watchdog.admin.inc',
28
  );
29
  $items['admin/reports/mongodb/list'] = array(
30
    'type' => MENU_DEFAULT_LOCAL_TASK,
31
    'title' => 'Overview',
32
  );
33
  $items['admin/reports/mongodb/access-denied'] = array(
34
    'type' => MENU_LOCAL_TASK,
35
    'title' => "Top 'access denied' errors in MongoDB",
36
    'description' => "View 'access denied' errors (403s).",
37
    'page callback' => 'mongodb_watchdog_page_top',
38
    'page arguments' => array('access denied'),
39
    'access arguments' => array('access site reports'),
40
    'file' => 'mongodb_watchdog.admin.inc',
41
  );
42
  $items['admin/reports/mongodb/page-not-found'] = array(
43
    'type' => MENU_LOCAL_TASK,
44
    'title' => "Top 'page not found' errors in MongoDB",
45
    'description' => "View 'page not found' errors (404s).",
46
    'page callback' => 'mongodb_watchdog_page_top',
47
    'page arguments' => array('page not found'),
48
    'access arguments' => array('access site reports'),
49
    'file' => 'mongodb_watchdog.admin.inc',
50
  );
51
52
  return $items;
53
}
54
55
/**
56
 * Load a MongoDB watchdog event.
57
 *
58
 * @param string $id
59
 *
60
 * @return object|FALSE
61
 */
62
function mongodb_watchdog_event_load($id) {
63
  $result = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'))
0 ignored issues
show
Bug introduced by
The method findOne does only exist in MongoCollection, but not in MongoDebugCollection and MongoDummy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
64
    ->findOne(array('_id' => $id));
65
  return $result ? $result : FALSE;
66
}
67
68
/**
69
 * Implements hook_watchdog().
70
 *
71
 * Refer to issue #1355808 regarding filtering.
72
 *
73
 * @link http://drupal.org/node/1355808 @endlink
74
 */
75
function mongodb_watchdog_watchdog(array $log_entry) {
76
  $watchdog_limit = variable_get('watchdog_limit', WATCHDOG_DEBUG);
77
  if (isset($log_entry['severity']) && $log_entry['severity'] > $watchdog_limit) {
78
    return;
79
  }
80
81
  static $checked_ids = array();
82
83
  // Find the function that generated this error.
84
  $log_entry = (array) $log_entry;
85
  _mongodb_watchdog_enhance_log_entry($log_entry, debug_backtrace());
86
  $account = $log_entry['user'];
87
  // Special handling for core bug #904994:
88
  if (!isset($log_entry['variables'])) {
89
    $special_messages = array(
90
      'page not found' => 'Page not found: @param',
91
      'access denied'  => 'Access denied: @param',
92
    );
93
    $type = $log_entry['type'];
94
    $log_entry['variables'] = array('@param' => $log_entry['message']);
95
    $log_entry['message'] = isset($special_messages[$type])
96
      ? $special_messages[$log_entry['type']]
97
      : '@param';
98
  }
99
100
  $event = array(
101
    'variables' => $log_entry['variables'],
102
    'timestamp' => $log_entry['timestamp'],
103
    'user' => array('name' => isset($account->name) ? $account->name : variable_get('anonymous', t('Anonymous')), 'uid' => $log_entry['uid']),
104
    'ip' => $log_entry['ip'],
105
    'request_uri' => $log_entry['request_uri'],
106
    'referer' => $log_entry['referer'],
107
    'link' => $log_entry['link'],
108
  );
109
  unset($log_entry['variables'], $log_entry['user'], $log_entry['ip'], $log_entry['request_uri'], $log_entry['referer'], $log_entry['link']);
110
111
  $newobj = array(
112
    '$set' => $log_entry,
113
    '$inc' => array('count' => 1),
114
  );
115
  $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'));
116
  $id = md5($log_entry['function'] . ':' . $log_entry['line'] . ':' . $log_entry['severity'] . ':' . $log_entry['type'] . ':' . $log_entry['message']);
117
  if (!isset($checked_ids[$id])) {
118
    $checked_ids[$id] = $collection->findOne(array('_id' => $id), array('_id' => 1));
0 ignored issues
show
Bug introduced by
The method findOne does only exist in MongoCollection, but not in MongoDebugCollection and MongoDummy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
119
  }
120
  $collection->update(array('_id' => $id), $newobj, array('upsert' => TRUE) + mongodb_default_write_options(FALSE));
0 ignored issues
show
Bug introduced by
The method update does only exist in MongoCollection, but not in MongoDebugCollection and MongoDummy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
121
  $collection = $collection->db->selectCollection('watchdog_event_' . $id);
122
  if (empty($checked_ids[$id])) {
123
    $max = variable_get('mongodb_watchdog_items', 10000);
124
    $command = array('create' => $collection->getName(), 'capped' => TRUE, 'size' => $max * 1000, "max" => $max);
125
    $collection->db->command($command);
126
    $checked_ids[$id] = TRUE;
127
  }
128
  $collection->insert($event, mongodb_default_write_options(FALSE));
129
}
130
131
/**
132
 * Fill in the log_entry function, file, and line.
133
 *
134
 * @param array $log_entry
135
 * @param array $backtrace
136
 *
137
 * @return void
138
 */
139
function _mongodb_watchdog_enhance_log_entry(&$log_entry, $backtrace) {
140
  // Create list of functions to ignore in backtrace.
141
  static $ignore = array(
142
    'mongodb_watchdog_watchdog' => 1,
143
    'call_user_func_array' => 1,
144
    'module_invoke' => 1,
145
    'watchdog' => 1,
146
    '_drupal_log_error' => 1,
147
    '_drupal_error_handler' => 1,
148
    '_drupal_error_handler_real' => 1,
149
    'theme_render_template' => 1,
150
  );
151
152
  foreach ($backtrace as $bt) {
153
    if (isset($bt['function'])) {
154
      if (isset($bt['line']) && !isset($ignore[$bt['function']])) {
155
        if (isset($bt['file'])) {
156
          $log_entry['file'] = $bt['file'];
157
        }
158
        $log_entry['function'] = $bt['function'];
159
        $log_entry['line'] = $bt['line'];
160
        break;
161
      }
162
      elseif ($bt['function'] == '_drupal_exception_handler') {
163
        $e = $bt['args'][0];
164
        _mongodb_watchdog_enhance_log_entry($log_entry, $e->getTrace());
165
      }
166
    }
167
  }
168
}
169