ItemPresenter::actionDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace UniMan\Presenters;
4
5
use UniMan\Components\DatabaseSelect\TablesSideBarControl;
6
use UniMan\Core\Forms\FilterForm\FilterForm;
7
use UniMan\Core\Forms\ItemForm;
8
use App\Component\VisualPaginator;
9
use Nette\Application\ForbiddenRequestException;
10
11
class ItemPresenter extends BasePresenter
12
{
13
    private $onPage;
14
15
    private $sorting = [];
16
17
    private $filter = [];
18
19
    private $columns = [];
20
21
    public function actionDefault($driver, $database, $type, $table, $page = 1, $onPage = FilterForm::DEFAULT_ON_PAGE, array $filter = [], array $sorting = [])
0 ignored issues
show
Unused Code introduced by
The parameter $driver is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $page is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        ksort($sorting);
24
        $this->database = $database;
25
        $this->type = $type;
26
        $this->table = $table;
27
        $this->onPage = $onPage;
28
        $this->sorting = $sorting;
29
        $this->filter = $filter;
30
        $this->columns = $this->driver->headerManager()->itemsHeaders($type, $table);
31
    }
32
33
    public function renderDefault($driver, $database, $type, $table, $page = 1, $onPage = FilterForm::DEFAULT_ON_PAGE, array $filter = [], array $sorting = [])
34
    {
35
        $this->template->driver = $driver;
36
        $this->template->database = $database;
37
        $this->template->type = $type;
38
        $this->template->table = $table;
39
        $this->template->sorting = $sorting;
40
        $this->template->filter = $filter;
41
        
42
        $itemsCount = $this->driver->dataManager()->itemsCount($type, $table, $filter);
43
        $this->template->itemsCount = $itemsCount;
44
        $this->template->items = $this->driver->dataManager()->items($type, $table, $page, $onPage, $filter, $sorting);
45
46
        $this->template->columns = $this->columns;
47
48
        $visualPaginator = $this['paginator'];
49
        $paginator = $visualPaginator->getPaginator();
50
        $paginator->setItemCount($itemsCount);
51
        $paginator->setItemsPerPage($onPage);
52
        $paginator->page = $page;
53
54
        foreach ($this->driver->dataManager()->getMessages() as $message => $type) {
55
            $type = $type ?: 'info';
56
            $this->flashMessage($message, $type);
57
        }
58
    }
59
60 View Code Duplication
    public function actionCreate($driver, $database, $type, $table)
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...
61
    {
62
        if (!$this->driver->permissions()->canCreateItem($database, $type, $table)) {
63
            throw new ForbiddenRequestException('Create item is not allowed');
64
        }
65
        $this->template->driver = $driver;
66
        $this->database = $database;
67
        $this->template->type = $this->type = $type;
68
        $this->table = $table;
69
    }
70
71 View Code Duplication
    public function actionEdit($driver, $database, $type, $table, $item)
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...
72
    {
73
        if (!$this->driver->permissions()->canEditItem($database, $type, $table, $item)) {
74
            throw new ForbiddenRequestException('Edit item is not allowed');
75
        }
76
        $this->template->driver = $driver;
77
        $this->database = $database;
78
        $this->template->type = $this->type = $type;
79
        $this->table = $table;
80
        $this->item = $item;
81
    }
82
83
    public function handleDelete($driver, $database, $type, $table, $item)
84
    {
85
        if (!$this->driver->permissions()->canDeleteItem($database, $type, $table, $item)) {
86
            throw new ForbiddenRequestException('Delete item is not allowed');
87
        }
88 View Code Duplication
        if ($this->driver->dataManager()->deleteItem($type, $table, $item)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            $this->flashMessage('Item was successfully deleted', 'success');
90
        } else {
91
            $this->flashMessage('Item was not deleted', 'danger');
92
        }
93
        $this->redirect('this', $driver, $database, $type, $table);
94
    }
95
96
    protected function createComponentForm()
97
    {
98
        return new ItemForm($this->translator, $this->driver, $this->database, $this->type, $this->table, $this->item);
99
    }
100
101
    protected function createComponentFilterForm()
102
    {
103
        $form = $this->driver->formManager()->filterForm($this->translator, $this->columns, $this->filter, $this->sorting, $this->onPage);
104
        $form->doRedirect[] = function ($onPage, $filter, $sorting) {
0 ignored issues
show
Bug introduced by
Accessing doRedirect on the interface UniMan\Core\Forms\FilterForm\FilterFormInterface 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...
105
            $this->redirect('Item:default', $this->driver->type(), $this->database, $this->type, $this->table, 1, $onPage, $filter, $sorting);
106
        };
107
        $form->doReset[] = function () {
0 ignored issues
show
Bug introduced by
Accessing doReset on the interface UniMan\Core\Forms\FilterForm\FilterFormInterface 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...
108
            $this->redirect('Item:default', $this->driver->type(), $this->database, $this->type, $this->table);
109
        };
110
        return $form;
111
    }
112
113
    protected function createComponentPaginator()
114
    {
115
        return new VisualPaginator();
116
    }
117
118
    protected function createComponentTablesSideBar()
119
    {
120
        return new TablesSideBarControl($this->driver, $this->database, $this->table);
121
    }
122
}
123