SentryLogger   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 3 1
A __construct() 0 13 2
A _write() 0 6 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require_once 'Zend/Log/Writer/Abstract.php';
4
5
/**
6
 * Publish SilverStripe errors and warnings to Sentry using the Raven client.
7
 */
8
class SentryLogger extends Zend_Log_Writer_Abstract {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
10
    private $sentry;
11
12
    private $logLevels = array(
13
        'NOTICE'    => Raven_Client::INFO,
14
        'WARN'      => Raven_Client::WARNING,
15
        'ERR'       => Raven_Client::ERROR
16
    );
17
18
    /**
19
     * @config
20
     */
21
    private static $sentry_dsn;
0 ignored issues
show
Unused Code introduced by
The property $sentry_dsn is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
24
    public static function factory($config) {
25
        return new SentryLogger();
26
    }
27
28
    public function __construct()
29
    {
30
        $DSN = Config::inst()->get('SentryLogger', 'sentry_dsn');
31
        $this->sentry = new Raven_Client($DSN);
32
        $this->sentry->setEnvironment(Director::get_environment_type());
33
        
34
        if(Member::currentUserID()) {
35
            $this->sentry->user_context(array(
36
                'email' => Member::currentUser()->Email,
37
                'id' => Member::currentUserID()
38
            ));    
39
        }
40
    }
41
42
    /**
43
     * Send the error.
44
     * 
45
     * @param array $event
46
     * @return void
47
    */
48
    public function _write($event) {
49
        $data['level'] = $this->logLevels[$event['priorityName']];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
50
        $data['timestamp'] = strtotime($event['timestamp']);
51
        $backtrace = SS_Backtrace::filter_backtrace(debug_backtrace(), array("SentryLogger->_write"));
52
        $this->sentry->captureMessage($event['message']['errstr'], array(), $data, $backtrace);
0 ignored issues
show
Documentation introduced by
$backtrace is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55
}
56