BillWindow::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 16
cp 0
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 9
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
 * Created by PhpStorm.
4
 * User: php_r
5
 * Date: 25.2.2018
6
 * Time: 19.52
7
 */
8
9
namespace eXpansion\Framework\GameCurrencyBundle\Plugins\Gui;
10
11
12
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
13
use eXpansion\Framework\Core\Model\Gui\Window;
14
use eXpansion\Framework\Core\Model\Gui\WindowFactoryContext;
15
use eXpansion\Framework\Core\Plugins\Gui\WindowFactory;
16
use eXpansion\Framework\Core\Storage\GameDataStorage;
17
use eXpansion\Framework\GameCurrencyBundle\Services\GameCurrencyService;
18
use eXpansion\Framework\Notifications\Services\Notifications;
19
20
class BillWindow extends WindowFactory
21
{
22
    /**
23
     * @var GameCurrencyService
24
     */
25
    private $currencyService;
26
    /**
27
     * @var GameDataStorage
28
     */
29
    private $gameDataStorage;
30
    private $recipient = "";
31
    private $amount = "";
32
    /**
33
     * @var Notifications
34
     */
35
    private $notifications;
36
37
38
    /**
39
     * BillWindow constructor.
40
     * @param                      $name
41
     * @param                      $sizeX
42
     * @param                      $sizeY
43
     * @param null                 $posX
44
     * @param null                 $posY
45
     * @param WindowFactoryContext $context
46
     * @param GameCurrencyService  $currencyService
47
     * @param GameDataStorage      $gameDataStorage
48
     * @param Notifications        $notifications
49
     */
50
    public function __construct(
51
        $name,
52
        $sizeX,
53
        $sizeY,
54
        $posX = null,
55
        $posY = null,
56
        WindowFactoryContext $context,
57
        GameCurrencyService $currencyService,
58
        GameDataStorage $gameDataStorage,
59
        Notifications $notifications
60
    ) {
61
        parent::__construct($name, $sizeX, $sizeY, $posX, $posY, $context);
62
        $this->currencyService = $currencyService;
63
        $this->gameDataStorage = $gameDataStorage;
64
        $this->notifications = $notifications;
65
    }
66
67
    public function setDetails($login, $amount)
68
    {
69
        $this->recipient = $login;
70
        $this->amount = $amount;
71
    }
72
73
    protected function createContent(ManialinkInterface $manialink)
74
    {
75
76
        $column1 = $this->uiFactory->createLayoutRow(0, 0, [], 1);
77
        $column1->addChildren([
78
            $this->uiFactory->createLabel("Recipient"),
79
            $this->uiFactory->createInput("login")->setDefault($this->recipient),
80
            $this->uiFactory->createLabel("Amount"),
81
            $this->uiFactory->createInput("amount")->setDefault($this->amount),
82
        ]);
83
84
85
        $actions = $this->uiFactory->createLayoutLine(0, 0, [], 3);
86
        $actions->addChildren([
87
            $this->uiFactory->createButton("Send")->setAction(
88
                $this->actionFactory->createManialinkAction($manialink, [$this, "callbackSend"], 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...
89
            ),
90
            $this->uiFactory->createButton("Cancel")->setAction(
91
                $this->actionFactory->createManialinkAction($manialink, [$this, "callbackCancel"], 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...
92
            ),
93
        ]);
94
95
96
        $column2 = $this->uiFactory->createLayoutRow(0, 0, [], 1);
97
        $column2->addChildren([
98
            $this->uiFactory->createLabel("Message"),
99
            $this->uiFactory->createTextbox("message", "...", 3)
100
                ->setWidth(50),
101
            $actions,
102
        ]);
103
104
        $manialink->addChild($this->uiFactory->createLayoutLine(0, 0, [$column1, $column2], 2));
105
    }
106
107
    /** @param ManialinkInterface|Window $manialink */
108
    public function callbackSend($manialink, $login, $entries, $args)
109
    {
110
        if (!is_numeric($entries['amount'])) {
111
            $this->notifications->error("Amount is not integer", [], "Error", 10500, $manialink->getUserGroup());
112
113
            return;
114
        }
115
116
117
        $serverLogin = $this->gameDataStorage->getSystemInfo()->serverLogin;
118
        $bill = $this->currencyService->createBill(
119
            $serverLogin,
120
            $entries['amount'],
121
            $entries['login'],
122
            $entries['message']
123
        );
124
125
        $this->setBusy($manialink, "Processing...");
126
127
        if ($bill == false) {
128
            $this->notifications->error("Error while processing planets transaction", [], "Error", 10500,
129
                $manialink->getUserGroup());
130
            $this->closeManialink($manialink);
131
        }
132
133
        $this->currencyService->sendBill(
134
            $bill,
135
            function () use ($manialink, $bill) {
136
                $this->notifications->info("Successfully payed ".$bill->getAmount()."p to ".$bill->getReceiverlogin(),
137
                    [], "Success", 3500, $manialink->getUserGroup());
138
                $this->closeManialink($manialink);
139
            },
140
            function ($status) use ($manialink) {
141
                $this->notifications->error("Server said: ".$status,
142
                    [], "Error", 10500, $manialink->getUserGroup());
143
                $this->closeManialink($manialink);
144
            }
145
        );
146
    }
147
148
    /** @param ManialinkInterface|Window $manialink */
149
    public function callbackCancel($manialink, $login, $entries, $args)
150
    {
151
        $this->closeManialink($manialink);
152
    }
153
154
}