Column   F
last analyzed

Complexity

Total Complexity 67

Size/Duplication

Total Lines 455
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 67
lcom 1
cbo 2
dl 0
loc 455
rs 3.04
c 0
b 0
f 0

56 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setProperty() 0 10 2
A getName() 0 4 1
A getGetter() 0 4 2
A setGetter() 0 4 1
A getLabel() 0 6 3
A setLabel() 0 4 1
A getHelp() 0 4 1
A setHelp() 0 4 1
A isSortable() 0 4 1
A isFilterable() 0 4 1
A isReal() 0 4 1
A getSortable() 0 4 1
A setSortable() 0 4 1
A getSortOn() 0 4 2
A setSortOn() 0 4 1
A getFilterable() 0 4 1
A setFilterable() 0 4 1
A getFilterOn() 0 4 1
A setFilterOn() 0 4 1
A humanize() 0 4 1
A setDbType() 0 4 1
A getDbType() 0 4 1
A setDbFormat() 0 4 1
A getDbFormat() 0 4 1
A setFormType() 0 4 1
A getFormType() 0 4 1
A setFormOptions() 0 4 1
A getFormOptions() 0 4 1
A setFilterType() 0 4 1
A getFilterType() 0 4 1
A setFilterOptions() 0 4 1
A getFilterOptions() 0 4 1
A setLocalizedDateFormat() 0 4 1
A getLocalizedDateFormat() 0 4 1
A setLocalizedTimeFormat() 0 4 1
A getLocalizedTimeFormat() 0 4 1
A setAddFormOptions() 0 6 2
A setAddFilterOptions() 0 6 2
A setExtras() 0 4 1
A getExtras() 0 4 1
A setSortType() 0 4 1
A getSortType() 0 4 1
A getCustomView() 0 4 1
A setCustomView() 0 4 1
A setPrimaryKey() 0 4 1
A getPrimaryKey() 0 4 1
A setCredentials() 0 4 1
A getCredentials() 0 4 1
A setFiltersCredentials() 0 4 1
A getFiltersCredentials() 0 8 2
A setGridClass() 0 4 1
A getGridClass() 0 4 1
A setManyToMany() 0 4 1
A getManyToMany() 0 4 1
A parseOption() 0 17 4

How to fix   Complexity   

Complex Class

Complex classes like Column 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 Column, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Generator;
4
5
use Admingenerator\GeneratorBundle\Exception\InvalidOptionException;
6
7
/**
8
 * This class describe a column
9
 *
10
 * @author cedric Lombardot
11
 * @author Piotr Gołębiewski <[email protected]>
12
 * @author Stéphane Escandell <[email protected]>
13
 */
14
use Doctrine\Common\Util\Inflector;
15
16
class Column
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $sortable = true;
27
28
    /**
29
     * @var string
30
     */
31
    protected $sortOn;
32
33
    /**
34
     * @var string
35
     */
36
    protected $sortType = 'default';
37
38
    /**
39
     * @var bool
40
     */
41
    protected $filterable = false;
42
43
    /**
44
     * @var string
45
     */
46
    protected $filterOn;
47
48
    /**
49
     * @var string
50
     */
51
    protected $dbType;
52
53
    /**
54
     * If set, formats field for scopes and filters. The formatting is a simple
55
     * sprintf with one string argument (field name).
56
     *
57
     * Example:
58
     *    for field:  "createdAt"
59
     *    dbFormat:   "DATE(%s)""
60
     *    the output will be "DATE(createdAt)"
61
     *
62
     * If undefined, the field will not be formatted.
63
     *
64
     * Since the functions may vary in different Database types, Admingenerator does not,
65
     * by default, format fields in any way. It is up to developer to implement this for his fields.
66
     *
67
     * Note: this feature was created mainly for Date/DateTime fields.
68
     */
69
    protected $dbFormat;
70
71
    /**
72
     * @var string
73
     */
74
    protected $customView = null;
75
76
    /**
77
     * @var string
78
     */
79
    protected $formType;
80
81
    /**
82
     * @var string
83
     */
84
    protected $filterType;
85
86
    /**
87
     * @var array
88
     */
89
    protected $formOptions = array();
90
91
    /**
92
     * @var array
93
     */
94
    protected $filterOptions = array();
