Completed
Push — main ( fffb13...d8afdd )
by Laurent
139:07 queued 124:29
created

Article::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 8

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
declare(strict_types=1);
4
5
namespace Domain\Model\Article;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Domain\Model\Article\Entities\ZoneStorage;
9
use Domain\Model\Article\VO\Packaging;
10
use Domain\Model\Common\Entities\FamilyLog;
11
use Domain\Model\Common\Entities\Taxes;
12
use Domain\Model\Common\VO\NameField;
13
use Domain\Model\Supplier\Supplier;
14
15
/**
16
 * Article.
17
 *
18
 * @category Entity
19
 */
20
class Article
21
{
22
    /**
23
     * @var int
24
     */
25
    protected $articleId;
26
27
    /**
28
     * @var string Name of article
29
     */
30
    protected $name;
31
32
    /**
33
     * @var string Name of supplier
34
     */
35
    protected $supplier;
36
37
    /**
38
     * @var array Packaging (subdivision of parcel)
39
     */
40
    protected $packaging;
41
42
    /**
43
     * @var float Price of article
44
     */
45
    protected $price;
46
47
    /**
48
     * @var string Rate of VAT
49
     */
50
    protected $taxes;
51
52
    /**
53
     * @var float Quantity in stock
54
     */
55
    protected $quantity;
56
57
    /**
58
     * @var float Minimum stock
59
     */
60
    protected $minStock;
61
62
    /**
63
     * @var ArrayCollection Storage area(s)
64
     */
65
    protected $zoneStorages;
66
67
    /**
68
     * @var string Logistics family
69
     */
70
    protected $familyLog;
71
72
    /**
73
     * @var bool Active/Inactive
74
     */
75
    protected $active;
76
77
    /**
78
     * @var string
79
     */
80
    protected $slug;
81
82
    /**
83
     * Article constructor.
84
     *
85
     * @param NameField  $name
86
     * @param Supplier   $supplier
87
     * @param Packaging  $packaging
88
     * @param float      $price
89
     * @param Taxes      $taxes
90
     * @param float      $minStock
91
     * @param array      $zoneStorages
92
     * @param FamilyLog  $familyLog
93
     * @param float|null $quantity
94
     * @param bool|null  $active
95
     */
96
    public function __construct(
97
        NameField $name,
98
        Supplier $supplier,
99
        Packaging $packaging,
100
        float $price,
101
        Taxes $taxes,
102
        float $minStock,
103
        array $zoneStorages,
104
        FamilyLog $familyLog,
105
        ?float $quantity = 0.000,
106
        ?bool $active = true
107
    ) {
108
        $this->name = $name->getValue();
109
        $this->supplier = $supplier->name();
110
        $this->packaging = $packaging;
0 ignored issues
show
Documentation Bug introduced by
It seems like $packaging of type object<Domain\Model\Article\VO\Packaging> is incompatible with the declared type array of property $packaging.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
        $this->price = $price;
112
        $this->taxes = $taxes->name();
113
        $this->quantity = $quantity;
114
        $this->minStock = $minStock;
115
        $this->zoneStorages = new ArrayCollection($this->makeZoneStorageEntities($zoneStorages));
116
        $this->familyLog = $familyLog->path();
117
        $this->active = $active;
118
        $this->slug = $name->slugify();
119
    }
120
121
    /**
122
     * Create an Article.
123
     *
124
     * @param NameField $name
125
     * @param Supplier  $supplier
126
     * @param Packaging $packaging
127
     * @param float     $price
128
     * @param Taxes     $taxes
129
     * @param float     $minStock
130
     * @param array     $zoneStorages
131
     * @param FamilyLog $familyLog
132
     *
133
     * @return Article
134
     */
135
    public static function create(
136
        NameField $name,
137
        Supplier $supplier,
138
        Packaging $packaging,
139
        float $price,
140
        Taxes $taxes,
141
        float $minStock,
142
        array $zoneStorages,
143
        FamilyLog $familyLog
144
    ): self {
145
        return new self(
146
            $name,
147
            $supplier,
148
            $packaging,
149
            $price,
150
            $taxes,
151
            $minStock,
152
            $zoneStorages,
153
            $familyLog
154
        );
155
    }
156
157
    final public function renameArticle(NameField $name): void
158
    {
159
        $this->name = $name->getValue();
160
        $this->slug = $name->slugify();
161
    }
162
163
    /**
164
     * @param array $zoneStorages
165
     *
166
     * @return ZoneStorage[]
167
     */
168
    private function makeZoneStorageEntities(array $zoneStorages): array
169
    {
170
        return array_map(static function ($zone) {
171
            return new ZoneStorage(NameField::fromString($zone));
172
        }, $zoneStorages);
173
    }
174
}
175