Completed
Branch BUG-10669-spco-validation-msg (4c1309)
by
unknown
49:56 queued 37:41
created

CoreLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 57
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A load() 0 6 2
A reset() 0 6 2
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use EE_Registry;
6
use EventEspresso\core\services\container\CoffeeShop;
7
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException;
8
use InvalidArgumentException;
9
10
defined('EVENT_ESPRESSO_VERSION') || exit;
11
12
13
14
/**
15
 * Class CoreLoader
16
 * Currently uses EE_Registry for instantiating classes,
17
 * but will later be replaced by the CoffeeShop DI container
18
 *
19
 * @package       Event Espresso
20
 * @author        Brent Christensen
21
 * @since         $VID:$
22
 */
23
class CoreLoader implements LoaderInterface
24
{
25
26
    /**
27
     * @var EE_Registry|CoffeeShop $generator
28
     */
29
    private $generator;
30
31
32
33
    /**
34
     * CoreLoader constructor.
35
     *
36
     * @param EE_Registry|CoffeeShop $generator
37
     * @throws InvalidArgumentException
38
     */
39
    public function __construct($generator)
40
    {
41
        if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) {
42
            throw new InvalidArgumentException(
43
                esc_html__(
44
                    'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.',
45
                    'event_espresso'
46
                )
47
            );
48
        }
49
        $this->generator = $generator;
50
    }
51
52
53
54
    /**
55
     * @param string $fqcn
56
     * @param array  $arguments
57
     * @return mixed
58
     * @throws ServiceNotFoundException
59
     */
60
    public function load($fqcn, $arguments = array())
61
    {
62
        return $this->generator instanceof EE_Registry
63
            ? $this->generator->create($fqcn, $arguments)
64
            : $this->generator->brew($fqcn, $arguments);
65
    }
66
67
68
69
    /**
70
     * calls reset() on generator if method exists
71
     */
72
    public function reset()
73
    {
74
        if (method_exists($this->generator, 'reset')) {
75
            $this->generator->reset();
0 ignored issues
show
Bug introduced by
The method reset does only exist in EE_Registry, but not in EventEspresso\core\services\container\CoffeeShop.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
        }
77
    }
78
79
}
80
// End of file CoreLoader.php
81
// Location: core/services/loaders/CoreLoader.php