ArticleRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 19.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 0 Features 4
Metric Value
wmc 12
c 9
b 0
f 4
lcom 1
cbo 3
dl 27
loc 138
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getArticlesWithCountComment() 0 17 1
A getArticleWithDep() 0 12 1
A getArticleWithCountComment() 14 14 1
B getArticlesSorted() 0 55 6
A getPopularArticles() 13 13 1
A getRecentArticles() 0 11 1
A getCountArticles() 0 7 1

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
namespace AppBundle\Repository;
4
use AppBundle\Model\PaginatorWithPages;
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * ArticleRepository
9
 *
10
 * This class was generated by the Doctrine ORM. Add your own custom
11
 * repository methods below.
12
 */
13
class ArticleRepository extends EntityRepository
14
{
15
    public function getArticlesWithCountComment($page = 1, $max = 10)
16
    {
17
        $first = $max * ($page - 1);
18
        $query = $this->createQueryBuilder('a')
19
            ->select('a, c, t, u, count(cm.id) as countComments')
20
            ->leftJoin('a.categories', 'c')
21
            ->leftJoin('a.tags', 't')
22
            ->leftJoin('a.comments', 'cm')
23
            ->join('a.user', 'u')
24
            ->groupBy('a, c, t, u')
25
            ->orderBy('a.createdAt', 'DESC')
26
            ->setFirstResult($first)
27
            ->setMaxResults($max)
28
            ->getQuery();
29
30
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
31
    }
32
33
    public function getArticleWithDep($slug)
34
    {
35
        return $this->createQueryBuilder('a')
36
            ->select('a, c, t, u')
37
            ->leftJoin('a.categories', 'c')
38
            ->leftJoin('a.tags', 't')
39
            ->join('a.user', 'u')
40
            ->where('a.slug = ?1')
41
            ->setParameter(1, $slug)
42
            ->getQuery()
43
            ->getSingleResult();
44
    }
45
46 View Code Duplication
    public function getArticleWithCountComment($slug)
0 ignored issues
show
Duplication introduced by
This method 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...
47
    {
48
        return $this->createQueryBuilder('a')
49
            ->select('a, c, t, u, count(cm.id) as countComments')
50
            ->leftJoin('a.categories', 'c')
51
            ->leftJoin('a.tags', 't')
52
            ->leftJoin('a.comments', 'cm')
53
            ->join('a.user', 'u')
54
            ->where('a.slug = ?1')
55
            ->groupBy('a, c, t, u')
56
            ->setParameter(1, $slug)
57
            ->getQuery()
58
            ->getSingleResult();
59
    }
60
61
    public function getArticlesSorted($sortBy, $param, $page = 1, $max = 10)
62
    {
63
        $first = $max * ($page - 1);
64
        $query = $this->createQueryBuilder('a')
65
            ->select('a, c, t, u, count(cm.id) as countComments')
66
            ->leftJoin('a.categories', 'c')
67
            ->leftJoin('a.tags', 't')
68
            ->leftJoin('a.comments', 'cm')
69
            ->join('a.user', 'u')
70
            ->groupBy('a, c, t, u')
71
            ->orderBy('a.createdAt', 'DESC');
72
73
        switch ($sortBy) {
74
            case 'category':
75
                $query
76
                    ->where('c.slug = ?1')
77
                    ->setParameter(1, $param);
78
                break;
79
            case 'tag':
80
                $query
81
                    ->where('t.slug = ?1')
82
                    ->setParameter(1, $param);
83
                break;
84
            case 'author':
85
                $query
86
                    ->where('u.slug = ?1')
87
                    ->setParameter(1, $param);
88
                break;
89
            case 'date':
90
                $query
91
                    ->where('a.createdAt >= ?1')
92
                    ->andWhere('a.createdAt <= ?2')
93
                    ->setParameter(1, $param." 00:00:00")
94
                    ->setParameter(2, $param." 23:59:59");
95
                break;
96
            case 'search':
97
                $query
98
                    ->where(
99
                        $query->expr()->like(
100
                            $query->expr()->lower('a.title'),
101
                            $query->expr()->lower('?1')
102
                        )
103
                    )
104
                    ->setParameter(1, '%'.$param.'%');
105
                break;
106
        }
107
108
        $query
109
            ->setFirstResult($first)
110
            ->setMaxResults($max)
111
            ->getQuery();
112
113
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
114
115
    }
116
117 View Code Duplication
    public function getPopularArticles($max = 5)
0 ignored issues
show
Duplication introduced by
This method 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...
118
    {
119
        return $this->createQueryBuilder('a')
120
            ->select('a, u, avg(c.rating) as rating')
121
            ->leftJoin('a.comments', 'c')
122
            ->join('a.user', 'u')
123
            ->groupBy('a, u')
124
            ->orderBy('rating', 'DESC')
125
            ->setFirstResult(0)
126
            ->setMaxResults($max)
127
            ->getQuery()
128
            ->getResult();
129
    }
130
131
    public function getRecentArticles($max = 5)
132
    {
133
        return $this->createQueryBuilder('a')
134
            ->select('a, u')
135
            ->join('a.user', 'u')
136
            ->orderBy('a.createdAt', 'DESC')
137
            ->setFirstResult(0)
138
            ->setMaxResults($max)
139
            ->getQuery()
140
            ->getResult();
141
    }
142
143
    public function getCountArticles()
144
    {
145
        return $this->createQueryBuilder('a')
146
            ->select('count(a.id) as countArticles')
147
            ->getQuery()
148
            ->getSingleResult();
149
    }
150
}
151