Passed
Push — master ( d2db2a...b6e042 )
by Alec
01:24
created

DoesProcessException   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 16
eloc 23
dl 0
loc 82
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A doNotThrowOnError() 0 4 1
A throwOnError() 0 4 1
A doesThrowsOnError() 0 3 1
A processException() 0 5 2
A debugException() 0 5 3
A dumpExceptionObject() 0 4 3
A dumpExceptionMessage() 0 6 3
A dump() 0 7 2
1
<?php
2
/**
3
 * User: alec
4
 * Date: 28.12.18
5
 * Time: 15:21
6
 */
7
8
namespace AlecRabbit\Traits;
9
10
trait DoesProcessException
11
{
12
    /** @var bool */
13
    protected $throwOnError = true;
14
15
    /** @return self */
16 1
    public function doNotThrowOnError(): self
17
    {
18 1
        $this->throwOnError = false;
19 1
        return $this;
20
    }
21
22
    /** @return self */
23 1
    public function throwOnError(): self
24
    {
25 1
        $this->throwOnError = true;
26 1
        return $this;
27
    }
28
29
    /** @return bool */
30 2
    public function doesThrowsOnError(): bool
31
    {
32 2
        return $this->throwOnError;
33
    }
34
35
    /**
36
     * @param \Throwable $e
37
     * @throws \Throwable
38
     */
39 2
    protected function processException(\Throwable $e): void
40
    {
41 2
        $this->debugException($e);
42 2
        if ($this->throwOnError) {
43 1
            throw $e;
44
        }
45 1
    }
46
47
    /**
48
     * @param \Throwable $e
49
     */
50 2
    protected function debugException(\Throwable $e): void
51
    {
52 2
        if (\defined('APP_DEBUG') && APP_DEBUG) {
53 2
            $this->dumpExceptionMessage($e);
54 2
            $this->dumpExceptionObject($e);
55
        }
56 2
    }
57
58
    /**
59
     * @param \Throwable $e
60
     */
61 2
    protected function dumpExceptionMessage(\Throwable $e): void
62
    {
63 2
        if (\defined('DEBUG_DUMP_EXCEPTION') && DEBUG_DUMP_EXCEPTION) {
64 2
            $this->dump(
65 2
                '[' . \get_class($e) . '] ' . $e->getMessage(),
66 2
                $e->getTraceAsString()
67
            );
68
        }
69 2
    }
70
71
    /**
72
     * @param mixed ...$that
73
     */
74
    protected function dump(...$that): void
75
    {
76
        // @codeCoverageIgnoreStart
77
        if (\function_exists('dump')) {
78
            dump(...$that);
79
        } else {
80
            var_dump(...$that);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($that) looks like debug code. Are you sure you do not want to remove it?
Loading history...
81
        }
82
        // @codeCoverageIgnoreEnd
83
    }
84
85
    /**
86
     * @param \Throwable $e
87
     */
88 2
    protected function dumpExceptionObject(\Throwable $e): void
89
    {
90 2
        if (\defined('DEBUG_DUMP_EXCEPTION_CLASS') && DEBUG_DUMP_EXCEPTION_CLASS) {
91 2
            $this->dump($e);
92
        }
93 2
    }
94
}
95