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 | private function initializeFromDN($dn) |
||
39 | |||
40 | private function initializeFromArray($array) |
||
55 | |||
56 | protected function update($obj) |
||
74 | |||
75 | /** |
||
76 | * Get the specified field from the cached object or LDAPObject |
||
77 | * |
||
78 | * @param string $fieldName The name of the field to retrieve |
||
79 | * |
||
80 | * @return mixed string|array the value of the field |
||
81 | */ |
||
82 | protected function getField($fieldName) |
||
90 | |||
91 | /** |
||
92 | * Get the value of the specified field from the cached object or LDAPObject |
||
93 | * |
||
94 | * @param string $fieldName The name of the field to retrieve |
||
95 | * |
||
96 | * @return mixed string the value of the field |
||
97 | */ |
||
98 | protected function getFieldSingleValue($fieldName) |
||
106 | |||
107 | /** |
||
108 | * Set the value of the specified field in the cached object or LDAPObject |
||
109 | * |
||
110 | * @param string $fieldName The name of the field to set |
||
111 | * @param mixed $fieldValue The value to set in the field |
||
112 | * |
||
113 | * @return boolean true if the field is set and false otherwise |
||
114 | */ |
||
115 | protected function setField($fieldName, $fieldValue) |
||
123 | |||
124 | /** |
||
125 | * Append a value of the specified field in the cached object or LDAPObject |
||
126 | * |
||
127 | * @param string $fieldName The name of the field to set |
||
128 | * @param mixed $fieldValue The value to append to the field |
||
129 | * |
||
130 | * @return boolean true if the field is set and false otherwise |
||
131 | */ |
||
132 | protected function appendField($fieldName, $fieldValue) |
||
140 | |||
141 | /** |
||
142 | * Get the value of the field in the local cache |
||
143 | * |
||
144 | * @param string $fieldName The name of the field to retrieve |
||
145 | * |
||
146 | * @return mixed the value of the field |
||
147 | */ |
||
148 | private function getFieldLocal($fieldName) |
||
160 | |||
161 | /** |
||
162 | * Get the value of the field in the server object |
||
163 | * |
||
164 | * @param string $fieldName The name of the field to retrieve |
||
165 | * |
||
166 | * @return mixed the value of the field |
||
167 | */ |
||
168 | private function getFieldServer($fieldName) |
||
177 | |||
178 | /** |
||
179 | * Get the value of the specified field from the local cache |
||
180 | * |
||
181 | * @param string $fieldName The name of the field to retrieve |
||
182 | * |
||
183 | * @return string the value of the field |
||
184 | */ |
||
185 | private function getFieldLocalSingleValue($fieldName) |
||
201 | |||
202 | /** |
||
203 | * Get the value of the specified field from the server |
||
204 | * |
||
205 | * @param string $fieldName The name of the field to retrieve |
||
206 | * |
||
207 | * @return string the value of the field |
||
208 | */ |
||
209 | private function getFieldServerSingleValue($fieldName) |
||
223 | |||
224 | /** |
||
225 | * Set the specified field in the server |
||
226 | * |
||
227 | * @param string $fieldName The name of the field to set |
||
228 | * @param mixed $fieldValue The value to write to the field |
||
229 | * |
||
230 | * @return boolean true if the field is set and false otherwise |
||
231 | */ |
||
232 | private function setFieldServer($fieldName, $fieldValue) |
||
247 | |||
248 | /** |
||
249 | * Append a value of the specified field in the server |
||
250 | * |
||
251 | * @param string $fieldName The name of the field to set |
||
252 | * @param mixed $fieldValue The value to append to the field |
||
253 | * |
||
254 | * @return boolean true if the field is set and false otherwise |
||
255 | */ |
||
256 | private function appendFieldServer($fieldName, $fieldValue) |
||
271 | |||
272 | /** |
||
273 | * Set the specified field in the local cache |
||
274 | * |
||
275 | * @param string $fieldName The name of the field to set |
||
276 | * @param mixed $fieldValue The value to write to the field |
||
277 | * |
||
278 | * @return boolean true if the field is set and false otherwise |
||
279 | */ |
||
280 | private function setFieldLocal($fieldName, $fieldValue) |
||
297 | |||
298 | /** |
||
299 | * Append a value of the specified field in the local cache |
||
300 | * |
||
301 | * @param string $fieldName The name of the field to set |
||
302 | * @param mixed $fieldValue The value to append to the field |
||
303 | * |
||
304 | * @return boolean true if the field is set and false otherwise |
||
305 | */ |
||
306 | private function appendFieldLocal($fieldName, $fieldValue) |
||
319 | } |
||
320 | |||
322 |
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: