Completed
Push — master ( c4e200...4a09e4 )
by Níckolas Daniel
05:14 queued 10s
created

Post::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 27
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 11

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 PODEntender\Domain\Model\Post;
4
5
use DateTimeInterface;
6
7
class Post
8
{
9
    private $guid;
10
11
    private $url;
12
13
    private $title;
14
15
    private $description;
16
17
    private $author;
18
19
    private $content;
20
21
    private $category;
22
23
    private $images;
24
25
    private $tags;
26
27
    private $createdAt;
28
29
    private $updatedAt;
30
31
    private $recommended;
32
33
    private $redirects;
34
35
    public function __construct(
36
        string $guid,
37
        string $url,
38
        string $title,
39
        string $description,
40
        string $author,
41
        string $content,
42
        string $category,
43
        PostImageCollection $images,
44
        array $tags,
45
        DateTimeInterface $createdAt,
46
        DateTimeInterface $updatedAt
47
    ) {
48
        $this->guid = $guid;
49
        $this->url = $url;
50
        $this->title = $title;
51
        $this->description = $description;
52
        $this->author = $author;
53
        $this->content = $content;
54
        $this->category = $category;
55
        $this->images = $images;
56
        $this->tags = $tags;
57
        $this->createdAt = $createdAt;
58
        $this->updatedAt = $updatedAt;
59
60
        $this->recommended = new PostCollection();
61
        $this->redirects = [];
62
    }
63
64
    public function guid(): string
65
    {
66
        return $this->guid;
67
    }
68
69
    public function url(): string
70
    {
71
        return $this->url;
72
    }
73
74
    public function title(): string
75
    {
76
        return $this->title;
77
    }
78
79
    public function description(): string
80
    {
81
        return $this->description;
82
    }
83
84
    public function author(): string
85
    {
86
        return $this->author;
87
    }
88
89
    public function content(): string
90
    {
91
        return $this->content;
92
    }
93
94
    public function category(): string
95
    {
96
        return $this->category;
97
    }
98
99
    public function images(): PostImageCollection
100
    {
101
        return $this->images;
102
    }
103
104
    public function cover(): string
105
    {
106
        return $this->images->offsetGet(0)->url();
107
    }
108
109
    // @todo -> create a proper TagCollection and Tag entity
110
    public function tags(): array
111
    {
112
        return $this->tags;
113
    }
114
115
    public function createdAt(): DateTimeInterface
116
    {
117
        return $this->createdAt;
118
    }
119
120
    public function updatedAt(): DateTimeInterface
121
    {
122
        return $this->updatedAt;
123
    }
124
125
    public function recommended(): PostCollection
126
    {
127
        return $this->recommended;
128
    }
129
130
    public function addRecommendations(PostCollection $recommendations): self
131
    {
132
        $newSelf = clone $this;
133
        $newSelf->recommended = $recommendations;
134
135
        return $newSelf;
136
    }
137
138
    public function redirects(): array
139
    {
140
        return $this->redirects ?? [];
141
    }
142
143
    public function addRedirect(string $from): self
144
    {
145
        $this->redirects[] = $from;
146
147
        return $this;
148
    }
149
}
150