Item::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 10

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 Rottenwood\KingdomBundle\Entity\Infrastructure;
4
5
use Doctrine\ORM\Mapping as ORM;
6
7
/**
8
 * Игровой предмет
9
 * @ORM\Table(name="items")
10
 * @ORM\Entity(repositoryClass="Rottenwood\KingdomBundle\Entity\Infrastructure\ItemRepository")
11
 * @ORM\InheritanceType("SINGLE_TABLE")
12
 * @ORM\DiscriminatorColumn(name="type", type="string")
13
 * @ORM\DiscriminatorMap({
14
 *      "armor" = "Rottenwood\KingdomBundle\Entity\Items\Armor",
15
 *      "clothes" = "Rottenwood\KingdomBundle\Entity\Items\Clothes",
16
 *      "food" = "Rottenwood\KingdomBundle\Entity\Items\Food",
17
 *      "key" = "Rottenwood\KingdomBundle\Entity\Items\Key",
18
 *      "resource_wood" = "Rottenwood\KingdomBundle\Entity\Items\ResourceWood",
19
 *      "ring" = "Rottenwood\KingdomBundle\Entity\Items\Ring",
20
 *      "scroll" = "Rottenwood\KingdomBundle\Entity\Items\Scroll",
21
 *      "shield" = "Rottenwood\KingdomBundle\Entity\Items\Shield",
22
 *      "weapon" = "Rottenwood\KingdomBundle\Entity\Items\Weapon",
23
 * })
24
 */
25
abstract class Item
26
{
27
28
    const USER_SLOT_HEAD = 'head';
29
    const USER_SLOT_AMULET = 'amulet';
30
    const USER_SLOT_BODY = 'body';
31
    const USER_SLOT_CLOAK = 'cloak';
32
    const USER_SLOT_WEAPON = 'weapon';
33
    const USER_SLOT_LEFT_HAND = 'left_hand';
34
    const USER_SLOT_GLOVES = 'gloves';
35
    const USER_SLOT_RING_FIRST = 'ring_first';
36
    const USER_SLOT_RING_SECOND = 'ring_second';
37
    const USER_SLOT_LEGS = 'legs';
38
    const USER_SLOT_BOOTS = 'boots';
39
40
    /**
41
     * @var string
42
     * @ORM\Column(name="id", type="string", length=100)
43
     * @ORM\Id
44
     */
45
    protected $id;
46
47
    /**
48
     * Название в именительном падеже
49
     * @var string
50
     * @ORM\Column(name="name", type="string", length=255)
51
     */
52
    protected $name;
53
54
    /**
55
     * Название в родительном падеже
56
     * @var string
57
     * @ORM\Column(name="name_2", type="string", length=255)
58
     */
59
    protected $name2;
60
61
    /**
62
     * Название в дательном падеже
63
     * @var string
64
     * @ORM\Column(name="name_3", type="string", length=255)
65
     */
66
    protected $name3;
67
68
    /**
69
     * Название в винительном падеже
70
     * @var string
71
     * @ORM\Column(name="name_4", type="string", length=255)
72
     */
73
    protected $name4;
74
75
    /**
76
     * Название в творительном падеже
77
     * @var string
78
     * @ORM\Column(name="name_5", type="string", length=255)
79
     */
80
    protected $name5;
81
82
    /**
83
     * Название в предложном падеже
84
     * @var string
85
     * @ORM\Column(name="name_6", type="string", length=255)
86
     */
87
    protected $name6;
88
89
    /**
90
     * Описание предмета
91
     * @var string
92
     * @ORM\Column(name="description", type="text")
93
     */
94
    protected $description;
95
96
    /**
97
     * Слоты куда можно одеть предмет
98
     * @var string[]
99
     * @ORM\Column(name="slots", type="simple_array", nullable=true)
100
     */
101
    protected $slots;
102
103
    /**
104
     * Название изображения предмета
105
     * @var string
106
     * @ORM\Column(name="picture", type="string", length=255)
107
     */
108
    protected $picture;
109
110
    /**
111
     * @param string   $id
112
     * @param string   $name
113
     * @param string   $name2
114
     * @param string   $name3
115
     * @param string   $name4
116
     * @param string   $name5
117
     * @param string   $name6
118
     * @param string   $description
119
     * @param string   $picture
120
     * @param string[] $slots
121
     */
122
    public function __construct(
123
        $id,
124
        $name,
125
        $name2,
126
        $name3,
127
        $name4,
128
        $name5,
129
        $name6,
130
        $description,
131
        $picture,
132
        array $slots = []
133
    ) {
134
        $this->id = $id;
135
        $this->name = $name;
136
        $this->name2 = $name2;
137
        $this->name3 = $name3;
138
        $this->name4 = $name4;
139
        $this->name5 = $name5;
140
        $this->name6 = $name6;
141
        $this->description = $description;
142
        $this->picture = $picture;
143
        $this->slots = $slots;
144
    }
145
146
    /**
147
     * Названия всех слотов
148
     * @return string[]
149
     */
150
    public static function getAllSlotNames()
151
    {
152
        return [
153
            self::USER_SLOT_HEAD,
154
            self::USER_SLOT_AMULET,
155
            self::USER_SLOT_BODY,
156
            self::USER_SLOT_CLOAK,
157
            self::USER_SLOT_WEAPON,
158
            self::USER_SLOT_LEFT_HAND,
159
            self::USER_SLOT_GLOVES,
160
            self::USER_SLOT_RING_FIRST,
161
            self::USER_SLOT_RING_SECOND,
162
            self::USER_SLOT_LEGS,
163
            self::USER_SLOT_BOOTS,
164
        ];
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getId()
171
    {
172
        return $this->id;
173
    }
174
175
    /**
176
     * Get name
177
     * @return string
178
     */
179
    public function getName()
180
    {
181
        return $this->name;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getName2()
188
    {
189
        return $this->name2;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getName3()
196
    {
197
        return $this->name3;
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getName4()
204
    {
205
        return $this->name4;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getName5()
212
    {
213
        return $this->name5;
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    public function getName6()
220
    {
221
        return $this->name6;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getDescription()
228
    {
229
        return $this->description;
230
    }
231
232
    /**
233
     * @return string[]
234
     */
235
    public function getSlots()
236
    {
237
        return $this->slots;
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function getPicture()
244
    {
245
        return $this->picture;
246
    }
247
248
    /**
249
     * Является ли предмет оружием
250
     * @return bool
251
     */
252
    public function isWeapon()
253
    {
254
        return $this->fitsTo(self::USER_SLOT_WEAPON);
255
    }
256
257
    /**
258
     * Подходит ли предмет в соответствующий слот
259
     * @param int $slotName
260
     * @return bool
261
     */
262
    public function fitsTo($slotName)
263
    {
264
        return in_array($slotName, $this->slots);
265
    }
266
}
267