Completed
Branch FET-10785-ee-system-loader (ba7074)
by
unknown
168:24 queued 155:42
created

CoreLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
B load() 0 28 6
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 LoaderDecoratorInterface
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
        $object = $this->generator instanceof EE_Registry
63
            ? $this->generator->create($fqcn, $arguments)
64
            : $this->generator->brew($fqcn, $arguments);
65
        // if we did NOT receive an instance of the requested object from EE_Registry
66
        if(! $object instanceof $fqcn && $this->generator instanceof EE_Registry) {
67
            // then we need to try some of these other loading methods
68
            $alternate_loaders = array(
69
                'load_core',
70
                'load_class',
71
                'load_model',
72
                'load_helper',
73
                'load_lib',
74
                'load_model_class',
75
                'load_service',
76
                'load_dms',
77
            );
78
            foreach ($alternate_loaders as $alternate_loader) {
79
                $object = $this->generator->{$alternate_loader}($fqcn, $arguments);
80
                // but then break out of the loop as soon as we find what we are looking for
81
                if ($object instanceof $fqcn) {
82
                    break;
83
                }
84
            }
85
        }
86
        return $object;
87
    }
88
89
90
91
    /**
92
     * calls reset() on generator if method exists
93
     */
94
    public function reset()
95
    {
96
        if (method_exists($this->generator, 'reset')) {
97
            $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...
98
        }
99
    }
100
101
}
102
// End of file CoreLoader.php
103
// Location: core/services/loaders/CoreLoader.php