EmissionRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 180
Duplicated Lines 23.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 42
loc 180
ccs 57
cts 57
cp 1
rs 10
c 3
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A findSectorByYear() 0 15 1
A findActivityByYearAndSector() 0 19 1
A findSubactivityByYearSectorAndActivity() 20 20 1
A findCategoryByYearSectorActivityAndSubactivity() 22 22 1
A findGasesByYear() 0 14 1
A findGasesAndSectorByYear() 0 16 1
A findSectorGroupedByYear() 0 12 1
A findSubactivitySectorBySector() 0 15 1
A findSubactivitySectorCategoryBySectorSubactivity() 0 16 1
A findSubactivitySectorCategoryBySectorSubactivityGroupByYearName() 0 19 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 Api\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
7
class EmissionRepository extends EntityRepository
8
{
9 1
    public function findSectorByYear($year)
10
    {
11
        $dql = 'SELECT s.name, 
12
                s.color, 
13
                s.description,  
14
                SUM(e.value) as total
15
            FROM Api\Entity\Emission e 
16
            INNER JOIN e.sector s
17
            WHERE e.year = :year
18 1
            GROUP BY e.sector';
19
20 1
        return $this->getEntityManager()->createQuery($dql)
21 1
            ->setParameter('year', $year)
22 1
            ->getResult();
23
    }
24
25 1
    public function findActivityByYearAndSector($year, $sector)
26
    {
27
        $dql = 'SELECT a.id as activity, 
28
                a.name, 
29
                SUM(e.value) as total
30
            FROM Api\Entity\Emission e 
31
            INNER JOIN e.sector s
32
            INNER JOIN e.activity a
33
            WHERE e.value > 0
34
            AND e.sector = :sector
35
            AND e.year = :year
36
            GROUP BY e.activity
37 1
            ORDER BY a.name';
38
39 1
        return $this->getEntityManager()->createQuery($dql)
40 1
            ->setParameter('year', $year)
41 1
            ->setParameter('sector', $sector)
42 1
            ->getResult();
43
    }
44
45 1 View Code Duplication
    public function findSubactivityByYearSectorAndActivity($year, $sector, $activity)
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...
46
    {
47
        $dql = 'SELECT sa.id as subactivity, 
48
                sa.name, 
49
                SUM(e.value) as total
50
            FROM Api\Entity\Emission e 
51
            INNER JOIN e.subactivity sa
52
            WHERE e.value > 0
53
            AND e.sector = :sector
54
            AND e.activity = :activity
55
            AND e.year = :year
56
            GROUP BY e.subactivity
57 1
            ORDER BY sa.name';
58
59 1
        return $this->getEntityManager()->createQuery($dql)
60 1
            ->setParameter('year', $year)
61 1
            ->setParameter('sector', $sector)
62 1
            ->setParameter('activity', $activity)
63 1
            ->getResult();
64
    }
65
66 1 View Code Duplication
    public function findCategoryByYearSectorActivityAndSubactivity($year, $sector, $activity, $subactivity)
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...
67
    {
68
        $dql = 'SELECT c.id as category, 
69
                c.name,
70
                SUM(e.value) as total
71
            FROM Api\Entity\Emission e 
72
            INNER JOIN e.category c
73
            WHERE e.value > 0
74
            AND e.sector = :sector
75
            AND e.activity = :activity
76
            AND e.subactivity = :subactivity
77
            AND e.year = :year
78
            GROUP BY e.category
79 1
            ORDER BY c.name';
80
81 1
        return $this->getEntityManager()->createQuery($dql)
82 1
            ->setParameter('year', $year)
83 1
            ->setParameter('sector', $sector)
84 1
            ->setParameter('activity', $activity)
85 1
            ->setParameter('subactivity', $subactivity)
86 1
            ->getResult();
87
    }
88
89 2
    public function findGasesByYear($year)
90
    {
91
        $dql = 'SELECT g.id as gas, 
92
                g.name, 
93
                g.color, 
94
                SUM(e.value) as total
95
            FROM Api\Entity\Emission e 
96
            LEFT JOIN e.gas g 
97 2
            where e.year = :year GROUP BY e.gas ORDER BY total DESC';
98
99 2
        return $this->getEntityManager()->createQuery($dql)
100 2
            ->setParameter('year', $year)
101 2
            ->getResult();
102
    }
103
104 1
    public function findGasesAndSectorByYear($year)
105
    {
106
        $dql = 'SELECT s.name as sector, 
107
                g.name as gas, 
108
                sum(e.value) as total
109
            FROM Api\Entity\Emission e 
110
            LEFT JOIN e.gas g 
111
            LEFT JOIN e.sector s
112
            WHERE e.year = :year
113
            GROUP BY e.gas, e.sector
114 1
            ORDER BY total DESC';
115
116 1
        return $this->getEntityManager()->createQuery($dql)
117 1
            ->setParameter('year', $year)
118 1
            ->getResult();
119
    }
120
121 2
    public function findSectorGroupedByYear()
122
    {
123
        $dql = 'SELECT s.name as sector, 
124
                e.year, 
125
                SUM(e.value) as total
126
            FROM Api\Entity\Emission e 
127
            LEFT JOIN e.sector s
128 2
            GROUP BY e.year, s.name';
129
130 2
        return $this->getEntityManager()->createQuery($dql)
131 2
            ->getResult();            
132
    }
133
134 1
    public function findSubactivitySectorBySector($sector)
135
    {
136
        $dql = 'SELECT sub.name as sector, 
137
                e.year, 
138
                SUM(e.value) as total
139
            FROM Api\Entity\Emission e
140
            INNER JOIN e.subactivity sub
141
            INNER JOIN e.sector s 
142
            WHERE s.id = :sector 
143 1
            GROUP BY e.year, sub.name';
144
145 1
        return $this->getEntityManager()->createQuery($dql)
146 1
            ->setParameter('sector', $sector)
147 1
            ->getResult();            
148
    }
149
150 1
    public function findSubactivitySectorCategoryBySectorSubactivity($sector, $subactivity)
151
    {
152
        $dql = 'SELECT DISTINCT c.name
153
            FROM Api\Entity\Emission e
154
            INNER JOIN e.subactivity sub
155
            INNER JOIN e.sector s 
156
            INNER JOIN e.category c 
157
            WHERE s.id = :sector
158
            AND sub.id = :subactivity
159 1
            ORDER BY c.name';
160
161 1
        return $this->getEntityManager()->createQuery($dql)
162 1
            ->setParameter('sector', $sector)
163 1
            ->setParameter('subactivity', $subactivity)
164 1
            ->getResult();
165
    }
166
167 1
    public function findSubactivitySectorCategoryBySectorSubactivityGroupByYearName($sector, $subactivity)
168
    {
169
        $dql = 'SELECT sub.name as subcategoria, 
170
                e.year, 
171
                c.name, 
172
                SUM(e.value) as value
173
            FROM Api\Entity\Emission e
174
            INNER JOIN e.subactivity sub
175
            INNER JOIN e.sector s 
176
            INNER JOIN e.category c 
177
            WHERE s.id = :sector
178
            AND sub.id = :subactivity
179 1
            GROUP BY e.year, c.name';
180
181 1
        return $this->getEntityManager()->createQuery($dql)
182 1
            ->setParameter('sector', $sector)
183 1
            ->setParameter('subactivity', $subactivity)
184 1
            ->getResult();            
185
    }
186
}
187