Passed
Push — test ( 33612f...4075b0 )
by Tom
12:36
created

ErrorCatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
dl 0
loc 62
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 1
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A end() 0 7 1
A __construct() 0 3 1
A create() 0 3 1
A getLastErrorMessage() 0 4 2
A start() 0 6 1
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 string|null
56
     */
57 2
    public function getLastErrorMessage()
58
    {
59 2
        $error = $this->last !== $this->previous;
60 2
        return $error ? $this->last['message'] : null;
61
    }
62
63
    /**
64
     * start catching session (is auto-started on create)
65
     *
66
     * @return void
67
     */
68 3
    private function start()
69
    {
70 3
        @$void;
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $void seems to be never defined.
Loading history...
71 3
        $this->previous = error_get_last();
72 3
        $this->level = error_reporting();
73 3
        error_reporting(0);
74 3
    }
75
}
76