AdventureRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 80.77 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 42
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailableAdventuresForUser() 21 21 2
A getCompletedAdventuresForUser() 21 21 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 Badger\Bundle\GameBundle\Doctrine\Repository;
4
5
use Badger\Component\Game\Repository\AdventureRepositoryInterface;
6
use Badger\Component\Game\Taggable\TaggableInterface;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityRepository;
9
10
/**
11
 * Doctrine implementation of repository for Adventure entities.
12
 *
13
 * @author  Marie Bochu <[email protected]>
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class AdventureRepository extends EntityRepository implements AdventureRepositoryInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 View Code Duplication
    public function getAvailableAdventuresForUser(TaggableInterface $user)
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...
22
    {
23
        $tagIds = [];
24
        foreach ($user->getTags() as $tag) {
25
            $tagIds[] = $tag->getId();
26
        }
27
28
        $qb = $this->createQueryBuilder('adventure');
29
        $qb->leftJoin('adventure.tags', 'tags')
30
            ->leftJoin('adventure.steps', 's')
31
            ->leftJoin('s.completions', 'sc', 'WITH', 'sc.user = :user AND sc.pending = 0')
32
            ->setParameter('user', $user)
33
            ->having('COUNT(sc) < COUNT(s)')
34
            ->andWhere('tags.id IN (:tagIds)')
35
            ->setMaxResults(15)
36
            ->groupBy('adventure.id')
37
            ->setParameter('tagIds', $tagIds, Connection::PARAM_STR_ARRAY)
38
        ;
39
40
        return $qb->getQuery()->getResult();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function getCompletedAdventuresForUser(TaggableInterface $user)
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
        $tagIds = [];
49
        foreach ($user->getTags() as $tag) {
50
            $tagIds[] = $tag->getId();
51
        }
52
53
        $qb = $this->createQueryBuilder('adventure');
54
        $qb->leftJoin('adventure.tags', 'tags')
55
            ->leftJoin('adventure.steps', 's')
56
            ->leftJoin('s.completions', 'sc', 'WITH', 'sc.user = :user AND sc.pending = 0')
57
            ->setParameter('user', $user)
58
            ->having('COUNT(sc) = COUNT(s)')
59
            ->andWhere('tags.id IN (:tagIds)')
60
            ->setMaxResults(15)
61
            ->groupBy('adventure.id')
62
            ->setParameter('tagIds', $tagIds, Connection::PARAM_STR_ARRAY)
63
        ;
64
65
        return $qb->getQuery()->getResult();
66
    }
67
}
68