Completed
Pull Request — master (#152)
by
unknown
02:44
created

VoteWidgetFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 12
nc 1
nop 8
crap 2

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\VoteManager\Plugins\Gui\Widget;
4
5
use eXpansion\Bundle\VoteManager\Services\VoteService;
6
use eXpansion\Bundle\VoteManager\Structures\Vote;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
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 eXpansion\Framework\Gui\Builders\WidgetBackground;
12
use eXpansion\Framework\Gui\Components\uiButton;
13
use eXpansion\Framework\Gui\Components\uiLabel;
14
use eXpansion\Framework\Gui\Ui\Factory;
15
use FML\Controls\Quad;
16
use FML\Script\ScriptLabel;
17
18
class VoteWidgetFactory extends WidgetFactory
19
{
20
21
    const x = 90;
22
    const y = 20;
23
    /** @var uiLabel */
24
    protected $label;
25
    /**
26
     * @var VoteService
27
     */
28
    private $voteService;
29
30
31
    /***
32
     * MenuFactory constructor.
33
     *
34
     * @param $name
35
     * @param $sizeX
36
     * @param $sizeY
37
     * @param null $posX
38
     * @param null $posY
39
     * @param WidgetFactoryContext $context
40
     * @param Factory $uiFactory
41
     * @param VoteService $voteService
42
     */
43
    public function __construct(
44
        $name,
45
        $sizeX,
46
        $sizeY,
47
        $posX,
48
        $posY,
49
        WidgetFactoryContext $context,
50
        Factory $uiFactory,
51
        VoteService $voteService
52
    ) {
53
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
54
55
        $this->uiFactory = $uiFactory;
56
        $this->voteService = $voteService;
57
    }
58
59
    /**
60
     * @param Widget|ManialinkInterface $manialink
61
     */
62
    protected function createContent(ManialinkInterface $manialink)
63
    {
64
        $label = $this->uiFactory->createLabel("Unknown Vote", UiLabel::TYPE_HEADER);
65
        $label->setTextColor("fff")
66
            ->setPosition(self::x / 2, -1)
67
            ->setTextSize(4)
68
            ->setAlign("center", "top");
69
        $this->label = $label;
70
        $manialink->addChild($this->label);
71
72
        $btnPosition = -9;
73
        $btn = $this->uiFactory->createButton(" F1", UiButton::TYPE_DEFAULT);
74
        $btn->setSize(18, 6)->setPosition(1, $btnPosition)
75
            ->setBackgroundColor("0f09");
76
        $btn->setAction(
77
            $this->actionFactory->createManialinkAction($manialink, [$this, "callbackYes"], null)
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
78
        );
79
        $manialink->addChild($btn);
80
81
        $btn = $this->uiFactory->createButton(" F2", UiButton::TYPE_DEFAULT);
82
        $btn->setSize(18, 6)->setPosition(self::x - 19, $btnPosition)
83
            ->setBackgroundColor("f009");
84
        $btn->setAction(
85
            $this->actionFactory->createManialinkAction($manialink, [$this, "callbackNo"], null)
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
86
        );
87
        $manialink->addChild($btn);
88
89
        $quad = Quad::create();
90
        $quad->setAlign("center", "center2");
91
        $quad->setSize(0.5, 9);
92
        $quad->setPosition(self::x / 2, $btnPosition - 3)
93
            ->setBackgroundColor("fff");
94
        $manialink->addChild($quad);
95
96
        $quad = Quad::create("yes");
97
        $quad->setAlign("left", "top");
98
        $quad->setSize((self::x - 20 * 2) / 2, 6);
99
        $quad->setPosition(20, $btnPosition)
100
            ->setBackgroundColor("0f09");
101
        $manialink->addChild($quad);
102
103
        $quad = Quad::create("no");
104
        $quad->setAlign("right", "top");
105
        $quad->setSize((self::x - 20 * 2) / 2, 6);
106
        $quad->setPosition(self::x - 20, $btnPosition)
107
            ->setBackgroundColor("f009");
108
        $manialink->addChild($quad);
109
110
        $quad = Quad::create("timer");
111
        $quad->setSize(self::x - 4, 1);
112
        $quad->setPosition(2, -self::y + 1)
113
            ->setAlign("left", "bottom")
114
            ->setBackgroundColor("fffa");
115
        $manialink->addChild($quad);
116
117
        $bg = new WidgetBackground(90, 20);
118
        $manialink->addChild($bg);
119
120
        $x = self::x;
121
122
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::OnInit,
123
            <<<EOL
124
            declare Real SizeX = 1. * ($x - 40) ;
125
            declare CMlQuad BgYes = (Page.GetFirstChild("yes") as CMlQuad);
126
            declare CMlQuad BgNo = (Page.GetFirstChild("no") as CMlQuad);
127
            declare CMlQuad Timer = (Page.GetFirstChild("timer") as CMlQuad);
128
            declare Real Exp_Vote_Yes for This = 1.;
129
            declare Real Exp_Vote_No for This = 0.;
130
            declare Real Exp_Vote_TimeElapsed for This = 1.;
131
            declare Real Exp_Vote_TimeTotal for This = 30.;
132
            declare Text Exp_Vote_check for This = "";
133
            declare Text Exp_Vote_oldCheck = "";                                                                                                       
134
EOL
135
        );
136
137
        $manialink->getFmlManialink()->getScript()->addCustomScriptLabel(ScriptLabel::Loop,
138
            <<<EOL
139
            if (Exp_Vote_check != Exp_Vote_oldCheck) {  
140
               Exp_Vote_oldCheck = Exp_Vote_check;          
141
               declare Real Total = (Exp_Vote_Yes + Exp_Vote_No);
142
               
143
               if (Total > 0) {
144
                    declare Real Ratio = 1. * (Exp_Vote_Yes / Total);
145
                    BgYes.Size.X = SizeX  * Ratio ;
146
                    BgNo.Size.X = SizeX  * (1. - Ratio);
147
               }
148
               
149
               Timer.Size.X = 86. *((Exp_Vote_TimeTotal - Exp_Vote_TimeElapsed) / Exp_Vote_TimeTotal);
150
            }                                                                            
151
EOL
152
        );
153
154
155
    }
156
157
    public function setMessage($message)
158
    {
159
        $this->label->setText($message);
160
    }
161
162
163
    protected function updateContent(ManialinkInterface $manialink)
164
    {
165
        parent::updateContent($manialink); // TODO: Change the autogenerated stub
166
    }
167
168
    public function callbackYes($manialink, $login, $entries, $args)
169
    {
170
        if ($this->voteService->getCurrentVote() instanceof Vote) {
171
            $this->voteService->getCurrentVote()->castYes($login);
172
        }
173
174
    }
175
176
    public function callbackNo($manialink, $login, $entries, $args)
177
    {
178
        if ($this->voteService->getCurrentVote()) {
179
            $this->voteService->getCurrentVote()->castNo($login);
180
        }
181
    }
182
183
}
184