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

Unbound   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 5
c 5
b 1
f 0
lcom 1
cbo 0
dl 0
loc 42
ccs 19
cts 19
cp 1
rs 10

3 Methods

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