ContextDto::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 9
dl 0
loc 30
rs 9.7666

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
4
namespace App\Src\UseCases\Domain\Context\Dto;
5
6
7
use App\Src\UseCases\Domain\Context\Model\Characteristic;
8
use App\Src\UseCases\Domain\Shared\Model\Dto;
9
10
class ContextDto extends Dto
11
{
12
    public $firstname;
13
    public $lastname;
14
    public $postalCode;
15
    public $department;
16
    public $characteristics;
17
    public $productions;
18
    public $characteristicsDepartement;
19
    public $description;
20
    public $sector;
21
    public $structure;
22
    public $userUuid;
23
    public $fullname;
24
25
    public function __construct(
26
        string $firstname,
27
        string $lastname,
28
        string $postalCode,
29
        array $characteristics,
30
        ?string $description,
31
        ?string $sector,
32
        ?string $structure,
33
        ?string $userUuid = null,
34
        string $departmentNumber = null
35
    )
36
    {
37
        $this->firstname = $firstname;
38
        $this->lastname = $lastname;
39
        $this->postalCode = $postalCode;
40
        $this->department = $departmentNumber;
41
        $this->fullname = $this->fullname();
42
        $characteristicsByType = [];
43
        foreach($characteristics as $characteristic){
44
            $characteristicsByType[$characteristic->type()][] = $characteristic;
45
        }
46
47
        $this->characteristicsDepartement = $characteristicsByType[Characteristic::DEPARTMENT] ?? [];
48
        $this->characteristics = $characteristicsByType[Characteristic::CROPPING_SYSTEM] ?? [];
49
        $this->productions = $characteristicsByType[Characteristic::FARMING_TYPE] ?? [];
50
51
        $this->description = $description;
52
        $this->sector = $sector;
53
        $this->structure = $structure;
54
        $this->userUuid = $userUuid;
55
    }
56
57
    private function fullname():string
58
    {
59
        return $this->firstname.' '.$this->lastname;
60
    }
61
62
}
63