Complex classes like prototype 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 prototype, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | final class prototype |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var \SplObjectStorage contains a list of frozen objects and the observer |
||
22 | */ |
||
23 | private static $frozen = null; |
||
24 | |||
25 | /** |
||
26 | * @var \SplObjectStorage contains a list of frozen objects and the observer |
||
27 | */ |
||
28 | private static $sealed = null; |
||
29 | |||
30 | /** |
||
31 | * @var \SplObjectStorage contains a list of objects made unextensible and the observer |
||
32 | */ |
||
33 | private static $notExtensible = null; |
||
34 | |||
35 | /** |
||
36 | * @var \SplObjectStorage contains a list of all 'child' instances for each prototype |
||
37 | */ |
||
38 | private static $instances = null; |
||
39 | |||
40 | /** |
||
41 | * @var \SplObjectStorage contains a list of all observers for each prototype |
||
42 | */ |
||
43 | private static $observers = null; |
||
44 | |||
45 | /** |
||
46 | * Returns a new \arc\prototype\Object object with the given properties. The |
||
47 | * properties array may contain closures, these will be available as methods on |
||
48 | * the new Prototype object. |
||
49 | * @param array $properties List of properties and methods |
||
50 | * @return \arc\prototype\Object |
||
51 | */ |
||
52 | 14 | public static function create($properties) |
|
56 | |||
57 | /** |
||
58 | * Returns a new \arc\prototype\Object object with the given object as its |
||
59 | * prototype and the given properties and methods set. |
||
60 | * @param \arc\prototype\Object $prototype The prototype for this object |
||
61 | * @param array $properties List of properties and methods |
||
62 | * @return \arc\prototype\Object |
||
63 | */ |
||
64 | 5 | public static function extend($prototype, $properties) |
|
83 | |||
84 | /** |
||
85 | * Helper method to remove cache information when a prototype is no longer needed. |
||
86 | * @param \arc\prototype\Object $obj The object to be removed |
||
87 | */ |
||
88 | 6 | public static function _destroy($obj) |
|
98 | |||
99 | /** |
||
100 | * Returns a new \arc\prototype\Object with the given prototype set. In addition |
||
101 | * all properties on the extra objects passed to this method will be copied to the |
||
102 | * new Prototype object. For any property that is set on multiple objects, the value |
||
103 | * of the property in the later object overwrites values from other objects. |
||
104 | * @param \arc\prototype\Object $prototype the prototype for the new object |
||
105 | * @param \arc\prototype\Object ...$object the objects whose properties will be assigned |
||
106 | */ |
||
107 | 1 | public static function assign($prototype) |
|
117 | |||
118 | /** |
||
119 | * This makes changes to the given Prototype object impossible. |
||
120 | * The object becomes immutable. Any attempt to change the object will silently fail. |
||
121 | * @param \arc\prototype\Object $prototype the object to freeze |
||
122 | */ |
||
123 | 1 | public static function freeze($prototype) |
|
131 | |||
132 | /** |
||
133 | * This prevents reconfiguring an object or adding new properties. |
||
134 | * @param \arc\prototype\Object $prototype the object to freeze |
||
135 | */ |
||
136 | 1 | public static function seal($prototype) |
|
144 | |||
145 | /** |
||
146 | * Returns a list of keys of all the properties in the given prototype |
||
147 | * @param \arc\prototype\Object $prototype |
||
148 | * @return array |
||
149 | */ |
||
150 | public static function keys($prototype) |
||
155 | |||
156 | /** |
||
157 | * Returns an array with key:value pairs for all properties in the given prototype |
||
158 | * @param \arc\prototype\Object $prototype |
||
159 | * @return array |
||
160 | */ |
||
161 | 1 | public static function entries($prototype) |
|
165 | |||
166 | /** |
||
167 | * Returns a list of all the property values in the given prototype |
||
168 | * @param \arc\prototype\Object $prototype |
||
169 | * @return array |
||
170 | */ |
||
171 | public static function values($prototype) |
||
176 | |||
177 | /** |
||
178 | * Returns true if the the property name is available in this prototype |
||
179 | * @param \arc\prototype\Object $prototype |
||
180 | * @param string $property |
||
181 | * @return bool |
||
182 | */ |
||
183 | public static function hasProperty($prototype, $property) |
||
188 | |||
189 | /** |
||
190 | * Returns a list of all the property names defined in this prototype instance |
||
191 | * without traversing its prototypes. |
||
192 | * @param \arc\prototype\Object $prototype |
||
193 | * @return array |
||
194 | */ |
||
195 | public static function ownKeys($prototype) |
||
200 | |||
201 | /** |
||
202 | * Returns an array with key:value pairs for all properties in this prototype |
||
203 | * instance wihtout traversing its prototypes. |
||
204 | * @param \arc\prototype\Object $prototype |
||
205 | * @return array |
||
206 | */ |
||
207 | 1 | public static function ownEntries($prototype) |
|
211 | |||
212 | /** |
||
213 | * Returns a list of all the property values in the given prototype |
||
214 | * instance wihtout traversing its prototypes. |
||
215 | * @param \arc\prototype\Object $prototype |
||
216 | * @return array |
||
217 | */ |
||
218 | public static function ownValues($prototype) |
||
223 | |||
224 | /** |
||
225 | * Returns true if the the property name is available in this prototype |
||
226 | * instance wihtout traversing its prototypes. |
||
227 | * @param \arc\prototype\Object $prototype |
||
228 | * @param string $property |
||
229 | * @return bool |
||
230 | */ |
||
231 | 1 | public static function hasOwnProperty($prototype, $property) |
|
236 | |||
237 | /** |
||
238 | * Returns true if the given prototype is made immutable by freeze() |
||
239 | * @param \arc\prototype\Object $prototype |
||
240 | * @return bool |
||
241 | */ |
||
242 | public static function isFrozen($prototype) |
||
246 | |||
247 | /** |
||
248 | * Returns true if the given prototype is sealed by seal() |
||
249 | * @param \arc\prototype\Object $prototype |
||
250 | * @return bool |
||
251 | */ |
||
252 | 4 | public static function isSealed($prototype) |
|
256 | |||
257 | /** |
||
258 | * Returns true if the given prototype is made not Extensible |
||
259 | * @param \arc\prototype\Object $prototype |
||
260 | * @return bool |
||
261 | */ |
||
262 | 7 | public static function isExtensible($prototype) |
|
266 | |||
267 | /** |
||
268 | * This calls the $callback function each time a property of $prototype is |
||
269 | * changed or unset. The callback is called with the prototype object, the |
||
270 | * name of the property and the new value (null if unset). |
||
271 | * If the closure returns false exactly (no other 'falsy' values will work), |
||
272 | * the change will be cancelled |
||
273 | * @param \arc\prototype\Object $prototype |
||
274 | * @param \Closure $callback |
||
275 | */ |
||
276 | 1 | public static function observe($prototype, $callback, $acceptList=null) |
|
296 | |||
297 | /** |
||
298 | * Returns a list of observers for the given prototype. |
||
299 | * @param \arc\prototype\Object $prototype |
||
300 | * @return array |
||
301 | */ |
||
302 | 1 | public static function getObservers($prototype) |
|
306 | |||
307 | /** |
||
308 | * Makes an object no longer extensible. |
||
309 | * @param \arc\prototype\Object $prototype |
||
310 | */ |
||
311 | 2 | public static function preventExtensions($prototype) |
|
318 | |||
319 | /** |
||
320 | * Removes an observer callback for the given prototype. |
||
321 | * @param \arc\prototype\Prototyp $prototype |
||
322 | * @param \Closure $callback the observer callback to be removed |
||
323 | */ |
||
324 | public static function unobserve($prototype, $callback) |
||
330 | |||
331 | /** |
||
332 | * Returns true if the object as the given prototype somewhere in its |
||
333 | * prototype chain, including itself. |
||
334 | */ |
||
335 | public static function hasPrototype($obj, $prototype) |
||
346 | |||
347 | /** |
||
348 | * Returns a list of prototype objects that have this prototype object |
||
349 | * in their prototype chain. |
||
350 | * @param \arc\prototype\Object $prototype |
||
351 | * @return array |
||
352 | */ |
||
353 | public static function getDescendants($prototype) |
||
362 | |||
363 | /** |
||
364 | * Returns a list of prototype objects that have this prototype object |
||
365 | * as their direct prototype. |
||
366 | * @param \arc\prototype\Object $prototype |
||
367 | * @return array |
||
368 | */ |
||
369 | public static function getInstances($prototype) |
||
373 | |||
374 | /** |
||
375 | * Returns the full prototype chain for the given object. |
||
376 | * @param \arc\prototype\Object $obj |
||
377 | * @return array |
||
378 | */ |
||
379 | public static function getPrototypes($obj) |
||
388 | |||
389 | /** |
||
390 | * Returns a new function that calls the given function just once and then simply |
||
391 | * returns its result on each subsequent call. |
||
392 | * @param callable function to call just once and then remember the result |
||
393 | */ |
||
394 | public static function memoize($f) |
||
398 | } |
||
399 | |||
429 | } |
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.