Completed
Push — develop ( 6d3372...82247e )
by Victor
02:40
created

ArticleRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 21.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75.27%

Importance

Changes 8
Bugs 0 Features 3
Metric Value
wmc 11
c 8
b 0
f 3
lcom 1
cbo 3
dl 27
loc 128
ccs 70
cts 93
cp 0.7527
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 45 5
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 1
    public function getArticlesWithCountComment($page = 1, $max = 10)
16
    {
17 1
        $first = $max * ($page - 1);
18 1
        $query = $this->createQueryBuilder('a')
19 1
            ->select('a, c, t, u, count(cm.id) as countComments')
20 1
            ->leftJoin('a.categories', 'c')
21 1
            ->leftJoin('a.tags', 't')
22 1
            ->leftJoin('a.comments', 'cm')
23 1
            ->join('a.user', 'u')
24 1
            ->groupBy('a, c, t, u')
25 1
            ->orderBy('a.createdAt', 'DESC')
26 1
            ->setFirstResult($first)
27 1
            ->setMaxResults($max)
28 1
            ->getQuery();
29
30 1
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
31
    }
32
33 1
    public function getArticleWithDep($slug)
34
    {
35 1
        return $this->createQueryBuilder('a')
36 1
            ->select('a, c, t, u')
37 1
            ->leftJoin('a.categories', 'c')
38 1
            ->leftJoin('a.tags', 't')
39 1
            ->join('a.user', 'u')
40 1
            ->where('a.slug = ?1')
41 1
            ->setParameter(1, $slug)
42 1
            ->getQuery()
43 1
            ->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 3
    public function getArticlesSorted($sortBy, $param, $page = 1, $max = 10)
62
    {
63 3
        $first = $max * ($page - 1);
64 3
        $query = $this->createQueryBuilder('a')
65 3
            ->select('a, c, t, u, count(cm.id) as countComments')
66 3
            ->leftJoin('a.categories', 'c')
67 3
            ->leftJoin('a.tags', 't')
68 3
            ->leftJoin('a.comments', 'cm')
69 3
            ->join('a.user', 'u')
70 3
            ->groupBy('a, c, t, u')
71 3
            ->orderBy('a.createdAt', 'DESC');
72
73
        switch ($sortBy) {
74 3
            case 'category':
75
                $query
76 1
                    ->where('c.slug = ?1')
77 1
                    ->setParameter(1, $param);
78 1
                break;
79 2
            case 'tag':
80
                $query
81 1
                    ->where('t.slug = ?1')
82 1
                    ->setParameter(1, $param);
83 1
                break;
84 1
            case 'author':
85
                $query
86 1
                    ->where('u.slug = ?1')
87 1
                    ->setParameter(1, $param);
88 1
                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
        }
97
98
        $query
99 3
            ->setFirstResult($first)
100 3
            ->setMaxResults($max)
101 3
            ->getQuery();
102
103 3
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
104
105
    }
106
107 5 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...
108
    {
109 5
        return $this->createQueryBuilder('a')
110 5
            ->select('a, u, avg(c.rating) as rating')
111 5
            ->leftJoin('a.comments', 'c')
112 5
            ->join('a.user', 'u')
113 5
            ->groupBy('a, u')
114 5
            ->orderBy('rating', 'DESC')
115 5
            ->setFirstResult(0)
116 5
            ->setMaxResults($max)
117 5
            ->getQuery()
118 5
            ->getResult();
119
    }
120
121 5
    public function getRecentArticles($max = 5)
122
    {
123 5
        return $this->createQueryBuilder('a')
124 5
            ->select('a, u')
125 5
            ->join('a.user', 'u')
126 5
            ->orderBy('a.createdAt', 'DESC')
127 5
            ->setFirstResult(0)
128 5
            ->setMaxResults($max)
129 5
            ->getQuery()
130 5
            ->getResult();
131
    }
132
133
    public function getCountArticles()
134
    {
135
        return $this->createQueryBuilder('a')
136
            ->select('count(a.id) as countArticles')
137
            ->getQuery()
138
            ->getSingleResult();
139
    }
140
}
141