Completed
Push — develop ( 117c72...bb7f99 )
by Victor
03:10
created

ArticleRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 22.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 79.55%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 10
c 6
b 0
f 3
lcom 1
cbo 3
dl 27
loc 122
ccs 70
cts 88
cp 0.7955
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getArticlesWithCountComment() 0 18 1
A getArticleWithDep() 0 12 1
A getArticleWithCountComment() 14 14 1
B getArticlesSorted() 0 46 5
A getPopularArticles() 13 13 1
A getRecentArticles() 0 11 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
//            ->getResult();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
31 1
        return $articles = new PaginatorWithPages($query, $fetchJoinCollection = true);
0 ignored issues
show
Unused Code introduced by
$articles is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
    }
33
34 1
    public function getArticleWithDep($slug)
35
    {
36 1
        return $this->createQueryBuilder('a')
37 1
            ->select('a, c, t, u')
38 1
            ->leftJoin('a.categories', 'c')
39 1
            ->leftJoin('a.tags', 't')
40 1
            ->join('a.user', 'u')
41 1
            ->where('a.slug = ?1')
42 1
            ->setParameter(1, $slug)
43 1
            ->getQuery()
44 1
            ->getSingleResult();
45
    }
46
47 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...
48
    {
49
        return $this->createQueryBuilder('a')
50
            ->select('a, c, t, u, count(cm.id) as countComments')
51
            ->leftJoin('a.categories', 'c')
52
            ->leftJoin('a.tags', 't')
53
            ->leftJoin('a.comments', 'cm')
54
            ->join('a.user', 'u')
55
            ->where('a.slug = ?1')
56
            ->groupBy('a, c, t, u')
57
            ->setParameter(1, $slug)
58
            ->getQuery()
59
            ->getSingleResult();
60
    }
61
62 3
    public function getArticlesSorted($sortBy, $param, $page = 1, $max = 10)
63
    {
64 3
        $first = $max * ($page - 1);
65 3
        $query = $this->createQueryBuilder('a')
66 3
            ->select('a, c, t, u, count(cm.id) as countComments')
67 3
            ->leftJoin('a.categories', 'c')
68 3
            ->leftJoin('a.tags', 't')
69 3
            ->leftJoin('a.comments', 'cm')
70 3
            ->join('a.user', 'u')
71 3
            ->groupBy('a, c, t, u')
72 3
            ->orderBy('a.createdAt', 'DESC');
73
74
        switch ($sortBy) {
75 3
            case 'category':
76
                $query
77 1
                    ->where('c.slug = ?1')
78 1
                    ->setParameter(1, $param);
79 1
                break;
80 2
            case 'tag':
81
                $query
82 1
                    ->where('t.slug = ?1')
83 1
                    ->setParameter(1, $param);
84 1
                break;
85 1
            case 'author':
86
                $query
87 1
                    ->where('u.slug = ?1')
88 1
                    ->setParameter(1, $param);
89 1
                break;
90
            case 'date':
91
                $query
92
                    ->where('a.createdAt >= ?1')
93
                    ->andWhere('a.createdAt <= ?2')
94
                    ->setParameter(1, $param." 00:00:00")
95
                    ->setParameter(2, $param." 23:59:59");
96
                break;
97
        }
98
99
        $query
100 3
            ->setFirstResult($first)
101 3
            ->setMaxResults($max)
102 3
            ->getQuery();
103
//            ->getResult();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
105 3
        return $articles = new PaginatorWithPages($query, $fetchJoinCollection = true);
0 ignored issues
show
Unused Code introduced by
$articles is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
107
    }
108
109 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...
110
    {
111 5
        return $this->createQueryBuilder('a')
112 5
            ->select('a, u, avg(c.rating) as rating')
113 5
            ->leftJoin('a.comments', 'c')
114 5
            ->join('a.user', 'u')
115 5
            ->groupBy('a, u')
116 5
            ->orderBy('rating', 'DESC')
117 5
            ->setFirstResult(0)
118 5
            ->setMaxResults($max)
119 5
            ->getQuery()
120 5
            ->getResult();
121
    }
122
123 5
    public function getRecentArticles($max = 5)
124
    {
125 5
        return $this->createQueryBuilder('a')
126 5
            ->select('a, u')
127 5
            ->join('a.user', 'u')
128 5
            ->orderBy('a.createdAt', 'DESC')
129 5
            ->setFirstResult(0)
130 5
            ->setMaxResults($max)
131 5
            ->getQuery()
132 5
            ->getResult();
133
    }
134
}
135