Complex classes like Entry 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 Entry, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Entry implements ArrayAccess |
||
11 | { |
||
12 | /** |
||
13 | * Object identifiers keys |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $identifiers = array(); |
||
18 | |||
19 | /** |
||
20 | * Registry state of the object |
||
21 | * |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $state = RegistryState::UNKNOWN; |
||
25 | |||
26 | /** |
||
27 | * Initial object values |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $initialValues = array(); |
||
32 | |||
33 | /** |
||
34 | * @var integer |
||
35 | */ |
||
36 | protected $storeTs = null; |
||
37 | |||
38 | /** |
||
39 | * @var integer |
||
40 | */ |
||
41 | protected $stateTs = null; |
||
42 | |||
43 | /** |
||
44 | * @var integer |
||
45 | */ |
||
46 | protected $actionTs = null; |
||
47 | |||
48 | /** |
||
49 | * The action to happend (if any) |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $action = null; |
||
54 | |||
55 | /** |
||
56 | * Action priority |
||
57 | * |
||
58 | * @var integer |
||
59 | */ |
||
60 | protected $actionPriority; |
||
61 | |||
62 | /** |
||
63 | * The stored object |
||
64 | * |
||
65 | * @var mixed |
||
66 | */ |
||
67 | protected $object; |
||
68 | |||
69 | /** |
||
70 | * Class name of the stored object |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $className; |
||
75 | |||
76 | /** |
||
77 | * Extra data associated with the stored object |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $data = array(); |
||
82 | |||
83 | /** |
||
84 | * Constructor |
||
85 | * |
||
86 | * @param mixed $object |
||
87 | * @param array $ids |
||
88 | * @param int $state |
||
89 | * @param array $data |
||
90 | * |
||
91 | * @return void |
||
|
|||
92 | * @throws InvalidArgumentException if $object is not an object |
||
93 | */ |
||
94 | public function __construct($object, array $ids = array(), $state = RegistryState::UNKNOWN, array $data = array()) |
||
118 | |||
119 | /** |
||
120 | * State comparision |
||
121 | * |
||
122 | * @param integer $state |
||
123 | * |
||
124 | * @return boolean |
||
125 | */ |
||
126 | public function isState($state) |
||
130 | |||
131 | /** |
||
132 | * Changes current state |
||
133 | * |
||
134 | * @param integer $state |
||
135 | * |
||
136 | * @return Entry |
||
137 | */ |
||
138 | public function setState($state) |
||
145 | |||
146 | /** |
||
147 | * Returns the state of the object |
||
148 | * |
||
149 | * @return integer |
||
150 | */ |
||
151 | public function getState() |
||
155 | |||
156 | /** |
||
157 | * Returns the date of the current state |
||
158 | * |
||
159 | * @return \DateTime |
||
160 | */ |
||
161 | public function getStateDate() |
||
168 | |||
169 | /** |
||
170 | * Returns the date of the storage |
||
171 | * |
||
172 | * @return \DateTime |
||
173 | */ |
||
174 | public function getStoreDate() |
||
181 | |||
182 | /** |
||
183 | * Defines initial values of the object and mark it as "fresh" |
||
184 | * |
||
185 | * @return Entry |
||
186 | */ |
||
187 | public function fresh() |
||
196 | |||
197 | /** |
||
198 | * Test this Entry against identifiers and className |
||
199 | * |
||
200 | * @param array $identifiers |
||
201 | * @param string $className |
||
202 | * |
||
203 | * @return boolean |
||
204 | */ |
||
205 | public function match(array $identifiers, $className = null) |
||
220 | |||
221 | /** |
||
222 | * Test this Entry against an object instance |
||
223 | * |
||
224 | * @param mixed $object |
||
225 | * |
||
226 | * @return boolean |
||
227 | */ |
||
228 | public function matchObject($object) |
||
232 | |||
233 | /** |
||
234 | * Tells if the objects values has been changed |
||
235 | * |
||
236 | * @return boolean |
||
237 | */ |
||
238 | public function hasChanged() |
||
260 | |||
261 | /** |
||
262 | * Returns an array of changed values |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | public function getChangedValues() |
||
284 | |||
285 | /** |
||
286 | * Tells if an action is defined |
||
287 | * |
||
288 | * @return boolean |
||
289 | */ |
||
290 | public function hasAction() |
||
294 | |||
295 | /** |
||
296 | * Defines an action |
||
297 | * |
||
298 | * @param string $action |
||
299 | * |
||
300 | * @return Entry |
||
301 | */ |
||
302 | public function setAction($action, $priority) |
||
310 | |||
311 | /** |
||
312 | * Returns the defined action if any, or null |
||
313 | * |
||
314 | * @return string|null |
||
315 | */ |
||
316 | public function getAction() |
||
320 | |||
321 | /** |
||
322 | * |
||
323 | * @return integer |
||
324 | */ |
||
325 | public function getActionPriority() |
||
329 | |||
330 | /** |
||
331 | * Getter for associated data |
||
332 | * |
||
333 | * @param string $key |
||
334 | * @param mixed $default |
||
335 | * |
||
336 | * @return mixed|null |
||
337 | */ |
||
338 | public function data($key, $default = false) |
||
346 | |||
347 | /** |
||
348 | * Merge datas with the existing ones |
||
349 | * |
||
350 | * @param array $data |
||
351 | * |
||
352 | * @return Entry |
||
353 | */ |
||
354 | public function mergeData(array $data) |
||
360 | |||
361 | /** |
||
362 | * Factory method |
||
363 | * |
||
364 | * @param mixed $object |
||
365 | * @param array $ids |
||
366 | * @param int $state |
||
367 | * @param array $data |
||
368 | * |
||
369 | * @return Entry |
||
370 | */ |
||
371 | public static function factory($object, array $ids = array(), $state = RegistryState::UNKNOWN, array $data = array()) |
||
375 | |||
376 | /** |
||
377 | * (PHP 5 >= 5.0.0)<br/> |
||
378 | * Whether a offset exists |
||
379 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
380 | * @param mixed $offset <p> |
||
381 | * An offset to check for. |
||
382 | * </p> |
||
383 | * @return boolean true on success or false on failure. |
||
384 | * </p> |
||
385 | * <p> |
||
386 | * The return value will be casted to boolean if non-boolean was returned. |
||
387 | */ |
||
388 | public function offsetExists($offset) |
||
392 | |||
393 | /** |
||
394 | * (PHP 5 >= 5.0.0)<br/> |
||
395 | * Offset to retrieve |
||
396 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
397 | * @param mixed $offset <p> |
||
398 | * The offset to retrieve. |
||
399 | * </p> |
||
400 | * @return mixed Can return all value types. |
||
401 | */ |
||
402 | public function offsetGet($offset) |
||
406 | |||
407 | /** |
||
408 | * (PHP 5 >= 5.0.0)<br/> |
||
409 | * Offset to set |
||
410 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
411 | * @param mixed $offset <p> |
||
412 | * The offset to assign the value to. |
||
413 | * </p> |
||
414 | * @param mixed $value <p> |
||
415 | * The value to set. |
||
416 | * </p> |
||
417 | * @return void |
||
418 | */ |
||
419 | public function offsetSet($offset, $value) |
||
423 | |||
424 | /** |
||
425 | * (PHP 5 >= 5.0.0)<br/> |
||
426 | * Offset to unset |
||
427 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
428 | * @param mixed $offset <p> |
||
429 | * The offset to unset. |
||
430 | * </p> |
||
431 | * @return void |
||
432 | */ |
||
433 | public function offsetUnset($offset) |
||
437 | |||
438 | /** |
||
439 | * Returns the stored object |
||
440 | * |
||
441 | * @return mixed |
||
442 | */ |
||
443 | public function getObject() |
||
447 | |||
448 | /** |
||
449 | * String representation of the Entry |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public function __toString() |
||
457 | |||
458 | /** |
||
459 | * @return array |
||
460 | */ |
||
461 | public function getIdentifiers() |
||
465 | |||
466 | /** |
||
467 | * @param array $identifiers |
||
468 | * |
||
469 | * @return Entry |
||
470 | */ |
||
471 | public function setIdentifiers(array $identifiers) |
||
477 | } |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.