Renderer   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 195
Duplicated Lines 32.82 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 5
dl 64
loc 195
rs 9.8
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
D getFilters() 11 36 10
A getName() 0 4 1
A isExport() 0 4 1
A isHtml() 0 4 1
A getRequest() 0 9 2
C getSortConditions() 53 53 10
A getCurrentPageNumber() 0 12 2
A prepareViewModel() 0 18 3
A execute() 0 7 1

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 ZfcDatagrid\Renderer\BootstrapTable;
4
5
use Zend\Http\PhpEnvironment\Request as HttpRequest;
6
use ZfcDatagrid\Datagrid;
7
use ZfcDatagrid\Renderer\AbstractRenderer;
8
9
class Renderer extends AbstractRenderer
10
{
11
    /**
12
     * @return string
13
     */
14
    public function getName()
15
    {
16
        return 'bootstrapTable';
17
    }
18
19
    /**
20
     * @return bool
21
     */
22
    public function isExport()
23
    {
24
        return false;
25
    }
26
27
    /**
28
     * @return bool
29
     */
30
    public function isHtml()
31
    {
32
        return true;
33
    }
34
35
    /**
36
     * @return HttpRequest
37
     *
38
     * @throws \Exception
39
     */
40
    public function getRequest()
41
    {
42
        $request = parent::getRequest();
43
        if (!$request instanceof HttpRequest) {
44
            throw new \Exception('Request must be an instance of Zend\Http\PhpEnvironment\Request for HTML rendering');
45
        }
46
47
        return $request;
48
    }
49
50
    /**
51
     * @see \ZfcDatagrid\Renderer\AbstractRenderer::getSortConditions()
52
     *
53
     * @return array
54
     *
55
     * @throws \Exception
56
     */
57 View Code Duplication
    public function getSortConditions()
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...
58
    {
59
        if (is_array($this->sortConditions)) {
60
            // set from cache! (for export)
61
            return $this->sortConditions;
62
        }
63
64
        $request = $this->getRequest();
65
66
        $optionsRenderer = $this->getOptionsRenderer();
67
        $parameterNames = $optionsRenderer['parameterNames'];
68
69
        $sortConditions = [];
70
        $sortColumns = $request->getPost($parameterNames['sortColumns'], $request->getQuery($parameterNames['sortColumns']));
71
        $sortDirections = $request->getPost($parameterNames['sortDirections'], $request->getQuery($parameterNames['sortDirections']));
72
        if ($sortColumns != '') {
73
            $sortColumns = explode(',', $sortColumns);
74
            $sortDirections = explode(',', $sortDirections);
75
76
            if (count($sortColumns) != count($sortDirections)) {
77
                throw new \Exception('Count missmatch order columns/direction');
78
            }
79
80
            foreach ($sortColumns as $key => $sortColumn) {
81
                $sortDirection = strtoupper($sortDirections[$key]);
82
83
                if ($sortDirection != 'ASC' && $sortDirection != 'DESC') {
84
                    $sortDirection = 'ASC';
85
                }
86
87
                foreach ($this->getColumns() as $column) {
88
                    /* @var $column \ZfcDatagrid\Column\AbstractColumn */
89
                    if ($column->getUniqueId() == $sortColumn) {
90
                        $sortConditions[] = [
91
                            'sortDirection' => $sortDirection,
92
                            'column' => $column,
93
                        ];
94
95
                        $column->setSortActive($sortDirection);
96
                    }
97
                }
98
            }
99
        }
100
101
        if (!empty($sortConditions)) {
102
            $this->sortConditions = $sortConditions;
103
        } else {
104
            // No user sorting -> get default sorting
105
            $this->sortConditions = $this->getSortConditionsDefault();
106
        }
107
108
        return $this->sortConditions;
109
    }
110
111
    /**
112
     * @todo Make parameter config
113
     *
114
     * @see \ZfcDatagrid\Renderer\AbstractRenderer::getFilters()
115
     */
116
    public function getFilters()
117
    {
118
        if (is_array($this->filters)) {
119
            return $this->filters;
120
        }
121
122
        $request = $this->getRequest();
123
124
        $filters = [];
125
        if (($request->isPost() === true || $request->isGet() === true) && $request->getPost('toolbarFilters', $request->getQuery('toolbarFilters')) !== null) {
126
            foreach ($request->getPost('toolbarFilters', $request->getQuery('toolbarFilters')) as $uniqueId => $value) {
0 ignored issues
show
Bug introduced by
The expression $request->getPost('toolb...uery('toolbarFilters')) of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
127
                if ($value != '') {
128 View Code Duplication
                    foreach ($this->getColumns() as $column) {
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...
129
                        /* @var $column \ZfcDatagrid\Column\AbstractColumn */
130
                        if ($column->getUniqueId() == $uniqueId) {
131
                            $filter = new \ZfcDatagrid\Filter();
132
                            $filter->setFromColumn($column, $value);
133
134
                            $filters[] = $filter;
135
136
                            $column->setFilterActive($filter->getDisplayColumnValue());
137
                        }
138
                    }
139
                }
140
            }
141
        }
142
143
        if (!empty($filters)) {
144
            $this->filters = $filters;
145
        } else {
146
            // No user sorting -> get default sorting
147
            $this->filters = $this->getFiltersDefault();
148
        }
149
150
        return $this->filters;
151
    }
152
153
    /**
154
     * @return int
155
     *
156
     * @throws \Exception
157
     */
158
    public function getCurrentPageNumber()
159
    {
160
        $optionsRenderer = $this->getOptionsRenderer();
161
        $parameterNames = $optionsRenderer['parameterNames'];
162
163
        $request = $this->getRequest();
164
        if ($request instanceof HttpRequest) {
165
            $this->currentPageNumber = (int) $request->getPost($parameterNames['currentPage'], $request->getQuery($parameterNames['currentPage'], 1));
166
        }
167
168
        return (int) $this->currentPageNumber;
169
    }
170
171
    /**
172
     * @param Datagrid $grid
173
     */
174
    public function prepareViewModel(Datagrid $grid)
175
    {
176
        parent::prepareViewModel($grid);
177
178
        $options = $this->getOptionsRenderer();
179
180
        $viewModel = $this->getViewModel();
181
182
        // Check if the datarange picker is enabled
183
        if (isset($options['daterange']['enabled']) && $options['daterange']['enabled'] === true) {
184
            $dateRangeParameters = $options['daterange']['options'];
185
186
            $viewModel->setVariable('daterangeEnabled', true);
187
            $viewModel->setVariable('daterangeParameters', $dateRangeParameters);
188
        } else {
189
            $viewModel->setVariable('daterangeEnabled', false);
190
        }
191
    }
192
193
    /**
194
     * @return \Zend\View\Model\ViewModel
195
     */
196
    public function execute()
197
    {
198
        $viewModel = $this->getViewModel();
199
        $viewModel->setTemplate($this->getTemplate());
200
201
        return $viewModel;
202
    }
203
}
204