InjectionException   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D fromInvalidCallable() 0 37 10
A getDependencyChain() 0 4 1
1
<?php
2
3
namespace Atreyu;
4
5
class InjectionException extends InjectorException
6
{
7
    public $dependencyChain;
8
    
9
    public function __construct(array $inProgressMakes, $message = "", $code = 0, \Exception $previous = null)
10
    {
11
        $this->dependencyChain = array_flip($inProgressMakes);
12
        ksort($this->dependencyChain);
13
        
14
        parent::__construct($message, $code, $previous);
15
    }
16
17
    /**
18
     * Add a human readable version of the invalid callable to the standard 'invalid invokable' message.
19
     */
20
    public static function fromInvalidCallable(
21
        array $inProgressMakes,
22
        $callableOrMethodStr,
23
        \Exception $previous = null
24
    ) {
25
        $callableString = null;
26
27
        if (is_string($callableOrMethodStr)) {
28
            $callableString .= $callableOrMethodStr;
29
        } else if (is_array($callableOrMethodStr) && 
30
            array_key_exists(0, $callableOrMethodStr) &&
31
            array_key_exists(0, $callableOrMethodStr)) {
32
            if (is_string($callableOrMethodStr[0]) && is_string($callableOrMethodStr[1])) {
33
                $callableString .= $callableOrMethodStr[0].'::'.$callableOrMethodStr[1];
34
            } else if (is_object($callableOrMethodStr[0]) && is_string($callableOrMethodStr[1])) {
35
                $callableString .= sprintf(
36
                    "[object(%s), '%s']",
37
                    get_class($callableOrMethodStr[0]),
38
                    $callableOrMethodStr[1]
39
                );
40
            }
41
        }
42
43
        if ($callableString) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $callableString of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
44
            // Prevent accidental usage of long strings from filling logs. 
45
            $callableString = substr($callableString, 0, 250);
46
            $message = sprintf(
47
                "%s. Invalid callable was '%s'",
48
                Injector::M_INVOKABLE,
49
                $callableString
50
            );
51
        } else {
52
            $message = \Atreyu\Injector::M_INVOKABLE;
53
        }
54
55
        return new self($inProgressMakes, $message, Injector::E_INVOKABLE, $previous);
56
    }
57
58
    /**
59
     * Returns the hierarchy of dependencies that were being created when
60
     * the exception occurred.
61
     * @return array
62
     */
63
    public function getDependencyChain()
64
    {
65
        return $this->dependencyChain;
66
    }
67
}
68