Complex classes like LDAPCachableObject 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 LDAPCachableObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | trait LDAPCachableObject |
||
5 | { |
||
6 | protected function initialize($data) |
||
24 | |||
25 | /** |
||
26 | * Get the data from the server based on the DN |
||
27 | * |
||
28 | * @param string $dn The DN of the LDAP object |
||
29 | * |
||
30 | * @return array The raw LDAP data |
||
31 | */ |
||
32 | private function initializeFromDN($dn) |
||
41 | |||
42 | private function initializeFromArray($array) |
||
62 | |||
63 | protected function update($obj) |
||
81 | |||
82 | /** |
||
83 | * Get the specified field from the cached object or LDAPObject |
||
84 | * |
||
85 | * @param string $fieldName The name of the field to retrieve |
||
86 | * |
||
87 | * @return mixed string|array the value of the field |
||
88 | */ |
||
89 | protected function getField($fieldName) |
||
97 | |||
98 | /** |
||
99 | * Get the value of the specified field from the cached object or LDAPObject |
||
100 | * |
||
101 | * @param string $fieldName The name of the field to retrieve |
||
102 | * |
||
103 | * @return mixed string the value of the field |
||
104 | */ |
||
105 | protected function getFieldSingleValue($fieldName) |
||
113 | |||
114 | /** |
||
115 | * Set the value of the specified field in the cached object or LDAPObject |
||
116 | * |
||
117 | * @param string $fieldName The name of the field to set |
||
118 | * @param mixed $fieldValue The value to set in the field |
||
119 | * |
||
120 | * @return boolean true if the field is set and false otherwise |
||
121 | */ |
||
122 | protected function setField($fieldName, $fieldValue) |
||
130 | |||
131 | /** |
||
132 | * Append a value of the specified field in the cached object or LDAPObject |
||
133 | * |
||
134 | * @param string $fieldName The name of the field to set |
||
135 | * @param mixed $fieldValue The value to append to the field |
||
136 | * |
||
137 | * @return boolean true if the field is set and false otherwise |
||
138 | */ |
||
139 | protected function appendField($fieldName, $fieldValue) |
||
147 | |||
148 | /** |
||
149 | * Get the value of the field in the local cache |
||
150 | * |
||
151 | * @param string $fieldName The name of the field to retrieve |
||
152 | * |
||
153 | * @return mixed the value of the field |
||
154 | */ |
||
155 | private function getFieldLocal($fieldName) |
||
167 | |||
168 | /** |
||
169 | * Get the value of the field in the server object |
||
170 | * |
||
171 | * @param string $fieldName The name of the field to retrieve |
||
172 | * |
||
173 | * @return mixed the value of the field |
||
174 | */ |
||
175 | private function getFieldServer($fieldName) |
||
184 | |||
185 | /** |
||
186 | * Get the value of the specified field from the local cache |
||
187 | * |
||
188 | * @param string $fieldName The name of the field to retrieve |
||
189 | * |
||
190 | * @return string the value of the field |
||
191 | */ |
||
192 | private function getFieldLocalSingleValue($fieldName) |
||
208 | |||
209 | /** |
||
210 | * Get the value of the specified field from the server |
||
211 | * |
||
212 | * @param string $fieldName The name of the field to retrieve |
||
213 | * |
||
214 | * @return string the value of the field |
||
215 | */ |
||
216 | private function getFieldServerSingleValue($fieldName) |
||
230 | |||
231 | /** |
||
232 | * Set the specified field in the server |
||
233 | * |
||
234 | * @param string $fieldName The name of the field to set |
||
235 | * @param mixed $fieldValue The value to write to the field |
||
236 | * |
||
237 | * @return boolean true if the field is set and false otherwise |
||
238 | */ |
||
239 | private function setFieldServer($fieldName, $fieldValue) |
||
277 | |||
278 | /** |
||
279 | * Append a value of the specified field in the server |
||
280 | * |
||
281 | * @param string $fieldName The name of the field to set |
||
282 | * @param mixed $fieldValue The value to append to the field |
||
283 | * |
||
284 | * @return boolean true if the field is set and false otherwise |
||
285 | */ |
||
286 | private function appendFieldServer($fieldName, $fieldValue) |
||
301 | |||
302 | /** |
||
303 | * Set the specified field in the local cache |
||
304 | * |
||
305 | * @param string $fieldName The name of the field to set |
||
306 | * @param mixed $fieldValue The value to write to the field |
||
307 | * |
||
308 | * @return boolean true if the field is set and false otherwise |
||
309 | */ |
||
310 | private function setFieldLocal($fieldName, $fieldValue) |
||
327 | |||
328 | /** |
||
329 | * Append a value of the specified field in the local cache |
||
330 | * |
||
331 | * @param string $fieldName The name of the field to set |
||
332 | * @param mixed $fieldValue The value to append to the field |
||
333 | * |
||
334 | * @return boolean true if the field is set and false otherwise |
||
335 | */ |
||
336 | private function appendFieldLocal($fieldName, $fieldValue) |
||
349 | } |
||
350 | |||
352 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: