Passed
Push — master ( 3f0369...26e86a )
by Tom
04:24
created

ErrorCatcher::getLastErrorMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
/**
8
 * catch errors w/ restore
9
 *
10
 * @package Ktomk\Pipelines
11
 */
12
class ErrorCatcher
13
{
14
    /**
15
     * @var null|array
16
     */
17
    private $last;
18
19
    /**
20
     * @var null|array
21
     */
22
    private $previous;
23
24
    /**
25
     * @var int
26
     */
27
    private $level;
28
29
    /**
30
     * @return ErrorCatcher
31
     */
32 1
    public static function create()
33
    {
34 1
        return new self();
35
    }
36
37 3
    public function __construct()
38
    {
39 3
        $this->start();
40 3
    }
41
42
    /**
43
     * @return bool an error occurred between start and end
44
     */
45 2
    public function end()
46
    {
47 2
        $this->last = error_get_last();
48 2
        $error = $this->last !== $this->previous;
49 2
        error_reporting($this->level);
50
51 2
        return $error;
52
    }
53
54
    /**
55
     * @return null|string
56
     */
57 2
    public function getLastErrorMessage()
58
    {
59 2
        $error = $this->last !== $this->previous;
60
61 2
        return $error ? $this->last['message'] : null;
62
    }
63
64
    /**
65
     * start catching session (is auto-started on create)
66
     *
67
     * @return void
68
     */
69 3
    private function start()
70
    {
71 3
        @$void;
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $void seems to be never defined.
Loading history...
72 3
        $this->previous = error_get_last();
73 3
        $this->level = error_reporting();
74 3
        error_reporting(0);
75 3
    }
76
}
77