Post   D
last analyzed

Complexity

Total Complexity 59

Size/Duplication

Total Lines 373
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
wmc 59
lcom 4
cbo 3
dl 0
loc 373
rs 4.08
c 0
b 0
f 0

49 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 2
A setTitle() 0 6 1
A getTitle() 0 4 1
A setAbstract() 0 4 1
A getAbstract() 0 4 1
A setContent() 0 4 1
A getContent() 0 4 1
A setEnabled() 0 4 1
A getEnabled() 0 4 1
A setSlug() 0 4 1
A getSlug() 0 4 1
A setPublicationDateStart() 0 4 1
A getPublicationDateStart() 0 4 1
A setCreatedAt() 0 4 1
A getCreatedAt() 0 4 1
A setUpdatedAt() 0 4 1
A getUpdatedAt() 0 4 1
A addComments() 0 5 1
A setComments() 0 8 2
A getComments() 0 4 1
A addTags() 0 4 1
A getTags() 0 4 1
A setTags() 0 4 1
A prePersist() 0 9 2
A preUpdate() 0 8 2
A getYear() 0 4 1
A getMonth() 0 4 1
A getDay() 0 4 1
A setCommentsEnabled() 0 4 1
A getCommentsEnabled() 0 4 1
A setCommentsCloseAt() 0 4 1
A getCommentsCloseAt() 0 4 1
A setCommentsDefaultStatus() 0 4 1
A getCommentsDefaultStatus() 0 4 1
A setCommentsCount() 0 4 1
A getCommentsCount() 0 4 1
A isCommentable() 0 12 5
A isPublic() 0 8 3
A setAuthor() 0 4 1
A getAuthor() 0 4 1
A setImage() 0 4 1
A getImage() 0 4 1
A setCollection() 0 4 1
A getCollection() 0 4 1
A setContentFormatter() 0 4 1
A getContentFormatter() 0 4 1
A setRawContent() 0 4 1
A getRawContent() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Post often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Post, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NewsBundle\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sonata\ClassificationBundle\Model\CollectionInterface;
19
use Sonata\ClassificationBundle\Model\Tag;
20
use Sonata\ClassificationBundle\Model\TagInterface;
21
use Sonata\MediaBundle\Model\MediaInterface;
22
use Symfony\Component\Security\Core\User\UserInterface;
23
24
abstract class Post implements PostInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $title;
30
31
    /**
32
     * @var string
33
     */
34
    protected $slug;
35
36
    /**
37
     * @var string
38
     */
39
    protected $abstract;
40
41
    /**
42
     * @var string
43
     */
44
    protected $content;
45
46
    /**
47
     * @var string
48
     */
49
    protected $rawContent;
50
51
    /**
52
     * @var string
53
     */
54
    protected $contentFormatter;
55
56
    /**
57
     * @var Collection|TagInterface[]
58
     */
59
    protected $tags;
60
61
    /**
62
     * @var Collection|CommentInterface[]
63
     */
64
    protected $comments;
65
66
    /**
67
     * @var bool
68
     */
69
    protected $enabled;
70
71
    /**
72
     * @var \DateTime|null
73
     */
74
    protected $publicationDateStart;
75
76
    /**
77
     * @var \DateTime
78
     */
79
    protected $createdAt;
80
81
    /**
82
     * @var \DateTime
83
     */
84
    protected $updatedAt;
85
86
    /**
87
     * @var bool
88
     */
89
    protected $commentsEnabled = true;
90
91
    /**
92
     * @var \DateTime|null
93
     */
94
    protected $commentsCloseAt;
95
96
    /**
97
     * @var int
98
     */
99
    protected $commentsDefaultStatus;
100
101
    /**
102
     * @var int
103
     */
104
    protected $commentsCount = 0;
105
106
    /**
107
     * @var UserInterface|null
108
     */
109
    protected $author;
110
111
    /**
112
     * @var MediaInterface|null
113
     */
114
    protected $image;
115
116
    /**
117
     * @var CollectionInterface|null
118
     */
119
    protected $collection;
120
121
    public function __construct()
122
    {
123
        $this->setPublicationDateStart(new \DateTime());
124
    }
125
126
    public function __toString()
127
    {
128
        return $this->getTitle() ?: 'n/a';
129
    }
130
131
    public function setTitle($title): void
132
    {
133
        $this->title = $title;
134
135
        $this->setSlug(Tag::slugify($title));
136
    }
137
138
    public function getTitle()
139
    {
140
        return $this->title;
141
    }
142
143
    public function setAbstract($abstract): void
144
    {
145
        $this->abstract = $abstract;
146
    }
147
148
    public function getAbstract()
149
    {
150
        return $this->abstract;
151
    }
152
153
    public function setContent($content): void
154
    {
155
        $this->content = $content;
156
    }
157
158
    public function getContent()
159
    {
160
        return $this->content;
161
    }
162
163
    public function setEnabled($enabled): void
164
    {
165
        $this->enabled = $enabled;
166
    }
167
168
    public function getEnabled()
169
    {
170
        return $this->enabled;
171
    }
172
173
    public function setSlug($slug): void
174
    {
175
        $this->slug = $slug;
176
    }
177
178
    public function getSlug()
179
    {
180
        return $this->slug;
181
    }
