Completed
Push — 17-watchdog-capped_tracker ( ded5c8 )
by Frédéric G.
02:40
created

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

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 34
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 41
rs 8.8571

1 Method

Rating   Name   Duplication   Size   Complexity  
A modules/mongodb_watchdog/mongodb_watchdog.module::mongodb_watchdog_event_load() 0 5 2
1
<?php
2
3
/**
4
 * @file
5
 * Fires watchdog messages to mongodb.
6
 */
7
8
/* ==== Everything below this line is broken ================================ */
9
10
/**
11
 * Load a MongoDB watchdog event.
12
 *
13
 * @param string $id
14
 *   The event id.
15
 *
16
 * @return object|false
17
 *   A loaded event.
18
 */
19
function mongodb_watchdog_event_load($id) {
20
  $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...
21
    ->findOne(array('_id' => $id));
22
  return $result ? $result : FALSE;
23
}
24
25
/**
26
 * Implements hook_watchdog().
27
 *
28
 * Refer to issue #1355808 regarding filtering.
29
 *
30
 * @link http://drupal.org/node/1355808 @endlink
31
 */
32
function mongodb_watchdog_watchdog(array $log_entry) {
33
  $watchdog_limit = variable_get('watchdog_limit', WATCHDOG_DEBUG);
34
  if (isset($log_entry['severity']) && $log_entry['severity'] > $watchdog_limit) {
35
    return;
36
  }
37
38
  static $checked_ids = array();
39
40
  // Find the function that generated this error.
41
  $log_entry = (array) $log_entry;
42
  _mongodb_watchdog_enhance_log_entry($log_entry, debug_backtrace());
43
  $account = $log_entry['user'];
44
  // Special handling for core bug #904994:
45
  if (!isset($log_entry['variables'])) {
46
    $special_messages = array(
47
      'page not found' => 'Page not found: @param',
48
      'access denied'  => 'Access denied: @param',
49
    );
50
    $type = $log_entry['type'];
51
    $log_entry['variables'] = array('@param' => $log_entry['message']);
52
    $log_entry['message'] = isset($special_messages[$type])
53
      ? $special_messages[$log_entry['type']]
54
      : '@param';
55
  }
56
57
  $event = array(
58
    'variables' => $log_entry['variables'],
59
    'timestamp' => $log_entry['timestamp'],
60
    'user' => array(
61
      'name' => isset($account->name) ? $account->name : variable_get('anonymous', t('Anonymous')),
62
      'uid' => $log_entry['uid'],
63
    ),
64
    'ip' => $log_entry['ip'],
65
    'request_uri' => $log_entry['request_uri'],
66
    'referer' => $log_entry['referer'],
67
    'link' => $log_entry['link'],
68
  );
69
  unset($log_entry['variables'], $log_entry['user'], $log_entry['ip'], $log_entry['request_uri'], $log_entry['referer'], $log_entry['link']);
70
71
  $newobj = array(
72
    '$set' => $log_entry,
73
    '$inc' => array('count' => 1),
74
  );
75
  $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'));
76
  $id = md5($log_entry['function'] . ':' . $log_entry['line'] . ':' . $log_entry['severity'] . ':' . $log_entry['type'] . ':' . $log_entry['message']);
77
  if (!isset($checked_ids[$id])) {
78
    $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...
79
  }
80
  $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...
81
  $collection = $collection->db->selectCollection('watchdog_event_' . $id);
82
  if (empty($checked_ids[$id])) {
83
    $max = variable_get('mongodb_watchdog_items', 10000);
84
    $command = array(
85
      'create' => $collection->getName(),
86
      'capped' => TRUE,
87
      'size' => $max * 1000,
88
      "max" => $max,
89
    );
90
    $collection->db->command($command);
91
    $checked_ids[$id] = TRUE;
92
  }
93
  $collection->insert($event, mongodb_default_write_options(FALSE));
94
}
95