Completed
Branch FET-10785-ee-system-loader (4ec117)
by
unknown
139:17 queued 127:33
created

CoreLoader::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 1
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\services\container\CoffeeShop;
8
use EventEspresso\core\services\container\exceptions\ServiceNotFoundException;
9
use InvalidArgumentException;
10
11
defined('EVENT_ESPRESSO_VERSION') || exit;
12
13
14
15
/**
16
 * Class CoreLoader
17
 * Currently uses EE_Registry for instantiating classes,
18
 * but will later be replaced by the CoffeeShop DI container
19
 *
20
 * @package       Event Espresso
21
 * @author        Brent Christensen
22
 * @since         $VID:$
23
 */
24
class CoreLoader implements LoaderDecoratorInterface
25
{
26
27
    /**
28
     * @var EE_Registry|CoffeeShop $generator
29
     */
30
    private $generator;
31
32
33
34
    /**
35
     * CoreLoader constructor.
36
     *
37
     * @param EE_Registry|CoffeeShop $generator
38
     * @throws InvalidArgumentException
39
     */
40
    public function __construct($generator)
41
    {
42
        if(!($generator instanceof EE_Registry || $generator instanceof CoffeeShop)) {
43
            throw new InvalidArgumentException(
44
                esc_html__(
45
                    'The CoreLoader class must receive an instance of EE_Registry or the CoffeeShop DI container.',
46
                    'event_espresso'
47
                )
48
            );
49
        }
50
        $this->generator = $generator;
51
    }
52
53
54
55
    /**
56
     * @param string $fqcn
57
     * @param array  $arguments
58
     * @return mixed
59
     * @throws EE_Error
60
     * @throws ServiceNotFoundException
61
     */
62
    public function load($fqcn, $arguments = array())
63
    {
64
        return $this->generator instanceof EE_Registry
65
            ? $this->generator->create($fqcn, $arguments)
66
            : $this->generator->brew($fqcn, $arguments);
67
    }
68
69
70
71
    /**
72
     * calls reset() on generator if method exists
73
     */
74
    public function reset()
75
    {
76
        if (method_exists($this->generator, 'reset')) {
77
            $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...
78
        }
79
    }
80
81
}
82
// End of file CoreLoader.php
83
// Location: core/services/loaders/CoreLoader.php
84