Context::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 20
rs 9.9666

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\Model;
5
6
7
use App\Src\UseCases\Domain\Ports\ContextRepository;
8
9
class Context
10
{
11
    private $uid;
12
    private $postalCode;
13
    private $characteristics;
14
    private $description;
15
    private $sector;
16
    private $structure;
17
    private $departmentNumber;
18
    private $coordinates;
19
20
    private $contextRepository;
21
22
    public function __construct(
23
        string $id,
24
        string $postalCode,
25
        array $characteristics = [],
26
        string $description = null,
27
        string $sector = null,
28
        string $structure = null,
29
        string $departmentNumber = null,
30
        array $coordinates = []
31
    )
32
    {
33
        $this->uid = $id;
34
        $this->postalCode = $postalCode;
35
        $this->characteristics = $characteristics;
36
        $this->description = $description;
37
        $this->sector = $sector;
38
        $this->structure = $structure;
39
        $this->departmentNumber = $departmentNumber;
40
        $this->coordinates = $coordinates;
41
        $this->contextRepository = app(ContextRepository::class);
42
    }
43
44
    public function id():string
45
    {
46
        return $this->uid;
47
    }
48
49
    public function create(string $userId)
50
    {
51
        $this->contextRepository->add($this, $userId);
52
    }
53
54
    public function update(array $params, string $userId)
55
    {
56
        $this->description = $params['description'] ?? $this->description;
57
        $this->postalCode = $params['postal_code'] ?? $this->postalCode;
58
        $this->characteristics = $params['characteristics'] ?? $this->characteristics;
59
        $this->sector = $params['sector'] ?? $this->sector;
60
        $this->structure = $params['structure'] ?? $this->structure;
61
        $this->departmentNumber = $params['department_number'] ?? $this->departmentNumber;
62
        $this->coordinates = $params['coordinates'] ?? $this->coordinates;
63
        $this->contextRepository->update($this, $userId);
64
    }
65
66
    public function addCharacteristics(array $characteristics, string $userId)
67
    {
68
        $this->characteristics = array_values(array_unique(array_merge($this->characteristics, $characteristics)));
69
        $this->contextRepository->update($this, $userId);
70
    }
71
72
    public function toArray()
73
    {
74
        return [
75
            'uuid' => $this->uid,
76
            'postal_code' => $this->postalCode,
77
            'characteristics' => $this->characteristics,
78
            'description' => $this->description,
79
            'sector' => $this->sector,
80
            'structure' => $this->structure,
81
            'coordinates' => $this->coordinates,
82
            'department_number' => $this->departmentNumber,
83
        ];
84
    }
85
}
86