ListBuilder   C
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 259
Duplicated Lines 16.6 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 54
lcom 1
cbo 5
dl 43
loc 259
rs 6.4799
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getYamlKey() 0 4 1
A getFormType() 0 9 2
A getFilterColumns() 0 9 2
A findFilterColumns() 0 17 5
A addFilterColumn() 0 4 1
A getFilterColumnsCredentials() 0 16 3
A getScopes() 0 4 1
A getScopeColumns() 0 9 2
A findScopeColumns() 0 9 2
A getScopesDisplayColumns() 0 17 6
A addScopeColumn() 0 4 1
A getBatchActions() 0 9 2
B setUserBatchActionConfiguration() 22 22 7
A addBatchAction() 0 4 1
A findBatchActions() 21 21 4
A getExcelActions() 0 9 2
A fillExportActions() 0 16 6
A getExportParamsForKey() 0 9 6

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ListBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ListBuilder, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Builder\Admin;
4
5
use Admingenerator\GeneratorBundle\Generator\Column;
6
use Admingenerator\GeneratorBundle\Generator\Action;
7
use Admingenerator\GeneratorBundle\Generator\Action\Generic\ExcelAction;
8
9
/**
10
 * This builder generates php for list actions
11
 *
12
 * @author cedric Lombardot
13
 * @author Piotr Gołębiewski <[email protected]>
14
 * @author Stéphane Escandell <[email protected]>
15
 */