95
96
    /**
97
     * @var string
98
     */
99
    protected $getter;
100
101
    /**
102
     * @var string
103
     */
104
    protected $label = null;
105
106
    /**
107
     * @var string
108
     */
109
    protected $help;
110
111
    /**
112
     * @var string
113
     */
114
    protected $localizedDateFormat;
115
116
    /**
117
     * @var string
118
     */
119
    protected $localizedTimeFormat;
120
121
    /**
122
     * @var string
123
     */
124
    protected $primaryKey = null;
125
126
    /**
127
     * For special columns template
128
     *
129
     * @var array
130
     */
131
    protected $extras = array();
132
133
    /**
134
     * @var string
135
     */
136
    protected $credentials = 'AdmingenAllowed';
137
138
    /**
139
     * @var string
140
     */
141
    protected $filtersCredentials = false;
142
143
    /**
144
     * @var string
145
     */
146
    protected $gridClass = '';
147
148
    /* Used for more verbose error messages */
149
    protected $debug = array();
150
151
    protected $manyToMany = false;
152
153
    /**
154
     * @param string $name
155
     * @param array  $debug
156
     */
157
    public function __construct($name, $debug)
158
    {
159
        $this->name     = $name;
160
        $this->filterOn = $name;
161
        $this->debug    = $debug;
162
    }
163
164
    public function setProperty($option, $value)
165
    {
166
        $setter = 'set'.Inflector::classify($option);
167
168
        if (method_exists($this, $setter)) {
169
            $this->{$setter}($value);
170
        } else {
171
            throw new InvalidOptionException($option, $this->name, $this->debug['generator'], $this->debug['builder']);
172
        }
173
    }
174
175
    public function getName()
176
    {
177
        return $this->name;
178
    }
179
180
    public function getGetter()
181
    {
182
        return $this->getter ? $this->getter : Inflector::camelize($this->name);
183
    }
184
185
    public function setGetter($getter)
186
    {
187
        $this->getter = $getter;
188
    }
189
190
    public function getLabel()
191
    {
192
        return false !== $this->label && empty($this->label)
193
            ? $this->humanize($this->getName())
194
            : $this->label;
195
    }
196
197
    public function setLabel($label)
198
    {
199
        return $this->label = $label;
200
    }
201
202
    public function getHelp()
203
    {
204
        return $this->help;
205
    }
206
207
    public function setHelp($help)
208
    {
209
        return $this->help = $help;
210
    }
211
212
    public function isSortable()
213
    {
214
        return $this->sortable;
215
    }
216
217
    public function isFilterable()
218
    {
219
        return $this->filterable;
220
    }
221
222
    public function isReal()
223
    {
224
        return $this->dbType != 'virtual';
225
    }
226
227
    public function getSortable()
228
    {
229
        return $this->sortable;
230
    }
231
232
    public function setSortable($sortable)
233
    {
234
        return $this->sortable = filter_var($sortable, FILTER_VALIDATE_BOOLEAN);
235
    }
236
237
    public function getSortOn()
238
    {
239
        return $this->sortOn != "" ? $this->sortOn : $this->name;
240
    }
241
242
    public function setSortOn($sort_on)
243
    {
244
        return $this->sortOn = $sort_on;
245
    }
246
247
    public function getFilterable()
248
    {
249
        return $this->filterable;
250
    }
251
252
    public function setFilterable($filterable)
253
    {
254
        return $this->filterable = filter_var($filterable, FILTER_VALIDATE_BOOLEAN);
255
    }
256
257
    public function getFilterOn()
258
    {
259
        return $this->filterOn;
260
    }
261
262
    public function setFilterOn($filterOn)
263
    {
264
        return $this->filterOn = $filterOn;
265
    }
266
267
    /**
268
     * @param string $text
269
     */
270
    private function humanize($text)
271
    {
272
        return ucfirst(str_replace('_', ' ', $text));
273
    }
274
275
    public function setDbType($dbType)
276
    {
277
        $this->dbType = $dbType;
278
    }
279
280
    public function getDbType()
281
    {
282
        return $this->dbType;
283
    }
284
285
    public function setDbFormat($dbFormat)
286
    {
287
        $this->dbFormat = $dbFormat;
288
    }
289
290
    public function getDbFormat()
