Completed
Push — assisted ( 48bf6b...70342f )
by Akihito
02:04
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 array   $msg
27
     *
28
     * @return string
29
     */
30 1
    private function buildMessage(Unbound $e, array $msg)
31
    {
32 1
        while ($e instanceof Unbound) {
33 1
            $msg[] = sprintf("- %s\n", $e->getMessage());
34 1
            $lastE = $e;
35 1
            $e = $e->getPrevious();
36 1
        }
37 1
        array_pop($msg);
38 1
        $msg = array_reverse($msg);
39
40 1
        return $this->getMainMessage($lastE) . implode('', $msg);
0 ignored issues
show
Bug introduced by
The variable $lastE does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
41
    }
42
43 2
    private function getMainMessage(Unbound $e)
44
    {
45 2
        return sprintf(
46 2
            "exception '%s' with message '%s'\n",
47 2
            get_class($e),
48 2
            $e->getMessage()
49 2
        );
50
    }
51
}
52