Complex classes like Accessor 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 Accessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Accessor |
||
50 | { |
||
51 | /** |
||
52 | * The object to access |
||
53 | * |
||
54 | * @var mixed |
||
55 | */ |
||
56 | protected $object; |
||
57 | |||
58 | /** |
||
59 | * Reflector for the object |
||
60 | * |
||
61 | * @var \ReflectionObject |
||
62 | */ |
||
63 | protected $reflector; |
||
64 | |||
65 | /** |
||
66 | * Override properties visibility ? |
||
67 | * |
||
68 | * @var boolean |
||
69 | */ |
||
70 | protected $force = false; |
||
71 | |||
72 | /** |
||
73 | * Constructor |
||
74 | * |
||
75 | * @param mixed $object The object we want to access |
||
76 | * |
||
77 | * @throws \InvalidArgumentException if $object is not an object |
||
78 | * @return void |
||
|
|||
79 | */ |
||
80 | public function __construct($object) |
||
88 | |||
89 | /** |
||
90 | * Returns all relations from an entity |
||
91 | * |
||
92 | * @return array<RelationInterface> Found relations |
||
93 | */ |
||
94 | public function getRelations() |
||
107 | |||
108 | /** |
||
109 | * Static toArray() modifier to handle objects and relations. |
||
110 | * {@see Accessor::toArray()} |
||
111 | * |
||
112 | * @param mixed $value Actual value |
||
113 | * |
||
114 | * @return mixed Filtered value |
||
115 | */ |
||
116 | public function everythingAsArrayModifier($value) |
||
142 | |||
143 | /** |
||
144 | * Try to retrieve a value from the object |
||
145 | * |
||
146 | * @param string $key Propertie's name |
||
147 | * |
||
148 | * @return mixed Actual value if reached or false |
||
149 | */ |
||
150 | public function get($key) |
||
176 | |||
177 | /** |
||
178 | * Try to set a value |
||
179 | * |
||
180 | * @param string $key Propertie's name |
||
181 | * @param mixed $value Desired value |
||
182 | * |
||
183 | * @return boolean true if successful |
||
184 | */ |
||
185 | public function set($key, $value) |
||
221 | |||
222 | /** |
||
223 | * Set multiple values at once |
||
224 | * |
||
225 | * @param array $values Array of keys->values to be set |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | public function setValues(array $values) |
||
235 | |||
236 | /** |
||
237 | * Gets a reflector for the object |
||
238 | * |
||
239 | * @return \ReflectionObject |
||
240 | */ |
||
241 | public function getReflector() |
||
249 | |||
250 | /** |
||
251 | * Make an array of keys->values (eventually filtered by $modifier) from |
||
252 | * object's properties. |
||
253 | * |
||
254 | * @param mixed $modifier Filtering callable |
||
255 | * |
||
256 | * @return array The resulting array |
||
257 | */ |
||
258 | public function toArray($modifier = null) |
||
275 | |||
276 | /** |
||
277 | * Produces a unique hash code based on values |
||
278 | * |
||
279 | * @param string $algo Desired algorythm |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function hashCode($algo = 'crc32') |
||
299 | |||
300 | /** |
||
301 | * Factory utility |
||
302 | * |
||
303 | * @param mixed $object The object we want to access |
||
304 | * |
||
305 | * @return Accessor |
||
306 | */ |
||
307 | public static function factory($object) |
||
311 | |||
312 | /** |
||
313 | * Should we force properties visibility ? |
||
314 | * |
||
315 | * @param boolean $bool yes or no |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | public function overrideVisibility($bool) |
||
323 | } |
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.