291
    {
292
        return $this->dbFormat;
293
    }
294
295
    public function setFormType($formType)
296
    {
297
        $this->formType = $formType;
298
    }
299
300
    public function getFormType()
301
    {
302
        return $this->formType;
303
    }
304
305
    public function setFormOptions($formOptions)
306
    {
307
        $this->formOptions = $formOptions;
308
    }
309
310
    public function getFormOptions()
311
    {
312
        return $this->formOptions;
313
    }
314
315
    public function setFilterType($filterType)
316
    {
317
        $this->filterType = $filterType;
318
    }
319
320
    public function getFilterType()
321
    {
322
        return $this->filterType;
323
    }
324
325
    public function setFilterOptions($filterOptions)
326
    {
327
        $this->filterOptions = $filterOptions;
328
    }
329
330
    public function getFilterOptions()
331
    {
332
        return $this->filterOptions;
333
    }
334
335
    public function setLocalizedDateFormat($localizedDateFormat)
336
    {
337
        $this->localizedDateFormat = $localizedDateFormat;
338
    }
339
340
    public function getLocalizedDateFormat()
341
    {
342
        return $this->localizedDateFormat;
343
    }
344
345
    public function setLocalizedTimeFormat($localizedTimeFormat)
346
    {
347
        $this->localizedTimeFormat = $localizedTimeFormat;
348
    }
349
350
    public function getLocalizedTimeFormat()
351
    {
352
        return $this->localizedTimeFormat;
353
    }
354
355
    public function setAddFormOptions(array $additionalOptions = array())
356
    {
357
        foreach ($additionalOptions as $name => $option) {
358
            $this->formOptions[$name] = $this->parseOption($option);
359
        }
360
    }
361
362
    public function setAddFilterOptions(array $additionalOptions = array())
363
    {
364
        foreach ($additionalOptions as $name => $option) {
365
           $this->filterOptions[$name] = $this->parseOption($option);
366
        }
367
    }
368
369
    public function setExtras(array $values)
370
    {
371
        $this->extras = $values;
372
    }
373
374
    public function getExtras()
375
    {
376
        return $this->extras;
377
    }
378
379
    public function setSortType($type)
380
    {
381
        $this->sortType = $type;
382
    }
383
384
    public function getSortType()
385
    {
386
        return $this->sortType;
387
    }
388
389
    public function getCustomView()
390
    {
391
        return $this->customView;
392
    }
393
394
    public function setCustomView($customView)
395
    {
396
        $this->customView = $customView;
397
    }
398
399
    public function setPrimaryKey($primaryKey)
400
    {
401
        $this->primaryKey = $primaryKey;
402
    }
403
404
    public function getPrimaryKey()
405
    {
406
        return $this->primaryKey;
407
    }
408
409
    public function setCredentials($credentials = '')
410
    {
411
        return $this->credentials = $credentials;
412
    }
413
414
    public function getCredentials()
415
    {
416
        return $this->credentials;
417
    }
418
419
    public function setFiltersCredentials($credentials = '')
420
    {
421
        $this->filtersCredentials = $credentials;
422
    }
423
424
    public function getFiltersCredentials()
425
    {
426
        if (false === $this->filtersCredentials) {
427
            return $this->credentials;
428
        }
429
430
        return $this->filtersCredentials;
431
    }
432
433
    public function setGridClass($gridClass)
434
    {
435
        $this->gridClass = $gridClass;
436
    }
437
438
    public function getGridClass()
439
    {
440
        return $this->gridClass;
441
    }
442
443
    public function setManyToMany($manyToMany)
444
    {
445
        $this->manyToMany = $manyToMany;
446
    }
447
448
    public function getManyToMany()
449
    {
450
        return $this->manyToMany;
451
    }
452
453
    protected function parseOption($option)
454
    {
455
        if (!is_array($option)) {
456
            return $option;
457
        }
458
459
        foreach ($option as $k => $v) {
460
            if (preg_match('/^\.(.+)/i', $k, $matches)) {
461
                // enable to call php function to build your form options
462
                // Only if key STARTS with a dot (.). Values are used a params for the
463
                // function. See tests for sample.
464
                return call_user_func_array($matches[1], $v);
465
            }
466
        }
467
468
        return $option;
469
    }
470
}
471