Completed
Push — master ( 41af6a...be871c )
by Fèvre
11s
created

PollsAnswersController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 76.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 26
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B delete() 26 26 3

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
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
6
class PollsAnswersController extends AppController
7
{
8
    /**
9
     * Delete an answer from a poll.
10
     *
11
     * @return \Cake\Network\Response
12
     */
13 View Code Duplication
    public function delete()
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...
14
    {
15
        $this->loadModel('PollsAnswers');
16
17
        $answer = $this->PollsAnswers
0 ignored issues
show
Documentation introduced by
The property PollsAnswers does not exist on object<App\Controller\Ad...PollsAnswersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
18
            ->find()
19
            ->where([
20
                'id' => $this->request->getAttribute('params')['id']
21
            ])
22
            ->first();
23
24
        //Check if the answer is found.
25
        if (empty($answer)) {
26
            $this->Flash->error(__d('admin', 'This answer doesn\'t exist or has been deleted.'));
27
28
            return $this->redirect(['action' => 'index']);
29
        }
30
31
        if ($this->PollsAnswers->delete($answer)) {
0 ignored issues
show
Documentation introduced by
The property PollsAnswers does not exist on object<App\Controller\Ad...PollsAnswersController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
32
            $this->Flash->success(__d('admin', 'This answer has been deleted successfully.'));
33
        } else {
34
             $this->Flash->error(__d('admin', 'An error occured while deleting this answers.'));
35
        }
36
37
        return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Http\ServerRequest>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
38
    }
39
}
40