Completed
Pull Request — 2.x (#139)
by Akihito
05:32
created

Unbound::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
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 2
    public function __toString()
14
    {
15 2
        $messages = [sprintf("- %s\n", $this->getMessage())];
16 2
        $e = $this->getPrevious();
17 2
        if (! $e instanceof \Exception) {
18 1
            return $this->getMainMessage($this);
19
        }
20
21 1
        return $this->buildMessage($e, $messages) . "\n" . $e->getTraceAsString();
0 ignored issues
show
Compatibility introduced by
$e of type object<Exception> is not a sub-type of object<Ray\Di\Exception\Unbound>. It seems like you assume a child class of the class Exception to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
22
    }
23
24
    /**
25
     * @param Unbound  $e
26
     * @param string[] $msg
27
     *
28
     * @return string
29
     */
30 1
    private function buildMessage(Unbound $e, array $msg)
31
    {
32 1
        $lastE = $e;
33 1
        while ($e instanceof Unbound) {
34 1
            $msg[] = sprintf("- %s\n", $e->getMessage());
35 1
            $lastE = $e;
36 1
            $e = $e->getPrevious();
37
        }
38 1
        array_pop($msg);
39 1
        $msg = array_reverse($msg);
40
41 1
        return $this->getMainMessage($lastE) . implode('', $msg);
42
    }
43
44 2
    private function getMainMessage(Unbound $e)
45
    {
46 2
        return sprintf(
47 2
            "exception '%s' with message '%s'\n",
48
            get_class($e),
49 2
            $e->getMessage()
50
        );
51
    }
52
}
53