AbstractElementFactory::__set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Malezha\Menu\Factory;
3
4
use Illuminate\Contracts\Config\Repository;
5
use Illuminate\Contracts\Container\Container;
6
use Malezha\Menu\Contracts\Element;
7
use Malezha\Menu\Contracts\ElementFactory;
8
9
/**
10
 * Class AbstractElementFactory
11
 * @package Malezha\Menu\Factory
12
 */
13
abstract class AbstractElementFactory implements ElementFactory
14
{
15
    /**
16
     * @var Container
17
     */
18
    protected $app;
19
20
    /**
21
     * @var array
22
     */
23
    protected $parameters = [];
24
25
    /**
26
     * @inheritdoc
27
     */
28 28
    public function __construct(Container $container)
29
    {
30 28
        $this->app = $container;
31 28
    }
32
33
    /**
34
     * @param string $class
35
     * @return array
36
     */
37 28
    protected function getElementConfig($class)
38
    {
39 28
        return $this->app->make(Repository::class)->get('menu.elements')[$class];
40
    }
41
42
    /**
43
     * @param string $name
44
     * @return mixed
45
     */
46 9
    protected function getParameter($name)
47
    {
48 9
        if (array_key_exists($name, $this->parameters)) {
49 9
            return $this->parameters[$name];
50
        }
51
52
        return null;
53
    }
54
55
    /**
56
     * @param string $name
57
     * @param mixed $value
58
     * @return $this
59
     */
60 25
    protected function setParameter($name, $value)
61
    {
62 25
        $this->parameters[$name] = $value;
63
        
64 25
        return $this;
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return $this
70
     */
71
    protected function unsetParameter($name)
72
    {
73
        unset($this->parameters[$name]);
74
        
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $name
80
     * @return bool
81
     */
82
    protected function existsParameter($name)
83
    {
84
        return array_key_exists($name, $this->parameters);
85
    }
86
87
    /**
88
     * @param array $parameters
89
     * @return array
90
     */
91 27
    protected function mergeParameters($parameters = [])
92
    {
93 27
        return array_merge($this->parameters, $parameters);
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99 9
    function __get($name)
100
    {
101 9
        return $this->getParameter($name);
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107 25
    function __set($name, $value)
108
    {
109 25
        $this->setParameter($name, $value);
110 25
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    function __isset($name)
116
    {
117
        return $this->existsParameter($name);
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    function __unset($name)
124
    {
125
        $this->unsetParameter($name);
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131
    public function toArray()
132
    {
133
        return $this->build()->toArray();
134
    }
135
136
    /**
137
     * @param Element $element
138
     */
139 22
    protected function setDisplayRule(Element $element)
140
    {
141 22
        if (array_key_exists('displayRule', $this->parameters) && method_exists($element, 'setDisplayRule')) {
142 22
            $element->setDisplayRule($this->parameters['displayRule']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Malezha\Menu\Contracts\Element as the method setDisplayRule() does only exist in the following implementations of said interface: Malezha\Menu\Element\Link, Malezha\Menu\Element\SubMenu, Malezha\Menu\Element\Text.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
143
        }
144
    }
145
}