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

Unbound   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 6
c 6
b 1
f 0
lcom 1
cbo 0
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMessage() 0 13 2
A __toString() 0 13 3
A getMainMessage() 0 8 1
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