Completed
Pull Request — master (#99)
by De Cramer
02:42
created

MenuFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace eXpansion\Bundle\Menu\Plugins\Gui;
4
5
use eXpansion\Framework\AdminGroups\Helpers\AdminGroups;
6
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
7
use eXpansion\Framework\Core\Model\Gui\ManiaScriptFactory;
8
use eXpansion\Framework\Core\Model\Gui\Widget;
9
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
10
use eXpansion\Framework\Core\Plugins\Gui\WidgetFactory;
11
use FML\Controls\Frame;
12
use FML\Controls\Label;
13
use FML\Controls\Quad;
14
15
16
/**
17
 * Class MenuFactory
18
 *
19
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
20
 * @author  oliver de Cramer <[email protected]>
21
 */
22
class MenuFactory extends WidgetFactory
23
{
24
    /** @var  ManiaScriptFactory */
25
    protected $menuScriptFactory;
26
27
    /** @var  AdminGroups */
28
    protected $adminGroupsHelper;
29
30
    public function __construct(
31
        $name,
32
        $sizeX,
33
        $sizeY,
34
        $posX,
35
        $posY,
36
        WidgetFactoryContext $context,
37
        ManiaScriptFactory $menuScriptFactory,
38
        AdminGroups $adminGroupsHelper
39
    ){
40
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
41
42
        $this->menuScriptFactory = $menuScriptFactory;
43
        $this->adminGroupsHelper = $adminGroupsHelper;
44
    }
45
46
47
    /**
48
     * @param ManialinkInterface|Widget $manialink
49
     */
50
    protected function createContent(ManialinkInterface $manialink)
51
    {
52
        $label = new Label("open");
53
        $label->setText("Open")
54
            ->setAreaFocusColor("5ff")
55
            ->setAreaColor("3af")
56
            ->setPosition(100, 60)
57
            ->setScriptEvents(true);
58
        // $manialink->addChild($label); // disables open menu button from main view
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
60
        /* Button frame first */
61
        $btnFrame = new Frame("buttons");
62
        $btnFrame->setZ(101)->setPosition(0, 30);
63
64
        $y = 0;
65
        $baseLabel = new Label();
66
        $baseLabel->setPosition(0, $y)
67
            ->setAreaColor("0000")
68
            ->setAreaFocusColor("0000")
69
            ->setSize(48, 7)
70
            ->setTextColor("FFF");
71
        $baseLabel->setAlign("center", "center2")
72
            ->addClass("button")
73
            ->setScriptEvents(true);
74
75
        $label = clone $baseLabel;
76
        $label->setPosition(0, $y -= 8)
77
            ->setText("Help")
78
            ->setDataAttributes(["do" => "!help"]);
79
        $btnFrame->addChild($label);
80
81
        $label = clone $baseLabel;
82
        $openSettingsId = $this->actionFactory->createManialinkAction($manialink, [$this, "showSettings"], []);
83
        $label->setPosition(0, $y -= 8)
84
            ->setText("Server Settings")
85
            ->setDataAttributes(["action" => $openSettingsId]);
86
        $btnFrame->addChild($label);
87
88
        $label = clone $baseLabel;
89
        $label->setPosition(0, $y -= 8)
90
            ->setText("Spectate")
91
            ->setDataAttributes(["action" => ""]);
92
        $btnFrame->addChild($label);
93
94
        $label = clone $baseLabel;
95
        $label->setPosition(0, $y -= 8)
96
            ->setText("Exit server")
97
            ->setDataAttributes(["do" => "!exit"]);
98
        $btnFrame->addChild($label);
99
100
101
        $label = clone $baseLabel;
102
        $label->setPosition(0, $y - 12)
103
            ->setText("Back to game")
104
            ->setDataAttributes(["action" => ""]);
105
        $btnFrame->addChild($label);
106
107
        $bgFrame = Frame::create("background");
108
        $bgFrame->setZ(100);
109
110
        $baseLabel = new Label();
111
        $baseLabel->setAreaColor("0000")
112
            ->setAreaFocusColor("0000")
113
            ->setTextColor("FFF");
114
        $baseLabel->setAlign("center", "center2")
115
            ->addClass("bg");
116
117
118
        $label = clone $baseLabel;
119
        $label->setText("")
120
            ->setTextSize(16)
121
            ->setPosition(0, 50)
122
            ->setSize(32, 32);
123
        $bgFrame->addChild($label);
124
125
        $label = clone $baseLabel;
126
        $label->setText("Server Menu")
127
            ->setTextSize(8)
128
            ->setPosition(0, 35)
129
            ->setTextFont('Oswald');
130
        $bgFrame->addChild($label);
131
132
133
        $quad = new Quad();
134
        $quad->addClass("bg")
135
            ->setPosition(0, 28)
136
            ->setSize(100, 0.5)
137
            ->setAlign("center", "center")
138
            ->setBackgroundColor("fff");
139
        $bgFrame->addChild($quad);
140
141
142
        $quad = new Quad();
143
        $quad->addClass("bg")
144
            ->setId("mainBg")
145
            ->setPosition(0, 0)
146
            ->setSize(322, 182);
147
        $quad->setAlign("center", "center")
148
            ->setStyles("Bgs1", "BgDialogBlur");
149
        $bgFrame->addChild($quad);
150
        $manialink->addChild($btnFrame);
151
152
        $manialink->addChild($bgFrame);
153
154
        $manialink->getFmlManialink()->addChild($this->menuScriptFactory->createScript(["settingsId" => $openSettingsId]));
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...
155
    }
156
157
    /**
158
     * @param ManialinkInterface|Widget $manialink
159
     */
160
    protected function updateContent(ManialinkInterface $manialink)
161
    {
162
        // Do stuff Here.
163
    }
164
165
    public function showSettings($login)
166
    {
167
        echo "Show settings: ".$login."\n";
168
    }
169
170
}
171