getWholeSectoralEvolutionAction()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 50
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 50
ccs 22
cts 22
cp 1
rs 8.6315
c 1
b 0
f 1
cc 5
eloc 23
nc 8
nop 0
crap 5
1
<?php
2
3
namespace Api\Controller;
4
5
use Zend\Mvc\Controller\AbstractRestfulController;
6
use Zend\View\Model\JsonModel;
7
use Api\Helper\Utils;
8
use Api\Entity\Emission;
9
use Api\Entity\Sector;
10
use Api\Entity\Subactivity;
11
12
/**
13
 * @SWG\Swagger(
14
 *   schemes={"http"},
15
 *   basePath="/informe", 
16
 *   @SWG\Info(
17
 *     title="API documentation",
18
 *     version="1.0.1"
19
 *   )
20
 * )
21
 */
22
class EvolutionReportController extends AbstractRestfulController
23
{
24
    /**
25
     * Entity manager.
26
     * @var Doctrine\ORM\EntityManager
27
     */
28
    private $entityManager;
29
30
    const START_YEAR = 1990;
31
32
    const END_YEAR = 2014;
33
34 4
    public function __construct($entityManager)
35
    {
36 4
        $this->entityManager = $entityManager;
37 4
    }
38
39 1
    public function getWholeSectoralEvolutionAction()
40
    {
41 1
        $response = [];
42
43
        // TRAIGO LOS SECTORES CON SUS COLORES
44
45 1
        $arrSectores = $this->entityManager->getRepository(Sector::class)
46 1
            ->getSectorsOrderedyName();
47
48
        // TRAIGO LOS VALORES POR ANO
49
        // LO QUE ESTA ADENTRO DEL LOOP DEBERIA IR ACA
50
51 1
        $arrAnos = [];
52 1
        $arrValores = [];
0 ignored issues
show
Unused Code introduced by
$arrValores 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...
53 1
        $arrColores = [];
0 ignored issues
show
Unused Code introduced by
$arrColores 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...
54
55 1
        for ($i = self::START_YEAR; $i <= self::END_YEAR; $i++) {
56 1
            $arrAnos[] = $i;
57
        }
58
59 1
        $column = 2;
60
61 1
        foreach ($arrSectores as $sector) {
62 1
            $response['column_'.$column][] = $sector['name'];
63 1
            $response['colores'][] = $sector['color'];
64
65 1
            foreach ($arrAnos as $ano) {
66
                // ATENCION, CABECEADA
67
                // ESTOY EJECUTANDO EL QUERY CADA VEZ QUE NECESITO LA LISTA DE VALORES
68
                // ESTA PARTE DEBERIA AFUERA DEL LOOP Y SE DEBERIA REUTILIZAR $arrValoresCrudo
69 1
                $arrValoresCrudo = $this->entityManager->getRepository(Emission::class)
70 1
                    ->findSectorGroupedByYear();
71
72 1
                if (empty($arrValoresCrudo)) {
73
                    $this->response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
74
                }
75
                // HASTA ACA
76
77 1
                $response['column_'.$column][] = Utils::returnSectorAno($arrValoresCrudo, $sector['name'], $ano);
78
            }
79
80 1
            $column++;
81
        }
82
83
84 1
        $arrAnos = array_merge(array('x'), $arrAnos);
85 1
        $response['column_1'] = $arrAnos;
86
87 1
        return new JsonModel($response);
88
    }
89
90 1 View Code Duplication
    public function getSectoralEvolutionAction()
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...
91
    {
92 1
        $sector = (int) $this->params()->fromRoute('sector');
93
94 1
        $response = [];
95
96
        // TRAIGO LOS SECTORES CON SUS COLORES
97
98 1
        $arrSectores = $this->entityManager->getRepository(Sector::class)->getSector($sector);
99
100 1
        if (empty($arrSectores)) {
101
            $this->response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
        }
103
104
        // TRAIGO LOS VALORES POR ANO
105
        // LO QUE ESTA ADENTRO DEL LOOP DEBERIA IR ACA
106
107 1
        $arrAnos = [];
108 1
        $arrValores = [];
0 ignored issues
show
Unused Code introduced by
$arrValores 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...
109 1
        $arrColores = [];
0 ignored issues
show
Unused Code introduced by
$arrColores 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...
110
111 1
        for ($i = self::START_YEAR; $i <= self::END_YEAR; $i++) {
112 1
            $arrAnos[] = $i;
113
        }
114
115 1
        $column = 2;
116
117 1
        foreach ($arrSectores as $sector) {
118 1
            $response['column_'.$column][] = $sector['name'];
119 1
            $response['colores'][] = $sector['color'];
120
121 1
            foreach ($arrAnos as $ano) {
122
                // ATENCION, CABECEADA
123
                // ESTOY EJECUTANDO EL QUERY CADA VEZ QUE NECESITO LA LISTA DE VALORES
124
                // ESTA PARTE DEBERIA AFUERA DEL LOOP Y SE DEBERIA REUTILIZAR $arrValoresCrudo
125 1
                $arrValoresCrudo = $this->entityManager->getRepository(Emission::class)
126 1
                    ->findSectorGroupedByYear();
127
128 1
                if (empty($arrValoresCrudo)) {
129
                    $this->response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
130
                }
131
                // HASTA ACA
132
133 1
                $response['column_'.$column][] = Utils::returnSectorAno($arrValoresCrudo, $sector['name'], $ano);
134
            }
135
136 1
            $column++;
137
        }
138
139 1
        $arrAnos = array_merge(array('x'), $arrAnos);
140 1
        $response['column_1'] = $arrAnos;
141
142 1
        return new JsonModel($response);
143
    }
144
145 1 View Code Duplication
    public function getSectoralEvolutionSubactivityAction()
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...
146
    {
147 1
        $sector = (int) $this->params()->fromRoute('sector');
148
149 1
        $response = [];
150
151 1
        $arrSubactividades = $this->entityManager->getRepository(Subactivity::class)
152 1
            ->findActivitySectorBySector($sector);
153
154 1
        if (empty($arrSubactividades)) {
155
            $this->response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
156
        }
157
158
        // TRAIGO LOS VALORES POR ANO
159
        // LO QUE ESTA ADENTRO DEL LOOP DEBERIA IR ACA
160
161 1
        $arrAnos = [];
162 1
        $arrValores = [];
0 ignored issues
show
Unused Code introduced by
$arrValores 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...
163 1
        $arrColores = [];
0 ignored issues
show
Unused Code introduced by
$arrColores 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...
164
165 1
        for ($i = self::START_YEAR; $i <= self::END_YEAR; $i++) {
166 1
            $arrAnos[] = $i;
167
        }
168
169 1
        $column = 2;
170
171 1
        foreach ($arrSubactividades as $subactividad) {
172 1
            $response['column_'.$column][] = $subactividad['name'];
173 1
            $response['groups'][] = $subactividad['name'];
174
175 1
            foreach ($arrAnos as $ano) {
176
                // ATENCION, CABECEADA
177
                // ESTOY EJECUTANDO EL QUERY CADA VEZ QUE NECESITO LA LISTA DE VALORES
178
                // ESTA PARTE DEBERIA AFUERA DEL LOOP Y SE DEBERIA REUTILIZAR $arrValoresCrudo
179 1
                $arrValoresCrudo = $this->entityManager->getRepository(Emission::class)
180 1
                    ->findSubactivitySectorBySector($sector);
181
182 1
                if (empty($arrValoresCrudo)) {
183
                    $this->response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
184
                }
185
                // HASTA ACA
186
187 1
                $response['column_'.$column][] = Utils::returnSectorAno($arrValoresCrudo, $subactividad['name'], $ano);
188
            }
189
190 1
            $column++;
191
        }
192
193 1
        $arrAnos = array_merge(array('x'), $arrAnos);
194 1
        $response['column_1'] = $arrAnos;
195
196 1
        return new JsonModel($response);
197
    }
198
199 1
    public function getSectoralEvolutionSubactivityCategoryAction()
200
    {
201 1
        $sector = (int) $this->params()->fromRoute('sector');
202 1
        $subactivity = (int) $this->params()->fromRoute('subactivity');
203
204 1
        $response = [];
205
206 1
        $arrCategorias = $this->entityManager->getRepository(Emission::class)
207 1
            ->findSubactivitySectorCategoryBySectorSubactivity($sector, $subactivity);
208
209
        // LO QUE ESTA ADENTRO DEL LOOP DEBERIA IR ACA
210 1
        $arrAnos = [];
211 1
        $arrValores = [];
0 ignored issues
show
Unused Code introduced by
$arrValores 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...
212 1
        $arrColores = [];
0 ignored issues
show
Unused Code introduced by
$arrColores 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...
213
214 1
        for ($i = self::START_YEAR; $i <= self::END_YEAR; $i++) {
215 1
            $arrAnos[] = $i;
216
        }
217
218 1
        $column = 2;
219
220
        // // // pr($arrCategorias);
221
        // // // pr($arr);
222
223 1
        foreach ($arrCategorias as $categoria) {
224 1
            $response['column_'.$column][] = $categoria['name'];
225 1
            $response['groups'][] = $categoria['name'];
226
227 1
            foreach ($arrAnos as $ano) {
228
229
                // ATENCION, CABECEADA
230
                // ESTOY EJECUTANDO EL QUERY CADA VEZ QUE NECESITO LA LISTA DE VALORES
231
                // ESTA PARTE DEBERIA AFUERA DEL LOOP Y SE DEBERIA REUTILIZAR $arrValoresCrudo
232 1
                $arrValoresCrudo = $this->entityManager->getRepository(Emission::class)
233 1
                    ->findSubactivitySectorCategoryBySectorSubactivityGroupByYearName($sector, $subactivity);
234
                
235 1
                if (empty($arrValoresCrudo)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
236
                    //$this->response->setStatusCode(404);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% 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...
237
                }
238
                // HASTA ACA
239
240 1
                $response['column_'.$column][] = Utils::returnCategoriaAno($arrValoresCrudo, $categoria['name'], $ano);
241
            }
242
243 1
            $column++;
244
        }
245
246 1
        $arrAnos = array_merge(array('x'), $arrAnos);
247 1
        $response['column_1'] = $arrAnos;
248
        
249 1
        return new JsonModel($response);
250
    }
251
}
252