Completed
Branch BUG-11137-domain-base (fa6779)
by
unknown
35:25 queued 23:29
created

CoreLoader::load()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 17
nop 3
dl 0
loc 39
rs 6.7272
c 0
b 0
f 0
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
                ? (bool)$arguments['EE_Registry::create(from_db)']
96
                : false;
97
            // load_only
98
            $load_only = isset($arguments['EE_Registry::create(load_only)'])
99
                ? (bool)$arguments['EE_Registry::create(load_only)']
100
                : false;
101
            // addon
102
            $addon = isset($arguments['EE_Registry::create(addon)'])
103
                ? (bool)$arguments['EE_Registry::create(addon)']
104
                : false;
105
            unset(
106
                $arguments['EE_Registry::create(from_db)'],
107
                $arguments['EE_Registry::create(load_only)'],
108
                $arguments['EE_Registry::create(addon)']
109
            );
110
            // addons need to be cached on EE_Registry
111
            $shared = $addon ? true : $shared;
112
            return $this->generator->create(
113
                $fqcn,
114
                $arguments,
115
                $shared,
116
                $from_db,
117
                $load_only,
118
                $addon
119
            );
120
        }
121
        return $this->generator->brew(
122
            $fqcn,
123
            $arguments,
124
            $shared ? CoffeeMaker::BREW_SHARED : CoffeeMaker::BREW_NEW
125
        );
126
127
    }
128
129
130
131
    /**
132
     * calls reset() on generator if method exists
133
     *
134
     * @throws EE_Error
135
     * @throws ReflectionException
136
     */
137
    public function reset()
138
    {
139
        if (method_exists($this->generator, 'reset')) {
140
            $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...
141
        }
142
    }
143
144
}
145
// End of file CoreLoader.php
146
// Location: core/services/loaders/CoreLoader.php
147