MediaComment   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 4
dl 71
loc 71
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setComments() 32 32 2
A getChildren() 23 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace frontend\widgets\comment;
9
10
use common\models\MediaComment as Comment;
11
use yii\data\Pagination;
12
13
/**
14
 * Class MediaComment
15
 *
16
 * @author Agiel K. Saputra <[email protected]>
17
 * @since 0.1.0
18
 */
19 View Code Duplication
class MediaComment extends BaseComment
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * Set comment and pagination.
23
     * Select all comments from database and create pagination.
24
     * Get child of current comment.
25
     */
26
    protected function setComments()
27
    {
28
        /* @var $models \common\models\BaseComment */
29
        $comments = [];
30
        $query = Comment::find()
31
            ->select(['id', 'author', 'email', 'url', 'date', 'content'])
32
            ->andWhere(['parent' => 0, 'media_id' => $this->model->id, 'status' => 'approved'])
33
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
34
            ->orderBy(['date' => $this->commentOrder]);
35
36
        $countQuery = clone $query;
37
38
        $pages = new Pagination([
39
            'totalCount' => $countQuery->count(),
40
            'pageSize' => $this->pageSize,
41
        ]);
42
43
        $this->pages = $pages;
44
45
        $models = $query
46
            ->offset($pages->offset)
47
            ->limit($pages->limit)
48
            ->all();
49
50
        /* @var $model \common\models\PostComment */
51
        foreach ($models as $model) {
52
            $comments[$model->id] = $model;
53
            $comments[$model->id]['child'] = $this->getChildren($model->id);
54
        }
55
56
        $this->comments = $comments;
57
    }
58
59
60
    /**
61
     * Get comment children based on comment ID.
62
     *
63
     * @param int $id
64
     * @return array|null
65
     */
66
    protected function getChildren($id)
67
    {
68
        $comments = [];
69
70
        $models = Comment::find()
71
            ->select(['id', 'author', 'email', 'url', 'date', 'content'])
72
            ->andWhere(['parent' => $id, 'media_id' => $this->model->id, 'status' => 'approved'])
73
            ->andWhere(['<=', 'date', date('Y-m-d H:i:s')])
74
            ->orderBy(['date' => $this->commentOrder])
75
            ->all();
76
77
        if (empty($models)) {
78
            $comments = null;
79
        } else {
80
            /* @var $model \common\models\PostComment */
81
            foreach ($models as $model) {
82
                $comments[$model->id] = $model;
83
                $comments[$model->id]['child'] = $this->getChildren($model->id);
84
            }
85
        }
86
87
        return $comments;
88
    }
89
}
90