Completed
Push — master ( 4ea9b4...70e6b0 )
by Serhii
04:57
created

PostRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 14.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 9
loc 62
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findAllOrByTag() 0 15 1
A getCount() 9 9 1
A getTaggedPostsQuery() 0 13 2

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 App\Repository;
4
5
use App\Entity\Post;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
8
class PostRepository extends AbstractRepository
9
{
10 6
    public function __construct(ManagerRegistry $registry)
11
    {
12 6
        parent::__construct($registry, Post::class);
13 6
    }
14
15
    /**
16
     * @param int $limit
17
     * @param int $page
18
     * @param string $tagSlug
19
     * @return Post[]
20
     */
21 3
    public function findAllOrByTag($limit, $page, $tagSlug = null)
22
    {
23 3
        $taggedPosts = $this->getTaggedPostsQuery($tagSlug)
24 3
            ->setMaxResults($limit)
25 3
            ->setFirstResult($page)
26 3
            ->orderBy('u.createdAt', 'DESC')
27 3
            ->getQuery()
28 3
            ->enableResultCache(60*60*24)
29 3
            ->execute()
30
        ;
31
32 3
        $pinnedPosts = $this->getTaggedPostsQuery($tagSlug, true)->getQuery()->execute();
33
34 3
        return array_merge($pinnedPosts, $taggedPosts);
35
    }
36
37
    /**
38
     * @param string $tagSlug
39
     * @return int
40
     */
41 3 View Code Duplication
    public function getCount($tagSlug = null)
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...
42
    {
43 3
        $qb = $this->getTaggedPostsQuery($tagSlug);
44 3
        $qb->select($qb->expr()->count('u'));
45
46 3
        $query = $qb->getQuery()->enableResultCache(60*60*24);
47
48 3
        return $query->getSingleScalarResult();
49
    }
50
51
    /**
52
     * @param string $tagSlug
53
     * @param bool $pinned
54
     * @return \Doctrine\ORM\QueryBuilder
55
     */
56 3
    protected function getTaggedPostsQuery($tagSlug = null, $pinned = false)
57
    {
58 3
        $qb = $this->createQueryBuilder('u')
59 3
            ->andWhere('u.pinned = :pinned')
60 3
            ->setParameter('pinned', $pinned)
61
        ;
62
63 3
        if ($tagSlug) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tagSlug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
64
            $qb->join('u.tags', 't')->andWhere('t.slug = :slug')->setParameter('slug', $tagSlug);
65
        }
66
67 3
        return $qb;
68
    }
69
}
70