Completed
Pull Request — master (#28)
by De Cramer
02:19
created

ManialinkFactory::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
ccs 14
cts 14
cp 1
cc 3
eloc 21
nc 4
nop 8
crap 3

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\Core\Plugins\Gui;
4
5
use eXpansion\Core\Model\Gui\Manialink;
6
use eXpansion\Core\Model\Gui\ManialinkInerface;
7
use eXpansion\Core\Model\UserGroups\Group;
8
use eXpansion\Core\Plugins\GuiHandler;
9
use eXpansion\Core\Plugins\UserGroups\Factory;
10
11
/**
12
 * Class ManialiveFactory allow the creation of manialinks.
13
 *
14
 * @package eXpansion\Core\Plugins\Gui
15
 * @author Oliver de Cramer
16
 */
17
class ManialinkFactory
18
{
19
    /** @var  GuiHandler */
20
    protected $guiHandler;
21
22
    /** @var Factory  */
23
    protected $groupFactory;
24
25
    /** @var  string */
26
    protected $name;
27
28
    /** @var  string */
29
    protected $className;
30
31
    /** @var ManialinkInerface[]  */
32
    protected $manialinks = [];
33
34
    /** @var Group[] */
35
    protected $groups = [];
36
37
    /** @var float */
38
    protected $sizeX;
39
40
    /** @var float */
41
    protected $sizeY;
42
43
    /** @var float */
44
    protected $posX;
45
46
    /** @var float */
47
    protected $posY;
48
49
    /**
50
     * GroupManialinkFactory constructor.
51
     *
52
     * @param GuiHandler $guiHandler
53
     * @param $name
54
     * @param $sizeX
55
     * @param $sizeY
56
     * @param null $posX
57
     * @param null $posY
58
     * @param string $className
59
     */
60 6
    public function __construct(
61
        GuiHandler $guiHandler,
62
        Factory $groupFactory,
63
        $name,
64
        $sizeX,
65
        $sizeY,
66
        $posX = null,
67
        $posY = null,
68
        $className = Manialink::class
69
    ) {
70 6
        if (is_null($posX)) {
71 6
            $posX = $sizeX/-2;
72
        }
73
74 6
        if (is_null($posY)) {
75 6
            $posY = $sizeY/2;
76
        }
77
78 6
        $this->guiHandler = $guiHandler;
79 6
        $this->groupFactory = $groupFactory;
80 6
        $this->name = $name;
81 6
        $this->className = $className;
82 6
        $this->sizeX = $sizeX;
83 6
        $this->sizeY = $sizeY;
84 6
        $this->posX = $posX;
0 ignored issues
show
Documentation Bug introduced by
It seems like $posX can also be of type integer. However, the property $posX is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
85 6
        $this->posY = $posY;
0 ignored issues
show
Documentation Bug introduced by
It seems like $posY can also be of type integer. However, the property $posY is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86 6
    }
87
88
    /**
89
     * Create & display manialink for user.
90
     *
91
     * @param Group|string|string[] $group
92
     *
93
     * @return Group
94
     */
95 5
    public function create($group)
96
    {
97 5
        if (is_string($group)) {
98 1
            $group = $this->groupFactory->createForPlayer($group);
99 4
        } else if (is_array($group)) {
100 3
            $group = $this->groupFactory->createForPlayers($group);
101
        }
102
103 5
        $this->manialinks[$group->getName()] = $this->createManialink($group);
104 5
        $this->guiHandler->addToDisplay($this->manialinks[$group->getName()]);
105
106 5
        return $group;
107
    }
108
109
    /**
110
     * Hide & destoy manialink fr user.
111
     *
112
     * @param Group $group
113
     *
114
     * @return void
115
     */
116 1
    public function destroy(Group $group)
117
    {
118 1
        if (isset($this->manialinks[$group->getName()])) {
119 1
            $this->guiHandler->addToHide($this->manialinks[$group->getName()]);
120 1
            unset($this->manialinks[$group->getName()]);
121
        }
122 1
    }
123
124
    /**
125
     * Create manialink object for user group.
126
     *
127
     * @param Group $group
128
     *
129
     * @return mixed
130
     */
131 5
    protected function createManialink(Group $group)
132
    {
133 5
        $className = $this->className;
134 5
        return new $className($group, $this->name, $this->sizeX, $this->sizeY, $this->posX, $this->posY);
135
    }
136
137
    /**
138
     * When a gup is destroyed, destroy manialinks of the group.
139
     *
140
     * @param Group $group
141
     * @param $lastLogin
142
     *
143
     * @return void
144
     */
145 1
    public function onExpansionGroupDestroy(Group $group, $lastLogin)
0 ignored issues
show
Unused Code introduced by
The parameter $lastLogin is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
    {
147 1
        if (isset($this->manialinks[$group->getName()])) {
148
            // Gui Handler will handle delete by it's own.
149 1
            unset($this->manialinks[$group->getName()]);
150
        }
151 1
    }
152
153 1
    public function onExpansionGroupAddUser(Group $group, $loginAdded)
0 ignored issues
show
Unused Code introduced by
The parameter $group is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $loginAdded is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    {
155
        // nothing to do here.
156 1
    }
157
158 1
    public function onExpansionGroupRemoveUser(Group $group, $loginRemoved)
0 ignored issues
show
Unused Code introduced by
The parameter $group is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $loginRemoved is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        // nothing to do here.
161 1
    }
162
}
163