AdminController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 29.59 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 29
loc 98
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 8 1
A indexAction() 0 7 1
A addAction() 14 27 4
A editAction() 15 32 5
A deleteAction() 0 15 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
3
namespace Widget\Controller;
4
5
use Application\Mvc\Controller;
6
use Widget\Model\Widget;
7
use Widget\Form\WidgetForm;
8
9
class AdminController extends Controller
10
{
11
12
    public function initialize()
13
    {
14
        $this->setAdminEnvironment();
15
        $this->helper->activeMenu()->setActive('admin-widget');
16
17
        $this->view->languages_disabled = true;
0 ignored issues
show
Bug introduced by
Accessing languages_disabled on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
18
19
    }
20
21
    public function indexAction()
22
    {
23
        $this->view->entries = Widget::find();
0 ignored issues
show
Bug introduced by
Accessing entries on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Are you sure the assignment to $this->view->entries is correct as \Widget\Model\Widget::find() (which targets Phalcon\Mvc\Model::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
24
25
        $this->helper->title($this->helper->at('Manage Widgets'), true);
26
27
    }
28
29
    public function addAction()
30
    {
31
        $widget = new Widget();
32
        $form = new WidgetForm();
33
34 View Code Duplication
        if ($this->request->isPost()) {
35
            $form->bind($this->request->getPost(), $widget);
36
            if ($form->isValid()) {
37
                if ($widget->save()) {
38
                    $this->redirect($this->url->get() . 'widget/admin/edit/' . $widget->getId());
39
                } else {
40
                    $this->flashErrors($widget);
41
                }
42
            } else {
43
                $this->flashErrors($form);
44
            }
45
        } else {
46
            $form->setEntity($widget);
47
        }
48
49
        $this->view->pick('admin/edit');
50
        $this->view->setVar('form', $form);
0 ignored issues
show
Bug introduced by
The method setVar does only exist in Phalcon\Mvc\View, but not in Phalcon\Mvc\ViewInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
51
52
        $this->view->title = $this->helper->at('Adding widget');
0 ignored issues
show
Bug introduced by
Accessing title on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
53
        $this->helper->title($this->view->title);
0 ignored issues
show
Bug introduced by
Accessing title on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
54
55
    }
56
57
    public function editAction($id)
58
    {
59
        $id = $this->filter->sanitize($id, "string");
60
        $widget = Widget::findFirst(["id = '$id'"]);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $widget is correct as \Widget\Model\Widget::fi...(array("id = '{$id}'")) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
        if (!$widget) {
62
            $this->redirect($this->url->get() . 'widget/admin/add');
63
        }
64
65
        $form = new WidgetForm();
66
        $form->remove('id');
67 View Code Duplication
        if ($this->request->isPost()) {
68
69
            $form->bind($this->request->getPost(), $widget);
70
            if ($form->isValid()) {
71
                if ($widget->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $widget (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
72
                    $this->redirect($this->url->get() . 'widget/admin/edit/' . $widget->getId());
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $widget (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
73
                } else {
74
                    $this->flashErrors($widget);
75
                }
76
            } else {
77
                $this->flashErrors($form);
78
            }
79
        } else {
80
            $form->setEntity($widget);
81
        }
82
83
        $this->view->setVar('form', $form);
0 ignored issues
show
Bug introduced by
The method setVar does only exist in Phalcon\Mvc\View, but not in Phalcon\Mvc\ViewInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
        $this->view->setVar('widget', $widget);
85
86
        $this->helper->title($this->helper->at('Editing widget'), true);
87
88
    }
89
90
    public function deleteAction($id)
91
    {
92
        $id = $this->filter->sanitize($id, "string");
93
        $model = Widget::findFirst(["id = '$id'"]);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Widget\Model\Widget::fi...(array("id = '{$id}'")) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94
        if ($model) {
95
96
            if ($this->request->isPost()) {
97
                $model->delete();
98
                $this->redirect($this->url->get() . 'widget/admin/index');
99
            }
100
101
            $this->view->model = $model;
0 ignored issues
show
Bug introduced by
Accessing model on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
        }
103
104
    }
105
106
}