Completed
Pull Request — master (#546)
by Richard
09:47
created

Kint_Object   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 219
rs 8.2769
c 0
b 0
f 0
wmc 41
lcom 1
cbo 1

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B addRepresentation() 0 22 4
A replaceRepresentation() 0 9 2
A removeRepresentation() 0 4 1
A getRepresentation() 0 6 2
A getRepresentations() 0 4 1
A clearRepresentations() 0 4 1
A getType() 0 4 1
A getModifiers() 0 16 4
A getAccess() 0 11 4
A getName() 0 4 1
A getOperator() 0 12 4
A getSize() 0 4 1
B getValueShort() 0 10 6
A getAccessPath() 0 4 1
A blank() 0 11 2
A transplant() 0 19 1
A isSequential() 0 4 1
A sortByAccess() 0 11 1
A sortByName() 0 10 2

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
class Kint_Object
4
{
5
    const ACCESS_NONE = null;
6
    const ACCESS_PUBLIC = 'public';
7
    const ACCESS_PROTECTED = 'protected';
8
    const ACCESS_PRIVATE = 'private';
9
10
    const OPERATOR_NONE = null;
11
    const OPERATOR_ARRAY = '=>';
12
    const OPERATOR_OBJECT = '->';
13
    const OPERATOR_STATIC = '::';
14
15
    public $name;
16
    public $type;
17
    public $static = false;
18
    public $const = false;
19
    public $access = self::ACCESS_NONE;
20
    public $owner_class;
21
    public $access_path;
22
    public $operator = self::OPERATOR_NONE;
23
    public $reference = false;
24
    public $size = null;
25
    public $depth = 0;
26
    public $representations = array();
27
    public $value = null;
28
    public $hints = array();
29
30
    public function __construct()
31
    {
32
    }
33
34
    public function addRepresentation(Kint_Object_Representation $rep, $pos = null)
35
    {
36
        if (isset($this->representations[$rep->name])) {
37
            return false;
38
        }
39
40
        if ($this->value === null) {
41
            $this->value = $rep;
42
        }
43
44
        if ($pos === null) {
45
            $this->representations[$rep->name] = $rep;
46
        } else {
47
            $this->representations = array_merge(
48
                array_slice($this->representations, 0, $pos),
49
                array($rep->name => $rep),
50
                array_slice($this->representations, $pos)
51
            );
52
        }
53
54
        return true;
55
    }
56
57
    public function replaceRepresentation(Kint_Object_Representation $rep, $pos = null)
58
    {
59
        if ($pos === null) {
60
            $this->representations[$rep->name] = $rep;
61
        } else {
62
            $this->removeRepresentation($rep->name);
63
            $this->addRepresentation($rep, $pos);
64
        }
65
    }
66
67
    public function removeRepresentation($name)
68
    {
69
        unset($this->representations[$name]);
70
    }
71
72
    public function getRepresentation($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
73
    {
74
        if (isset($this->representations[$name])) {
75
            return $this->representations[$name];
76
        }
77
    }
78
79
    public function getRepresentations()
80
    {
81
        return $this->representations;
82
    }
83
84
    public function clearRepresentations()
85
    {
86
        $this->representations = array();
87
    }
88
89
    public function getType()
90
    {
91
        return $this->type;
92
    }
93
94
    public function getModifiers()
95
    {
96
        $out = $this->getAccess();
97
98
        if ($this->const) {
99
            $out .= ' const';
100
        }
101
102
        if ($this->static) {
103
            $out .= ' static';
104
        }
105
106
        if (strlen($out)) {
107
            return ltrim($out);
108
        }
109
    }
110
111
    public function getAccess()
112
    {
113
        switch ($this->access) {
114
            case self::ACCESS_PRIVATE:
115
                return 'private';
116
            case self::ACCESS_PROTECTED:
117
                return 'protected';
118
            case self::ACCESS_PUBLIC:
119
                return 'public';
120
        }
121
    }
122
123
    public function getName()
124
    {
125
        return $this->name;
126
    }
127
128
    public function getOperator()
129
    {
130
        if ($this->operator === self::OPERATOR_ARRAY) {
131
            return '=>';
132
        } elseif ($this->operator === self::OPERATOR_OBJECT) {
133
            return '->';
134
        } elseif ($this->operator === self::OPERATOR_STATIC) {
135
            return '::';
136
        }
137
138
        return;
139
    }
140
141
    public function getSize()
142
    {
143
        return $this->size;
144
    }
145
146
    public function getValueShort()
147
    {
148
        if ($rep = $this->value) {
149
            if ($this->type === 'boolean') {
150
                return $rep->contents ? 'true' : 'false';
151
            } elseif ($this->type === 'integer' || $this->type === 'double') {
152
                return $rep->contents;
153
            }
154
        }
155
    }
156
157
    public function getAccessPath()
158
    {
159
        return $this->access_path;
160
    }
161
162
    public static function blank($name = null, $access_path = null)
163
    {
164
        $o = new self();
165
        $o->name = $name;
166
        $o->access_path = $name;
167
        if ($access_path) {
168
            $o->access_path = $access_path;
169
        }
170
171
        return $o;
172
    }
173
174
    public function transplant(Kint_Object $new)
175
    {
176
        $new->name = $this->name;
177
        $new->size = $this->size;
178
        $new->access_path = $this->access_path;
179
        $new->access = $this->access;
180
        $new->static = $this->static;
181
        $new->const = $this->const;
182
        $new->type = $this->type;
183
        $new->depth = $this->depth;
184
        $new->owner_class = $this->owner_class;
185
        $new->operator = $this->operator;
186
        $new->reference = $this->reference;
187
        $new->value = $this->value;
188
        $new->representations += $this->representations;
189
        $new->hints = array_merge($this->hints, $new->hints);
190
191
        return $new;
192
    }
193
194
    public static function isSequential(array $array)
195
    {
196
        return array_keys($array) === range(0, count($array) - 1);
197
    }
198
199
    public static function sortByAccess(Kint_Object $a, Kint_Object $b)
200
    {
201
        static $sorts = array(
202
            self::ACCESS_PUBLIC => 1,
203
            self::ACCESS_PROTECTED => 2,
204
            self::ACCESS_PRIVATE => 3,
205
            self::ACCESS_NONE => 4,
206
        );
207
208
        return $sorts[$a->access] - $sorts[$b->access];
209
    }
210
211
    public static function sortByName(Kint_Object $a, Kint_Object $b)
212
    {
213
        $ret = strnatcasecmp($a->name, $b->name);
214
215
        if ($ret === 0) {
216
            return is_int($a->name) - is_int($b->name);
217
        }
218
219
        return $ret;
220
    }
221
}
222