MetaCoreRuntimeException::toString()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by gerk on 29.09.16 10:33
4
 */
5
6
namespace PeekAndPoke\Component\MetaCore\Exception;
7
8
use PeekAndPoke\Component\Toolbox\ExceptionUtil;
9
10
11
/**
12
 * MetaCoreException
13
 *
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class MetaCoreRuntimeException extends \RuntimeException
17
{
18
    const UNKNOWN_TYPE                  = 1000;
19
    const NO_VAR_TAG                    = 1001;
20
    const COMPOUND_TYPE_WITH_NULLS_ONLY = 1002;
21
22
    /**
23
     * @param \Exception|null $previous
24
     *
25
     * @return MetaCoreRuntimeException
26
     */
27 1
    public static function unknownType(\Exception $previous = null)
28
    {
29 1
        return new static('Unknown type found', static::UNKNOWN_TYPE, $previous);
30
    }
31
32
    /**
33
     * @param \Exception|null $previous
34
     *
35
     * @return MetaCoreRuntimeException
36
     */
37 1
    public static function noVarTagFound(\Exception $previous = null)
38
    {
39 1
        return new static('No @var tag found', static::NO_VAR_TAG, $previous);
40
    }
41
42
    /**
43
     * @param \Exception|null $previous
44
     *
45
     * @return MetaCoreRuntimeException
46
     */
47 1
    public static function compoundTypeWithNullsOnly(\Exception $previous = null)
48
    {
49 1
        return new static('Only found "null" in compound type', static::COMPOUND_TYPE_WITH_NULLS_ONLY, $previous);
50
    }
51
52
    /**
53
     * Find the meta core root cause
54
     *
55
     * Walks through all previous Exception and find the the last in row that is a MetaCoreRuntimeException as well.
56
     *
57
     * @return MetaCoreRuntimeException
58
     */
59 3
    public function getMetaCoreRootCause()
60
    {
61 3
        $root = $this;
62
63 3
        while ($root->getPrevious() && $root->getPrevious() instanceof self) {
64 3
            $root = $root->getPrevious();
65
        }
66
67 3
        return $root;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 3
    public function toString()
74
    {
75 3
        return ExceptionUtil::toString($this);
76
    }
77
78
    /**
79
     * @return string
80
     */
81 3
    public function getCausesAsString()
82
    {
83 3
        $ret     = '';
84 3
        $current = $this;
85
86 3
        while ($current) {
87 3
            $ret .= $current->getMessage() . ' [' . $current->getCode() . ', ' . get_class($current) . ']' . "\n";
88 3
            $current = $current->getPrevious();
89
        }
90
91 3
        return $ret;
92
    }
93
}
94