16
class ListBuilder extends BaseBuilder
17
{
18
   /**
19
    * @var array
20
    */
21
    protected $excelActions = null;
22
23
    /**
24
     * @var array
25
     */
26
    protected $batchActions = null;
27
28
    /**
29
     * @var array
30
     */
31
    protected $scopeColumns = null;
32
33
    /**
34
     * @var array
35
     */
36
    protected $filterColumns = null;
37
38
    /**
39
     * (non-PHPdoc)
40
     * @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey()
41
     */
42
    public function getYamlKey()
43
    {
44
        return 'list';
45
    }
46
47
    /**
48
     * Retrieve the FQCN formType used by this builder
49
     *
50
     * @return string
51
     */
52
    public function getFormType()
53
    {
54
        return sprintf(
55
            '%s%s\\Form\Type\\%s\\FiltersType',
56
            $this->getVariable('namespace_prefix') ? $this->getVariable('namespace_prefix') . '\\' : '',
57
            $this->getVariable('bundle_name'),
58
            $this->getBaseGeneratorName()
59
        );
60
    }
61
62
    public function getFilterColumns()
63
    {
64
        if (null === $this->filterColumns) {
65
            $this->filterColumns = array();
66
            $this->findFilterColumns();
67
        }
68
69
        return $this->filterColumns;
70
    }
71
72
    protected function findFilterColumns()
73
    {
74
        $columnsName = $this->getVariable('filters');
75
        $fromFilterConfiguration = true;
76
        if (null === $columnsName) {
77
            $fromFilterConfiguration = false;
78
            $columnsName = $this->getAllFields();
79
        }
80
81
        foreach ($columnsName as $columnName) {
82
            $column = $this->createColumn($columnName, true);
83
84
            if ($fromFilterConfiguration || $column->isFilterable()) {
85
                $this->addFilterColumn($column);
86
            }
87
        }
88
    }
89
90
    protected function addFilterColumn(Column $column)
91
    {
92
        $this->filterColumns[$column->getName()] = $column;
93
    }
94
95
    public function getFilterColumnsCredentials()
96
    {
97
        $credentials = array();
98
99
        foreach($this->getFilterColumns() as $column) {
100
            if (! $filterCredentials = $column->getFiltersCredentials()) {
101
                // If one column has no Credentials constraint, we always
102
                // have to display the filter panel
103
                return array();
104
            }
105
106
            $credentials[] = $filterCredentials;
107
        }
108
109
        return $credentials;
110
    }
111
112
    /**
113
     * Find scopes parameters
114
     */
115
    public function getScopes()
116
    {
117
        return $this->getGenerator()->getFromYaml('builders.list.params.scopes');
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getScopeColumns()
124
    {
125
        if (null === $this->scopeColumns) {
126
            $this->scopeColumns = array();
127
            $this->findScopeColumns();
128
        }
129
130
        return $this->scopeColumns;
131
    }
132
133
    protected function findScopeColumns()
134
    {
135
        foreach ($this->getScopesDisplayColumns() as $columnName) {
136
            $column = $this->createColumn($columnName, true);
137
138
            // Set the user parameters
139
            $this->addScopeColumn($column);
140
        }
141
    }
142
143
    /**
144
     * @return array Scopes display column names
145
     */
146
    protected function getScopesDisplayColumns()
147
    {
148
        $scopeGroups = $this->getGenerator()->getFromYaml('builders.list.params.scopes', array());
149
        $scopeColumns = array();
150
151
        foreach ($scopeGroups as $scopeGroup) {
152
            foreach ($scopeGroup as $scopeFilter) {
153
                if (array_key_exists('filters', $scopeFilter) && is_array($scopeFilter['filters'])) {
154
                    foreach ($scopeFilter['filters'] as $field => $value) {
155
                        $scopeColumns[] = $field;
156
                    }
157
                }
158
            }
159
        }
160
161
        return $scopeColumns;
162
    }
163
164
    protected function addScopeColumn(Column $column)
165
    {
166
        $this->scopeColumns[$column->getName()] = $column;
167
    }
168
169
    /**
170
     * Return a list of batch action from list.batch_actions
171
     * @return array
172
     */
173
    public function getBatchActions()
174
    {
175
        if (null === $this->batchActions) {
176
            $this->batchActions = array();
177
            $this->findBatchActions();
178
        }
179
180
        return $this->batchActions;
181
    }
182
183 View Code Duplication
    protected function setUserBatchActionConfiguration(Action $action)
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...
184
    {
185
        $batchActions = $this->getVariable('batch_actions', array());
186
        $builderOptions = is_array($batchActions) && array_key_exists($action->getName(), $batchActions)
187
            ? $batchActions[$action->getName()]
188
            : array();
189
190
        $globalOptions = $this->getGenerator()->getFromYaml(
191
            'params.batch_actions.'.$action->getName(),
192
            array()
193
        );
194
195
        if (null !== $builderOptions) {
196
            foreach ($builderOptions as $option => $value) {
197
                $action->setProperty($option, $value);
198
            }
199
        } elseif (null !== $globalOptions) {
200
            foreach ($globalOptions as $option => $value) {
201
                $action->setProperty($option, $value);
202
            }
203
        }
204
    }
205
206
    protected function addBatchAction(Action $action)
207
    {
208
        $this->batchActions[$action->getName()] = $action;
209
    }
210
211 View Code Duplication
    protected function findBatchActions()
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...
212
    {
213
        $batchActions = $this->getVariable('batch_actions', array());
214
215
        foreach ($batchActions as $actionName => $actionParams) {
216
            $action = $this->findBatchAction($actionName);
217
218
            if (!$action) {
219
                $action = new Action($actionName);
220
            }
221
222
            if ($globalCredentials = $this->getGenerator()->getFromYaml('params.credentials')) {
223
                // If generator is globally protected by credentials
224
                // batch actions are also protected
225
                $action->setCredentials($globalCredentials);
226
            }
227
228
            $this->setUserBatchActionConfiguration($action);
229
            $this->addBatchAction($action);
230
        }
231
    }
232
233
  /**
234
   * Return a list of actions from excel.export
235
   * 
236
   * @return array
237
   */
238
  public function getExcelActions()
239
  {
240
      if (null === $this->excelActions) {
241
          $this->excelActions = array();
242
          $this->fillExportActions();
243
      }
244
245
      return $this->excelActions;
246
  }
247
248
  protected function fillExportActions()
249
  {
250
      $export = $this->getGenerator()->getFromYaml('builders.excel.params.export', []);
251
      if (!count($export)) return;
252
253
      foreach ($export as $keyName => $params ) {
254
          if (!isset($params['show_button']) || (isset($params['show_button']) && filter_var($params['show_button'], FILTER_VALIDATE_BOOLEAN))) {
255
              $action = new ExcelAction($keyName, $this);
256
              $action->setCredentials($this->getExportParamsForKey($keyName, 'credentials', 'AdmingenAllowed'));
257
              $action->setClass($this->getExportParamsForKey($keyName, 'class', 'btn-info'));
258
              $action->setIcon($this->getExportParamsForKey($keyName, 'icon', 'fa-file-excel-o'));
259
              $action->setLabel($this->getExportParamsForKey($keyName, 'label', 'action.generic.excel'));
260
              $this->excelActions[$keyName] = $action;
261
          }
262
      }
263
  }
264
265
  public function getExportParamsForKey($key, $name, $default)
266
  {
267
      if (!$key) return $default;
268
269
      $export = $this->getGenerator()->getFromYaml('builders.excel.params.export', []);
270
      if (!count($export) || !isset($export[$key]) || !count($export[$key]) || !isset($export[$key][$name])) return $default;
271
272
      return $export[$key][$name];
273
  }
274
}
275