Completed
Push — dev ( 845ef5...8eacd8 )
by
unknown
02:50
created

MenuFactory::createContent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 111
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 111
rs 8.2857
cc 1
eloc 84
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 Widget $manialink
47
     */
48
    protected function createContent(ManialinkInterface $manialink)
49
    {
50
        /* Button frame first */
51
52
        $label = Label::create("open");
53
        $label->setText("Open")
54
            ->setAreaFocusColor("5ff")
55
            ->setAreaColor("3af")
56
            ->setPosition(100, 60)
57
            ->setScriptEvents(true);
58
       // $manialink->addChild($label);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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
        $btnFrame = Frame::create("buttons");
61
        $btnFrame->setZ(101)->setPosition(0, 30);
62
63
        $y = 0;
64
        $baseLabel = Label::create();
65
        $baseLabel->setAreaColor("0000")
66
            ->setAreaFocusColor("0000")
67
            ->setTextColor("FFF")
68
            ->setAlign("center", "center2")
69
            ->addClass("button")
70
            ->setScriptEvents(true);
71
72
        $label = clone $baseLabel;
73
        $label->setPosition(0, $y -= 8)
74
            ->setText("Help")
75
            ->setTextPrefix(" ")
76
            ->setDataAttributes(["action" => "!help"]);
77
        $btnFrame->addChild($label);
78
79
        $label = clone $baseLabel;
80
        $label->setPosition(0, $y -= 10)
81
            ->setText("Show Profile")
82
            ->setTextPrefix(" ")
83
            ->setDataAttributes(["action" => "!profile"]);
84
        $btnFrame->addChild($label);
85
86
        $label = clone $baseLabel;
87
        $label->setPosition(0, $y -= 8)
88
            ->setText("Spectate")
89
            ->setTextPrefix("")
90
            ->setDataAttributes(["action" => "!spec"]);
91
        $btnFrame->addChild($label);
92
93
        $label = clone $baseLabel;
94
        $label->setPosition(0, $y -= 8)
95
            ->setText("Return to game")
96
            ->setTextPrefix(" ")
97
            ->setDataAttributes(["action" => "!close"]);
98
        $btnFrame->addChild($label);
99
100
101
        $label = clone $baseLabel;
102
        $label->setPosition(0, $y -= 12)
103
            ->setText("Return to main menu")
104
            ->setTextPrefix("🏁 ")
105
            ->setDataAttributes(["action" => "!quit"]);
106
        $btnFrame->addChild($label);
107
108
109
110
        $bgFrame = Frame::create("background");
111
        $bgFrame->setZ(100);
112
113
        $baseLabel = Label::create();
114
        $baseLabel->setAreaColor("0000")
115
            ->setAreaFocusColor("0000")
116
            ->setTextColor("FFF")
117
            ->setAlign("center", "center2")
118
            ->addClass("bg");
119
120
121
        $label = clone $baseLabel;
122
        $label->setText("")
123
            ->setTextSize(16)
124
            ->setPosition(0, 50)
125
            ->setSize(32, 32);
126
        $bgFrame->addChild($label);
127
128
        $label = clone $baseLabel;
129
        $label->setText("Server Menu")
130
            ->setTextSize(8)
131
            ->setPosition(0, 35)
132
            ->setTextFont('Oswald');
133
        $bgFrame->addChild($label);
134
135
136
        $quad = Quad::create();
137
        $quad->addClass("bg")
138
            ->setPosition(0, 28)
139
            ->setSize(100, 0.5)
140
            ->setAlign("center", "center")
141
            ->setBackgroundColor("fff");
142
        $bgFrame->addChild($quad);
143
144
        $quad = Quad::create();
145
        $quad->addClass("bg")
146
            ->setPosition(0, 0)
147
            ->setSize(322, 182)
148
            ->setAlign("center", "center")
149
            ->setStyles("Bgs1", "BgDialogBlur");
150
        $bgFrame->addChild($quad);
151
152
153
154
        $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...
155
        $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...
156
157
        $manialink->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 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...
158
    }
159
160
    /**
161
     * @param Widget $manialink
162
     */
163
    protected function updateContent(ManialinkInterface $manialink)
164
    {
165
        // Do stuff Here.
166
    }
167
168
}
169