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 |
||
47 | class Accessor |
||
48 | { |
||
49 | /** |
||
50 | * The object to access |
||
51 | * |
||
52 | * @var mixed |
||
53 | */ |
||
54 | protected $object; |
||
55 | |||
56 | /** |
||
57 | * Reflector for the object |
||
58 | * |
||
59 | * @var \ReflectionObject |
||
60 | */ |
||
61 | protected $reflector; |
||
62 | |||
63 | /** |
||
64 | * Override properties visibility ? |
||
65 | * |
||
66 | * @var boolean |
||
67 | */ |
||
68 | protected $force = false; |
||
69 | |||
70 | /** |
||
71 | * Constructor |
||
72 | * |
||
73 | * @param mixed $object The object we want to access |
||
74 | * |
||
75 | * @throws \InvalidArgumentException if $object is not an object |
||
76 | * @return void |
||
|
|||
77 | */ |
||
78 | 17 | public function __construct($object) |
|
86 | |||
87 | /** |
||
88 | * Try to retrieve a value from the object |
||
89 | * |
||
90 | * @param string $key Propertie's name |
||
91 | * |
||
92 | * @return mixed Actual value if reached or false |
||
93 | */ |
||
94 | 10 | public function get($key) |
|
124 | |||
125 | /** |
||
126 | * Try to set a value |
||
127 | * |
||
128 | * @param string $key Propertie's name |
||
129 | * @param mixed $value Desired value |
||
130 | * |
||
131 | * @return boolean true if successful |
||
132 | */ |
||
133 | 7 | public function set($key, $value) |
|
171 | |||
172 | /** |
||
173 | * Set multiple values at once |
||
174 | * |
||
175 | * @param array $values Array of keys->values to be set |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | 2 | public function setValues(array $values) |
|
185 | |||
186 | /** |
||
187 | * Gets a reflector for the object |
||
188 | * |
||
189 | * @return \ReflectionObject |
||
190 | */ |
||
191 | 14 | public function getReflector() |
|
199 | |||
200 | /** |
||
201 | * Make an array of keys->values (eventually filtered by $modifier) from |
||
202 | * object's properties. |
||
203 | * |
||
204 | * @param mixed $modifier Filtering callable |
||
205 | * |
||
206 | * @return array The resulting array |
||
207 | */ |
||
208 | 8 | public function toArray($modifier = null) |
|
226 | |||
227 | /** |
||
228 | * Recursive function to get an associative array of class properties by |
||
229 | * property name => ReflectionProperty() object including inherited ones |
||
230 | * from extended classes. |
||
231 | * |
||
232 | * @param string $className Class name |
||
233 | * @param integer $filter ReflectionProperty filter |
||
234 | * |
||
235 | * @author muratyaman at gmail dot com |
||
236 | * @return array |
||
237 | */ |
||
238 | 11 | protected function getClassProperties($className, $filter = null) |
|
266 | |||
267 | /** |
||
268 | * Returns class attributes |
||
269 | * |
||
270 | * @return array The resulting array |
||
271 | */ |
||
272 | 7 | public function getAttributes() |
|
283 | |||
284 | /** |
||
285 | * Produces a unique hash code based on values |
||
286 | * |
||
287 | * @param string $algo Desired algorythm |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | 1 | public function hashCode($algo = 'md5') |
|
307 | |||
308 | /** |
||
309 | * Static toArray() modifier to handle objects and relations. |
||
310 | * {@see Accessor::toArray()} |
||
311 | * |
||
312 | * @param mixed $value Actual value |
||
313 | * |
||
314 | * @return mixed Filtered value |
||
315 | */ |
||
316 | 1 | public function everythingAsArrayModifier($value) |
|
331 | |||
332 | /** |
||
333 | * Factory utility |
||
334 | * |
||
335 | * @param mixed $object The object we want to access |
||
336 | * |
||
337 | * @return Accessor |
||
338 | */ |
||
339 | 2 | public static function factory($object) |
|
343 | |||
344 | /** |
||
345 | * Should we force properties visibility ? |
||
346 | * |
||
347 | * @param boolean $bool yes or no |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | 4 | public function overrideVisibility($bool) |
|
355 | } |
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.