Passed
Push — master ( 1c101f...c673c5 )
by Matthew
11:42
created

GridColumn   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 120
Duplicated Lines 9.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 11
loc 120
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 5
A isSearchable() 0 4 1
A format() 0 8 2
D _format() 11 35 10
A getFormatter() 0 4 1
A setFormatter() 0 4 1
A toArray() 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 Dtc\GridBundle\Grid\Column;
4
5
use Dtc\GridBundle\Grid\Source\GridSourceInterface;
6
7
class GridColumn extends AbstractGridColumn
8
{
9
    protected $formatter;
10
    protected $field;
11
    protected $label;
12
    protected $searchable;
13
    protected $order;
14
15
    /**
16
     * GridColumn constructor.
17
     *
18
     * @param $field
19
     * @param string|null $label
20
     * @param mixed       $formatter
21
     * @param array|null  $options
22
     * @param bool        $searchable
23
     * @param int|null    $order      If there are columns that have an order mixed with columns of 'null' order, the null ones will appear last
24
     */
25
    public function __construct($field,
26
            $label = null,
27
            $formatter = null,
28
            array $options = null,
29
            $searchable = true,
30
            $order = null)
31
    {
32
        $this->field = $field;
33
34
        if (!$label) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $label of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35
            $label = ucwords($field);
36
        }
37
38
        $this->label = $label;
39
        $this->formatter = $formatter;
40
        if ($options) {
41
            $this->setOptions($options);
42
        }
43
44
        $this->searchable = $searchable;
45
46
        $this->order = $order;
47
48
        if ($this->order !== null && !is_int($this->order)) {
49
            throw new \InvalidArgumentException('$order must be an integer or null');
50
        }
51
    }
52
53
    public function isSearchable()
54
    {
55
        return $this->searchable;
56
    }
57
58
    public function format($object, GridSourceInterface $gridsource)
59
    {
60
        if ($this->formatter) {
61
            return call_user_func($this->formatter, $object, $this);
62
        } else {
63
            return $this->_format($object);
64
        }
65
    }
66
67
    protected function _format($object)
68
    {
69
        $value = null;
70
        if (is_array($object)) {
71
            if (isset($object[$this->field])) {
72
                $value = $object[$this->field];
73
            }
74
        } elseif (is_object($object)) {
75
            $funcPrefix = array(
76
                    'get',
77
                    'is',
78
                    'has',
79
            );
80
            foreach ($funcPrefix as $prefix) {
81
                $methodName = $prefix.$this->field;
82
                if (method_exists($object, $methodName)) {
83
                    $value = $object->$methodName();
84
                    break;
85
                }
86
            }
87
        }
88 View Code Duplication
        if (is_object($value)) {
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
            if ($value instanceof \DateTime) {
90
                return $value->format(\DateTime::ISO8601);
91
            }
92
93
            return 'object: '.get_class($value);
94
        } elseif (is_scalar($value)) {
95
            return $value;
96
        } elseif (is_array($value)) {
97
            return 'array: '.print_r($value, true);
98
        }
99
100
        return $value;
101
    }
102
103
    /**
104
     * @return $formatter
0 ignored issues
show
Documentation introduced by
The doc-type $formatter could not be parsed: Unknown type name "$formatter" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
105
     */
106
    public function getFormatter()
107
    {
108
        return $this->formatter;
109
    }
110
111
    /**
112
     * @param $formatter
113
     */
114
    public function setFormatter($formatter)
115
    {
116
        $this->formatter = $formatter;
117
    }
118
119
    public function toArray()
120
    {
121
        $retVal = (array) $this;
122
        unset($retVal['formatter']);
123
124
        return $retVal;
125
    }
126
}
127