Completed
Push — 2.x ( 9979b5...bcb56c )
by Akihito
10s
created

Unbound::buildMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di\Exception;
8
9
use Ray\Di\Exception;
10
11
class Unbound extends \LogicException implements ExceptionInterface
12
{
13 3
    public function __toString()
14
    {
15 3
        $messages = [sprintf("- %s\n", $this->getMessage())];
16 3
        $e = $this->getPrevious();
17 3
        if (! $e instanceof \Exception) {
18 1
            return $this->getMainMessage($this);
19
        }
20 2
        if ($e instanceof Unbound) {
21 1
            return $this->buildMessage($e, $messages) . "\n" . $e->getTraceAsString();
22
        }
23
24 1
        return parent::__toString();
25
    }
26
27
    /**
28
     * @param Unbound  $e
29
     * @param string[] $msg
30
     *
31
     * @return string
32
     */
33 1
    private function buildMessage(Unbound $e, array $msg)
34
    {
35 1
        $lastE = $e;
36 1
        while ($e instanceof Unbound) {
37 1
            $msg[] = sprintf("- %s\n", $e->getMessage());
38 1
            $lastE = $e;
39 1
            $e = $e->getPrevious();
40
        }
41 1
        array_pop($msg);
42 1
        $msg = array_reverse($msg);
43
44 1
        return $this->getMainMessage($lastE) . implode('', $msg);
45
    }
46
47 2
    private function getMainMessage(Unbound $e)
48
    {
49 2
        return sprintf(
50 2
            "exception '%s' with message '%s'\n",
51
            get_class($e),
52 2
            $e->getMessage()
53
        );
54
    }
55
}
56