AdminController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 29.63 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 32
loc 108
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A indexAction() 0 6 1
A addAction() 32 32 5
A editAction() 0 38 5
A deleteAction() 0 17 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
 * Created by PhpStorm.
4
 * User: office-pb1
5
 * Date: 18.07.14
6
 * Time: 20:49
7
 */
8
9
namespace Page\Controller;
10
11
use Application\Mvc\Controller;
12
use Page\Model\Page;
13
use Page\Form\PageForm;
14
15
class AdminController extends Controller
16
{
17
18
    public function initialize()
19
    {
20
        $this->setAdminEnvironment();
21
        $this->helper->activeMenu()->setActive('admin-page');
22
        Page::setTranslateCache(false);
23
    }
24
25
    public function indexAction()
26
    {
27
        $this->view->entries = Page::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 \Page\Model\Page::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...
28
29
        $this->helper->title($this->helper->at('Manage Pages'), true);
30
    }
31
32 View Code Duplication
    public function addAction()
33
    {
34
        $this->view->pick(['admin/edit']);
35
        $form = new PageForm();
36
        $model = new Page();
37
38
        if ($this->request->isPost()) {
39
            $post = $this->request->getPost();
40
            $form->bind($post, $model);
41
            if ($form->isValid()) {
42
                if ($model->create()) {
43
                    $form->bind($post, $model);
44
                    $model->updateFields($post);
45
                    if ($model->update()) {
46
                        $this->flash->success($this->helper->at('Page created'));
47
                        return $this->redirect($this->url->get() . 'page/admin/edit/' . $model->getId() . '?lang=' . LANG);
48
                    } else {
49
                        $this->flashErrors($model);
50
                    }
51
                } else {
52
                    $this->flashErrors($model);
53
                }
54
            } else {
55
                $this->flashErrors($form);
56
            }
57
        }
58
59
        $this->helper->title($this->helper->at('Manage Pages'), true);
60
61
        $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...
62
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form 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...
63
    }
64
65
    public function editAction($id)
66
    {
67
        $id = (int) $id;
68
        $form = new PageForm();
69
        $model = Page::findFirst($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Page\Model\Page::findFirst($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...
70
71
        if ($model->getSlug() == 'index') {
0 ignored issues
show
Bug introduced by
The method getSlug cannot be called on $model (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
            $form->get('slug')->setAttribute('disabled', 'disabled');
0 ignored issues
show
Bug introduced by
The method setAttribute cannot be called on $form->get('slug') (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
        }
74
75
        if ($this->request->isPost()) {
76
            $post = $this->request->getPost();
77
            $form->bind($post, $model);
78
            if ($form->isValid()) {
79
                $model->updateFields($post);
0 ignored issues
show
Bug introduced by
The method updateFields cannot be called on $model (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...
80
                if ($model->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $model (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...
81
                    $this->flash->success($this->helper->at('Updated has been successful'));
82
83
                    // Очищаем кеш страницы
84
                    $query = "slug = '{$model->getSlug()}'";
0 ignored issues
show
Bug introduced by
The method getSlug cannot be called on $model (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...
85
                    $key = md5("Page::findFirst($query)");
86
                    $this->cache->delete($key);
87
88
                    return $this->redirect($this->url->get() . 'page/admin/edit/' . $model->getId() . '?lang=' . LANG);
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $model (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...
89
                } else {
90
                    $this->flashErrors($model);
91
                }
92
            } else {
93
                $this->flashErrors($form);
94
            }
95
        } else {
96
            $form->setEntity($model);
97
        }
98
99
        $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...
100
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form 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...
101
        $this->helper->title($this->helper->at('Edit Page'), true);
102
    }
103
104
    public function deleteAction($id)
105
    {
106
        $model = Page::findFirst($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Page\Model\Page::findFirst($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...
107
108
        if ($model->getSlug() == 'index') {
0 ignored issues
show
Bug introduced by
The method getSlug cannot be called on $model (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...
109
            $this->flash->error($this->helper->at('Index page can not be removed'));
110
            return $this->redirect($this->url->get() . 'page/admin');
111
        }
112
113
        if ($this->request->isPost()) {
114
            $model->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (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...
115
            $this->redirect($this->url->get() . 'page/admin');
116
        }
117
118
        $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...
119
        $this->helper->title($this->helper->at('Delete Page'), true);
120
    }
121
122
}