Total Complexity | 40 |
Total Lines | 217 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BasicObject 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.
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 BasicObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class BasicObject |
||
31 | { |
||
32 | const ACCESS_NONE = null; |
||
33 | const ACCESS_PUBLIC = 1; |
||
34 | const ACCESS_PROTECTED = 2; |
||
35 | const ACCESS_PRIVATE = 3; |
||
36 | |||
37 | const OPERATOR_NONE = null; |
||
38 | const OPERATOR_ARRAY = 1; |
||
39 | const OPERATOR_OBJECT = 2; |
||
40 | const OPERATOR_STATIC = 3; |
||
41 | |||
42 | public $name; |
||
43 | public $type; |
||
44 | public $static = false; |
||
45 | public $const = false; |
||
46 | public $access = self::ACCESS_NONE; |
||
47 | public $owner_class; |
||
48 | public $access_path; |
||
49 | public $operator = self::OPERATOR_NONE; |
||
50 | public $reference = false; |
||
51 | public $depth = 0; |
||
52 | public $size; |
||
53 | public $value; |
||
54 | public $hints = array(); |
||
55 | |||
56 | protected $representations = array(); |
||
57 | |||
58 | public function __construct() |
||
59 | { |
||
60 | } |
||
61 | |||
62 | public function addRepresentation(Representation $rep, $pos = null) |
||
63 | { |
||
64 | if (isset($this->representations[$rep->getName()])) { |
||
65 | return false; |
||
66 | } |
||
67 | |||
68 | if (null === $pos) { |
||
69 | $this->representations[$rep->getName()] = $rep; |
||
70 | } else { |
||
71 | $this->representations = \array_merge( |
||
72 | \array_slice($this->representations, 0, $pos), |
||
73 | array($rep->getName() => $rep), |
||
74 | \array_slice($this->representations, $pos) |
||
75 | ); |
||
76 | } |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | |||
81 | public function replaceRepresentation(Representation $rep, $pos = null) |
||
82 | { |
||
83 | if (null === $pos) { |
||
84 | $this->representations[$rep->getName()] = $rep; |
||
85 | } else { |
||
86 | $this->removeRepresentation($rep); |
||
87 | $this->addRepresentation($rep, $pos); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function removeRepresentation($rep) |
||
92 | { |
||
93 | if ($rep instanceof Representation) { |
||
94 | unset($this->representations[$rep->getName()]); |
||
95 | } elseif (\is_string($rep)) { |
||
96 | unset($this->representations[$rep]); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | public function getRepresentation($name) |
||
101 | { |
||
102 | if (isset($this->representations[$name])) { |
||
103 | return $this->representations[$name]; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public function getRepresentations() |
||
108 | { |
||
109 | return $this->representations; |
||
110 | } |
||
111 | |||
112 | public function clearRepresentations() |
||
113 | { |
||
114 | $this->representations = array(); |
||
115 | } |
||
116 | |||
117 | public function getType() |
||
118 | { |
||
119 | return $this->type; |
||
120 | } |
||
121 | |||
122 | public function getModifiers() |
||
123 | { |
||
124 | $out = $this->getAccess(); |
||
125 | |||
126 | if ($this->const) { |
||
127 | $out .= ' const'; |
||
128 | } |
||
129 | |||
130 | if ($this->static) { |
||
131 | $out .= ' static'; |
||
132 | } |
||
133 | |||
134 | if (\strlen($out)) { |
||
|
|||
135 | return \ltrim($out); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public function getAccess() |
||
148 | } |
||
149 | } |
||
150 | |||
151 | public function getName() |
||
152 | { |
||
153 | return $this->name; |
||
154 | } |
||
155 | |||
156 | public function getOperator() |
||
157 | { |
||
158 | switch ($this->operator) { |
||
159 | case self::OPERATOR_ARRAY: |
||
160 | return '=>'; |
||
161 | case self::OPERATOR_OBJECT: |
||
162 | return '->'; |
||
163 | case self::OPERATOR_STATIC: |
||
164 | return '::'; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | public function getSize() |
||
169 | { |
||
170 | return $this->size; |
||
171 | } |
||
172 | |||
173 | public function getValueShort() |
||
174 | { |
||
175 | if ($rep = $this->value) { |
||
176 | if ('boolean' === $this->type) { |
||
177 | return $rep->contents ? 'true' : 'false'; |
||
178 | } |
||
179 | |||
180 | if ('integer' === $this->type || 'double' === $this->type) { |
||
181 | return $rep->contents; |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | public function getAccessPath() |
||
189 | } |
||
190 | |||
191 | public function transplant(BasicObject $old) |
||
192 | { |
||
193 | $this->name = $old->name; |
||
194 | $this->size = $old->size; |
||
195 | $this->access_path = $old->access_path; |
||
196 | $this->access = $old->access; |
||
197 | $this->static = $old->static; |
||
198 | $this->const = $old->const; |
||
199 | $this->type = $old->type; |
||
200 | $this->depth = $old->depth; |
||
201 | $this->owner_class = $old->owner_class; |
||
202 | $this->operator = $old->operator; |
||
203 | $this->reference = $old->reference; |
||
204 | $this->value = $old->value; |
||
205 | $this->representations += $old->representations; |
||
206 | $this->hints = \array_merge($this->hints, $old->hints); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Creates a new basic object with a name and access path. |
||
211 | * |
||
212 | * @param null|string $name |
||
213 | * @param null|string $access_path |
||
214 | * |
||
215 | * @return \Kint\Object\BasicObject |
||
216 | */ |
||
217 | public static function blank($name = null, $access_path = null) |
||
224 | } |
||
225 | |||
226 | public static function sortByAccess(BasicObject $a, BasicObject $b) |
||
227 | { |
||
228 | static $sorts = array( |
||
229 | self::ACCESS_PUBLIC => 1, |
||
230 | self::ACCESS_PROTECTED => 2, |
||
231 | self::ACCESS_PRIVATE => 3, |
||
232 | self::ACCESS_NONE => 4, |
||
233 | ); |
||
234 | |||
235 | return $sorts[$a->access] - $sorts[$b->access]; |
||
236 | } |
||
237 | |||
238 | public static function sortByName(BasicObject $a, BasicObject $b) |
||
247 | } |
||
248 | } |
||
249 |