Completed
Branch BUG-11137-domain-base (06ea45)
by
unknown
44:54 queued 34:30
created

CoreLoader::load()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 25
nc 9
nop 3
dl 0
loc 35
rs 8.439
c 1
b 0
f 1
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use EE_Error;
6
use EE_Registry;
7
use EventEspresso\core\exceptions\InvalidClassException;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidIdentifierException;
10
use EventEspresso\core\services\container\CoffeeMaker;
11
use EventEspresso\core\services\container\CoffeeShop;
12
use EventEspresso\core\services\container\exceptions\InstantiationException;
13
use EventEspresso\core\services\container\exceptions\ServiceExistsException;
14
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException;
15
use InvalidArgumentException;
16
use OutOfBoundsException;
17
use ReflectionException;
18
19
defined('EVENT_ESPRESSO_VERSION') || exit;
20
21
22
23
/**
24
 * Class CoreLoader
25
 * Currently uses EE_Registry for instantiating classes,
26
 * but will later be replaced by the CoffeeShop DI container
27
 *
28
 * @package       Event Espresso
29
 * @author        Brent Christensen
30
 * @since         $VID:$
31
 */
32
class CoreLoader implements LoaderDecoratorInterface
33
{
34
35
    /**
36
     * @var EE_Registry|CoffeeShop $generator
37
     */
38
    private $generator;
39
40
41
42
    /**
43
     * CoreLoader constructor.
44
     *
45
     * @param EE_Registry|CoffeeShop $generator
46
     * @throws InvalidArgumentException
47
     */
48
    public function __construct($generator)
49
    {
50
        if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) {
51
            throw new InvalidArgumentException(
52
                esc_html__(
53
                    'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.',
54
                    'event_espresso'
55
                )
56
            );
57
        }
58
        $this->generator = $generator;
59
    }
60
61
62
63
    /**
64
     * Calls the appropriate loading method from the installed generator;
65
     * If EE_Registry is being used, then the additional parameters for the EE_Registry::create() method
66
     * can be added to the $arguments array and they will be extracted and passed to EE_Registry::create(),
67
     * but NOT to the class being instantiated.
68
     * This is done by adding the parameters to the $arguments array as follows:
69
     *  array(
70
     *      'EE_Registry::create(from_db)'   => true, // boolean value, default = false
71
     *      'EE_Registry::create(load_only)' => true, // boolean value, default = false
72
     *      'EE_Registry::create(addon)'     => true, // boolean value, default = false
73
     *  )
74
     *
75
     * @param string $fqcn
76
     * @param array  $arguments
77
     * @param bool   $shared
78
     * @return mixed
79
     * @throws OutOfBoundsException
80
     * @throws ServiceExistsException
81
     * @throws InstantiationException
82
     * @throws InvalidIdentifierException
83
     * @throws InvalidDataTypeException
84
     * @throws InvalidClassException
85
     * @throws EE_Error
86
     * @throws ServiceNotFoundException
87
     * @throws ReflectionException
88
     */
89
    public function load($fqcn, $arguments = array(), $shared = true)
90
    {
91
        if($this->generator instanceof EE_Registry) {
92
            // check if additional EE_Registry::create() arguments have been passed
93
            // from_db
94
            $from_db = isset($arguments['EE_Registry::create(from_db)'])
95
                ? $arguments['EE_Registry::create(from_db)']
96
                : null;
97
            unset($arguments['EE_Registry::create(from_db)']);
98
            // load_only
99
            $load_only = isset($arguments['EE_Registry::create(load_only)'])
100
                ? $arguments['EE_Registry::create(load_only)']
101
                : null;
102
            unset($arguments['EE_Registry::create(load_only)']);
103
            // addon
104
            $addon = isset($arguments['EE_Registry::create(addon)'])
105
                ? $arguments['EE_Registry::create(addon)']
106
                : null;
107
            unset($arguments['EE_Registry::create(addon)']);
108
            return $this->generator->create(
109
                $fqcn,
110
                $arguments,
111
                $shared,
112
                $from_db,
113
                $load_only,
114
                $addon
115
            );
116
        }
117
        return $this->generator->brew(
118
            $fqcn,
119
            $arguments,
120
            $shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW
121
        );
122
123
    }
124
125
126
127
    /**
128
     * calls reset() on generator if method exists
129
     *
130
     * @throws EE_Error
131
     * @throws ReflectionException
132
     */
133
    public function reset()
134
    {
135
        if (method_exists($this->generator, 'reset')) {
136
            $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...
137
        }
138
    }
139
140
}
141
// End of file CoreLoader.php
142
// Location: core/services/loaders/CoreLoader.php
143