Completed
Pull Request — master (#130)
by
unknown
02:40
created

MenuContentFactory::createSubMenu()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 73
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 8.5021
c 0
b 0
f 0
cc 6
eloc 51
nc 16
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace eXpansion\Bundle\Menu\Plugins\Gui;
4
5
use eXpansion\Bundle\Menu\DataProviders\MenuItemProvider;
6
use eXpansion\Bundle\Menu\Gui\MenuTabsFactory;
7
use eXpansion\Bundle\Menu\Model\Menu\ItemInterface;
8
use eXpansion\Bundle\Menu\Model\Menu\ParentItem;
9
use eXpansion\Framework\Core\Model\Gui\Manialink;
10
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
11
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
12
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
13
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
14
use eXpansion\Framework\Gui\Components\uiButton;
15
use eXpansion\Framework\Gui\Components\uiLabel;
16
use FML\Controls\Frame;
17
use FML\Controls\Label;
18
use FML\Controls\Quad;
19
20
21
/**
22
 * Class MenuContentFactory
23
 *
24
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
25
 * @author  oliver de Cramer <[email protected]>
26
 */
27
class MenuContentFactory extends WidgetFactory
28
{
29
    /** @var  MenuItemProvider */
30
    protected $menuItemProvider;
31
32
    /** @var  ManiaScriptFactory */
33
    protected $menuScriptFactory;
34
35
    /** @var  MenuTabsFactory */
36
    protected $menuTabsFactory;
37
38
    /** @var string */
39
    protected $currentPath = 'admin';
40
41
    /**
42
     * MenuContentFactory constructor.
43
     *
44
     * @param $name
45
     * @param $sizeX
46
     * @param $sizeY
47
     * @param null $posX
48
     * @param null $posY
49
     * @param WidgetFactoryContext $context
50
     * @param ManiaScriptFactory $maniaScriptFactory
51
     * @param MenuItemProvider $menuItemProvider
52
     * @param MenuTabsFactory $menuTabsFactory
53
     */
54
    public function __construct(
55
        $name,
56
        $sizeX,
57
        $sizeY,
58
        $posX,
59
        $posY,
60
        WidgetFactoryContext $context,
61
        ManiaScriptFactory $maniaScriptFactory,
62
        MenuItemProvider $menuItemProvider,
63
        MenuTabsFactory $menuTabsFactory
64
    ) {
65
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
66
67
        $this->menuItemProvider = $menuItemProvider;
68
        $this->menuScriptFactory = $maniaScriptFactory;
69
        $this->menuTabsFactory = $menuTabsFactory;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    protected function createContent(ManialinkInterface $manialink)
76
    {
77
        parent::createContent($manialink);
78
79
        $tabsFrame = new Frame('tabs');
80
        $tabsFrame->setPosition(-144, 82);
81
        $manialink->getContentFrame()->setZ(101);
82
        $manialink->getContentFrame()->addChild($tabsFrame);
83
        $manialink->setData('tabs_frame', $tabsFrame);
84
85
        $contentFrame = new Frame('menu_content');
86
        $contentFrame->setPosition(0, 72);
87
        $manialink->getContentFrame()->addChild($contentFrame);
88
        $manialink->setData('menu_content_frame', $contentFrame);
89
90
        $backGroundFrame = new Frame('background');
91
        $manialink->getContentFrame()->addChild($backGroundFrame);
92
93
94
        /*
95
         * Adding background frame
96
         */
97
        $bgFrame = Frame::create("background");
98
99
        $quad = new Quad();
100
        $quad->addClass("bg")
101
            ->setId("mainBg")
102
            ->setPosition(0, 0)
103
            ->setSize(322, 182);
104
        $quad->setAlign("center", "center")
105
            ->setStyles("Bgs1", "BgDialogBlur");
106
        $bgFrame->addChild($quad);
107
108
        $manialink->getContentFrame()->addChild($bgFrame);
109
110
        /**
111
         * Adding script
112
         */
113
        $manialink->getFmlManialink()->addChild($this->menuScriptFactory->createScript([]));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getFmlManialink() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Widget, eXpansion\Framework\Core\Model\Gui\Window.

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...
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    protected function updateContent(ManialinkInterface $manialink)
120
    {
121
        parent::updateContent($manialink);
122
123
        $currentPath = $manialink->getData('current_path');
124
        if (is_null($currentPath)) {
125
            $currentPath = $this->currentPath;
126
            $manialink->setData('current_path', $currentPath);
127
        }
128
        $currentPath = trim($currentPath, '/');
129
130
131
        $rootItem = $this->menuItemProvider->getRootItem();
132
        $pathParts = explode('/', $currentPath);
133
134
        $this->createTabsMenu($manialink, $rootItem, $pathParts[0]);
0 ignored issues
show
Documentation introduced by
$rootItem is of type object<eXpansion\Bundle\...enu\ItemInterface>|null, but the function expects a object<eXpansion\Bundle\...\Model\Menu\ParentItem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
136
        /** @var Frame $contentFrame */
137
        $contentFrame = $manialink->getData('menu_content_frame');
138
        $contentFrame->removeAllChildren();
139
140
        $displayLevel = 0;
141
        for ($i = count($pathParts) - 1; $i >= 0; $i--) {
142
            $path = implode('/', array_slice($pathParts, 0, $i + 1));
143
144
            /** @var ParentItem $parentItem */
145
            $parentItem = $rootItem->getChild($path);
146
147
            $this->createSubMenu($manialink, $contentFrame, $parentItem, $displayLevel++);
0 ignored issues
show
Compatibility introduced by
$manialink of type object<eXpansion\Framewo...Gui\ManialinkInterface> is not a sub-type of object<eXpansion\Framewo...re\Model\Gui\Manialink>. It seems like you assume a concrete implementation of the interface eXpansion\Framework\Core...\Gui\ManialinkInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
148
        }
149
    }
150
151
    /**
152
     * Create tabs level menu.
153
     *
154
     * @param ManialinkInterface $manialink
155
     * @param ParentItem         $rootItem
156
     * @param                    $openId
157
     */
158
    protected function createTabsMenu(ManialinkInterface $manialink, ParentItem $rootItem, $openId)
159
    {
160
        /** @var Frame $tabsFrame */
161
        $tabsFrame = $manialink->getData('tabs_frame');
162
        $tabsFrame->removeAllChildren();
163
164
        $this->menuTabsFactory->createTabsMenu($manialink, $tabsFrame, $rootItem, $openId);
165
    }
166
167
    /**
168
     * Create content for sub menu.
169
     *
170
     * @param Manialink  $manialink
171
     * @param Frame      $frame
172
     * @param ParentItem $parentItem
173
     * @param            $displayLevel
174
     */
175
    protected function createSubMenu(Manialink $manialink, Frame $frame, ParentItem $parentItem, $displayLevel)
176
    {
177
        $posX = $displayLevel * (-160.0/3);
178
        $posY = ($displayLevel * (-100.0/3)) * 0.5;
179
        $scale = (0.5 / ($displayLevel + 1)) + 0.5;
180
181
        $contentFrame = new Frame();
182
        $contentFrame->setScale($scale);
183
        $contentFrame->setPosition($posX, $posY);
184
        $frame->addChild($contentFrame);
185
186
        if ($displayLevel > 0) {
187
            $overlay = new Quad();
188
            $overlay->setSize(60, 120);
189
            $overlay->setPosition(-30, 0);
190
            $overlay->setStyles("Bgs1", "BgDialogBlur");
191
192
            $action = $this->actionFactory->createManialinkAction(
193
                $manialink,
194
                [$this, 'callbackItemClick'],
195
                ['item' => $parentItem, 'ml' => $manialink]
196
            );
197
198
199
            $contentFrame->addChild($overlay);
200
            $overlay->setAction($action);
201
        }
202
203
        /* TITLE */
204
        $titleLabel = $this->uiFactory->createLabel($parentItem->getLabelId(), uiLabel::TYPE_TITLE);
205
        $titleLabel->setSize(60, 8);
206
        $titleLabel->setPosition(-30, 0);
207
        $titleLabel->setTranslate(true);
208
        $contentFrame->addChild($titleLabel);
209
210
        $titleLine = $this->uiFactory->createLine(-30, -8);
211
        $titleLine->to(30, -8);
212
        $contentFrame->addChild($titleLine);
213
214
        $posY = -12;
215
        foreach ($parentItem->getChilds() as $item) {
216
            if ($item->isVisibleFor($manialink->getUserGroup())) {
0 ignored issues
show
Documentation introduced by
$manialink->getUserGroup() is of type object<eXpansion\Framewo...Model\UserGroups\Group>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
217
                $button = $this->uiFactory->createButton($item->getLabelId());
218
                $button->setPosition(-25, $posY);
219
                $button->setSize(50, 8);
220
                $button->setTranslate(true);
221
222
                if ($displayLevel == 0) {
223
                    $action = $this->actionFactory->createManialinkAction(
224
                        $manialink,
225
                        [$this, 'callbackItemClick'],
226
                        ['item' => $item, 'ml' => $manialink]
227
                    );
228
                    $button->setAction($action);
229
                }
230
231
                $contentFrame->addChild($button);
232
                $posY -= 12;
233
            }
234
        }
235
236
        if ($displayLevel == 0) {
237
238
            $button = $this->uiFactory->createButton('expansion_menu.menu_close', uiButton::TYPE_DECORATED);
239
            $button->setBackgroundColor(uiButton::COLOR_WARNING);
240
            $button->setPosition(-25, $posY - 12);
241
            $button->setSize(50, 8);
242
            $button->setTranslate(true);
243
            $action = $this->actionFactory->createManialinkAction($manialink, [$this, 'callbackClose'], ['ml' =>$manialink]);
244
            $button->setAction($action);
245
            $contentFrame->addChild($button);
246
        }
247
    }
248
249
    /**
250
     * Callback when an item of the menu is clicked on.
251
     *
252
     * @param $login
253
     * @param $params
254
     * @param $args
255
     */
256
    public function callbackItemClick($login, $params, $args)
257
    {
258
        /** @var ItemInterface $item */
259
        $item = $args['item'];
260
        $item->execute($this, $args['ml'], $login, $params, $args);
261
    }
262
263
    /**
264
     * Callback when the close button is clicked.
265
     *
266
     * @param $login
267
     * @param $params
268
     * @param $args
269
     */
270
    public function callbackClose($login, $params, $args)
271
    {
272
        $this->destroy($args['ml']->getUserGroup());
273
    }
274
}
275