182
183
    public function setPublicationDateStart(?\DateTime $publicationDateStart = null): void
184
    {
185
        $this->publicationDateStart = $publicationDateStart;
186
    }
187
188
    public function getPublicationDateStart()
189
    {
190
        return $this->publicationDateStart;
191
    }
192
193
    public function setCreatedAt(?\DateTime $createdAt = null): void
194
    {
195
        $this->createdAt = $createdAt;
196
    }
197
198
    public function getCreatedAt()
199
    {
200
        return $this->createdAt;
201
    }
202
203
    public function setUpdatedAt(?\DateTime $updatedAt = null): void
204
    {
205
        $this->updatedAt = $updatedAt;
206
    }
207
208
    public function getUpdatedAt()
209
    {
210
        return $this->updatedAt;
211
    }
212
213
    public function addComments(CommentInterface $comment): void
214
    {
215
        $this->comments[] = $comment;
216
        $comment->setPost($this);
217
    }
218
219
    public function setComments($comments): void
220
    {
221
        $this->comments = new ArrayCollection();
222
223
        foreach ($this->comments as $comment) {
224
            $this->addComments($comment);
225
        }
226
    }
227
228
    public function getComments()
229
    {
230
        return $this->comments;
231
    }
232
233
    public function addTags(TagInterface $tags): void
234
    {
235
        $this->tags[] = $tags;
236
    }
237
238
    public function getTags()
239
    {
240
        return $this->tags;
241
    }
242
243
    public function setTags($tags): void
244
    {
245
        $this->tags = $tags;
246
    }
247
248
    public function prePersist(): void
249
    {
250
        if (!$this->getPublicationDateStart()) {
251
            $this->setPublicationDateStart(new \DateTime());
252
        }
253
254
        $this->setCreatedAt(new \DateTime());
255
        $this->setUpdatedAt(new \DateTime());
256
    }
257
258
    public function preUpdate(): void
259
    {
260
        if (!$this->getPublicationDateStart()) {
261
            $this->setPublicationDateStart(new \DateTime());
262
        }
263
264
        $this->setUpdatedAt(new \DateTime());
265
    }
266
267
    public function getYear()
268
    {
269
        return $this->getPublicationDateStart()->format('Y');
270
    }
271
272
    public function getMonth()
273
    {
274
        return $this->getPublicationDateStart()->format('m');
275
    }
276
277
    public function getDay()
278
    {
279
        return $this->getPublicationDateStart()->format('d');
280
    }
281
282
    public function setCommentsEnabled($commentsEnabled): void
283
    {
284
        $this->commentsEnabled = $commentsEnabled;
285
    }
286
287
    public function getCommentsEnabled()
288
    {
289
        return $this->commentsEnabled;
290
    }
291
292
    public function setCommentsCloseAt(?\DateTime $commentsCloseAt = null): void
293
    {
294
        $this->commentsCloseAt = $commentsCloseAt;
295
    }
296
297
    public function getCommentsCloseAt()
298
    {
299
        return $this->commentsCloseAt;
300
    }
301
302
    public function setCommentsDefaultStatus($commentsDefaultStatus): void
303
    {
304
        $this->commentsDefaultStatus = $commentsDefaultStatus;
305
    }
306
307
    public function getCommentsDefaultStatus()
308
    {
309
        return $this->commentsDefaultStatus;
310
    }
311
312
    public function setCommentsCount($commentsCount): void
313
    {
314
        $this->commentsCount = $commentsCount;
315
    }
316
317
    public function getCommentsCount()
318
    {
319
        return $this->commentsCount;
320
    }
321
322
    public function isCommentable()
323
    {
324
        if (!$this->getCommentsEnabled() || !$this->getEnabled()) {
325
            return false;
326
        }
327
328
        if ($this->getCommentsCloseAt() instanceof \DateTime) {
329
            return 1 === $this->getCommentsCloseAt()->diff(new \DateTime())->invert ? true : false;
330
        }
331
332
        return true;
333
    }
334
335
    public function isPublic()
336
    {
337
        if (!$this->getEnabled()) {
338
            return false;
339
        }
340
341
        return 0 === $this->getPublicationDateStart()->diff(new \DateTime())->invert ? true : false;
342
    }
343
344
    public function setAuthor($author): void
345
    {
346
        $this->author = $author;
347
    }
348
349
    public function getAuthor()
350
    {
351
        return $this->author;
352
    }
353
354
    public function setImage($image): void
355
    {
356
        $this->image = $image;
357
    }
358
359
    public function getImage()
360
    {
361
        return $this->image;
362
    }
363
364
    public function setCollection(?CollectionInterface $collection = null): void
365
    {
366
        $this->collection = $collection;
367
    }
368
369
    public function getCollection()
370
    {
371
        return $this->collection;
372
    }
373
374
    /**
375
     * @param $contentFormatter
376
     */
377
    public function setContentFormatter($contentFormatter): void
378
    {
379
        $this->contentFormatter = $contentFormatter;
380
    }
381
382
    public function getContentFormatter()
383
    {
384
        return $this->contentFormatter;
385
    }
386
387
    public function setRawContent($rawContent): void
388
    {
389
        $this->rawContent = $rawContent;
390
    }
391
392
    public function getRawContent()
393
    {
394
        return $this->rawContent;
395
    }
396
}
397