Completed
Push — dev ( 214409...8e757d )
by
unknown
03:22
created

MenuFactory::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 105
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 105
rs 8.2857
cc 1
eloc 83
nc 1
nop 1

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\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\Plugins\Gui\WidgetFactory;
10
use FML\Controls\Frame;
11
use FML\Controls\Label;
12
use FML\Controls\Quad;
13
14
15
/**
16
 * Class MenuFactory
17
 *
18
 * @package eXpansion\Bundle\Menu\Plugins\Gui;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class MenuFactory extends WidgetFactory
22
{
23
    /** @var  ManiaScriptFactory */
24
    protected $menuScriptFactory;
25
26
    /** @var  AdminGroups */
27
    protected $adminGroupsHelper;
28
29
    /**
30
     * @param ManiaScriptFactory $menuScriptFactory
31
     */
32
    public function setMenuScriptFactory($menuScriptFactory)
33
    {
34
        $this->menuScriptFactory = $menuScriptFactory;
35
    }
36
37
    /**
38
     * @param AdminGroups $adminGroupsHelper
39
     */
40
    public function setAdminGroupsHelper($adminGroupsHelper)
41
    {
42
        $this->adminGroupsHelper = $adminGroupsHelper;
43
    }
44
45
    /**
46
     * @param ManialinkInterface|Widget $manialink
47
     */
48
    protected function createContent(ManialinkInterface $manialink)
49
    {
50
        $label = new Label("open");
51
        $label->setText("Open")
52
            ->setAreaFocusColor("5ff")
53
            ->setAreaColor("3af")
54
            ->setPosition(100, 60)
55
            ->setScriptEvents(true);
56
        // $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...
57
58
        /* Button frame first */
59
        $btnFrame = new Frame("buttons");
60
        $btnFrame->setZ(101)->setPosition(0, 30);
61
62
        $y = 0;
63
        $baseLabel = new Label();
64
        $baseLabel->setPosition(0, $y)
65
            ->setAreaColor("0000")
66
            ->setAreaFocusColor("0000")
67
            ->setSize(48, 7)
68
            ->setTextColor("FFF");
69
        $baseLabel->setAlign("center", "center2")
70
            ->addClass("button")
71
            ->setScriptEvents(true);
72
73
        $label = clone $baseLabel;
74
        $label->setPosition(0, $y -= 8)
75
            ->setText("Help")
76
            ->setDataAttributes(["do" => "!help"]);
77
        $btnFrame->addChild($label);
78
79
        $label = clone $baseLabel;
80
        $openSettingsId = $this->actionFactory->createManialinkAction($manialink, [$this, "showSettings"], []);
81
        $label->setPosition(0, $y -= 8)
82
            ->setText("Server Settings")
83
            ->setDataAttributes(["action" => $openSettingsId]);
84
        $btnFrame->addChild($label);
85
86
        $label = clone $baseLabel;
87
        $label->setPosition(0, $y -= 8)
88
            ->setText("Spectate")
89
            ->setDataAttributes(["action" => ""]);
90
        $btnFrame->addChild($label);
91
92
        $label = clone $baseLabel;
93
        $label->setPosition(0, $y -= 8)
94
            ->setText("Exit server")
95
            ->setDataAttributes(["do" => "!exit"]);
96
        $btnFrame->addChild($label);
97
98
99
        $label = clone $baseLabel;
100
        $label->setPosition(0, $y - 12)
101
            ->setText("Back to game")
102
            ->setDataAttributes(["action" => ""]);
103
        $btnFrame->addChild($label);
104
105
        $bgFrame = Frame::create("background");
106
        $bgFrame->setZ(100);
107
108
        $baseLabel = new Label();
109
        $baseLabel->setAreaColor("0000")
110
            ->setAreaFocusColor("0000")
111
            ->setTextColor("FFF");
112
        $baseLabel->setAlign("center", "center2")
113
            ->addClass("bg");
114
115
116
        $label = clone $baseLabel;
117
        $label->setText("")
118
            ->setTextSize(16)
119
            ->setPosition(0, 50)
120
            ->setSize(32, 32);
121
        $bgFrame->addChild($label);
122
123
        $label = clone $baseLabel;
124
        $label->setText("Server Menu")
125
            ->setTextSize(8)
126
            ->setPosition(0, 35)
127
            ->setTextFont('Oswald');
128
        $bgFrame->addChild($label);
129
130
131
        $quad = new Quad();
132
        $quad->addClass("bg")
133
            ->setPosition(0, 28)
134
            ->setSize(100, 0.5)
135
            ->setAlign("center", "center")
136
            ->setBackgroundColor("fff");
137
        $bgFrame->addChild($quad);
138
139
140
        $quad = new Quad();
141
        $quad->addClass("bg")
142
            ->setId("mainBg")
143
            ->setPosition(0, 0)
144
            ->setSize(322, 182);
145
        $quad->setAlign("center", "center")
146
            ->setStyles("Bgs1", "BgDialogBlur");
147
        $bgFrame->addChild($quad);
148
        $manialink->addChild($btnFrame);
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 addChild() 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...
149
150
        $manialink->addChild($bgFrame);
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 addChild() 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...
151
        $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...
152
    }
153
154
    /**
155
     * @param ManialinkInterface|Widget $manialink
156
     */
157
    protected function updateContent(ManialinkInterface $manialink)
158
    {
159
        // Do stuff Here.
160
    }
161
162
    public function showSettings($login)
163
    {
164
        echo "Show settings: ".$login."\n";
165
    }
166
167
}
168