Completed
Push — master ( af91e2...d4dfb1 )
by jelmer
02:53
created

Error::getLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Monolog;
4
5
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface;
6
7
/**
8
 * A custom error class to collect all the data we need.
9
 *
10
 * @author Jelmer Prins <[email protected]>
11
 */
12
class Error implements ErrorInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $message;
18
19
    /**
20
     * @var int
21
     */
22
    private $line;
23
24
    /**
25
     * @var string
26
     */
27
    private $file;
28
29
    /**
30
     * @var array
31
     */
32
    private $trace;
33
34
    /**
35
     * @var array
36
     */
37
    private $parameters = array();
38
39
    /**
40
     * Create a new error wrapping the given error context info.
41
     *
42
     * @param string $message
43
     * @param int $line
44
     * @param string $file
45
     * @param array|null $trace
46
     * @param array|null $parameters
47
     */
48 11
    public function __construct($message, $line, $file, array $trace = null, array $parameters = null)
49
    {
50 11
        $this->setMessage($message);
51 11
        $this->setLine($line);
52 11
        $this->setFile($file);
53 11
        $this->setTrace($trace);
54 11
        $this->setParameters($parameters);
55 11
    }
56
57
    /**
58
     * @param string $file
59
     */
60 11
    private function setFile($file)
61
    {
62 11
        $this->file = $file;
63 11
    }
64
65
    /**
66
     * @return string
67
     */
68 10
    public function getFile()
69
    {
70 10
        return $this->file;
71
    }
72
73
    /**
74
     * @param int $line
75
     */
76 11
    private function setLine($line)
77
    {
78 11
        $this->line = $line;
79 11
    }
80
81
    /**
82
     * @return int
83
     */
84 10
    public function getLine()
85
    {
86 10
        return $this->line;
87
    }
88
89
    /**
90
     * @param string $message
91
     */
92 11
    private function setMessage($message)
93
    {
94 11
        $this->message = $message;
95 11
    }
96
97
    /**
98
     * @return string
99
     */
100 1
    public function getMessage()
101
    {
102 1
        return $this->message;
103
    }
104
105
    /**
106
     * @param array $parameters
107
     */
108 11
    private function setParameters(array $parameters = null)
4 ignored issues
show
Coding Style introduced by
setParameters uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setParameters uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setParameters uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
setParameters uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
109
    {
110 11
        $this->parameters = $parameters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameters can be null. However, the property $parameters is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
111
112
        // make sure we have at least the POST, GET, COOKIE and SESSION data
113 11
        if (!isset($this->parameters['_POST']) && !empty($_POST)) {
114 1
            $this->parameters['_POST'] = $_POST;
115 1
        }
116 11
        if (!isset($this->parameters['_GET']) && !empty($_GET)) {
117 1
            $this->parameters['_GET'] = $_GET;
118 1
        }
119 11
        if (!isset($this->parameters['_COOKIE']) && !empty($_COOKIE)) {
120 1
            $this->parameters['_COOKIE'] = $_COOKIE;
121 1
        }
122 11
        if (!isset($this->parameters['_SESSION']) && !empty($_SESSION)) {
123 1
            $this->parameters['_SESSION'] = $_SESSION;
124 1
        }
125 11
    }
126
127
    /**
128
     * @return array
129
     */
130 5
    public function getParameters()
131
    {
132 5
        return $this->parameters;
133
    }
134
135
    /**
136
     * @param array $trace
137
     */
138 11
    private function setTrace(array $trace = null)
139
    {
140 11
        if ($trace === null) {
141
            $trace = array(
142
                array(
143 10
                    'line' => $this->getLine(),
144 10
                    'file' => $this->getFile(),
145
                    'function' => 'unknown'
146 10
                )
147 10
            );
148 10
        }
149 11
        $this->trace = $trace;
150 11
    }
151
152
    /**
153
     * @return array
154
     */
155 2
    public function getTrace()
156
    {
157 2
        return $this->trace;
158
    }
159
}
160