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) |
||
34 | |||
35 | private function initializeFromArray($array) |
||
50 | |||
51 | protected function update($obj) |
||
69 | |||
70 | /** |
||
71 | * Get the specified field from the cached object or LDAPObject |
||
72 | * |
||
73 | * @param string $fieldName The name of the field to retrieve |
||
74 | * |
||
75 | * @return mixed string|array the value of the field |
||
76 | */ |
||
77 | protected function getField($fieldName) |
||
85 | |||
86 | /** |
||
87 | * Get the value of the specified field from the cached object or LDAPObject |
||
88 | * |
||
89 | * @param string $fieldName The name of the field to retrieve |
||
90 | * |
||
91 | * @return mixed string the value of the field |
||
92 | */ |
||
93 | protected function getFieldSingleValue($fieldName) |
||
101 | |||
102 | /** |
||
103 | * Set the value of the specified field in the cached object or LDAPObject |
||
104 | * |
||
105 | * @param string $fieldName The name of the field to set |
||
106 | * @param mixed $fieldValue The value to set in the field |
||
107 | * |
||
108 | * @return boolean true if the field is set and false otherwise |
||
109 | */ |
||
110 | protected function setField($fieldName, $fieldValue) |
||
118 | |||
119 | /** |
||
120 | * Append a value of the specified field in the cached object or LDAPObject |
||
121 | * |
||
122 | * @param string $fieldName The name of the field to set |
||
123 | * @param mixed $fieldValue The value to append to the field |
||
124 | * |
||
125 | * @return boolean true if the field is set and false otherwise |
||
126 | */ |
||
127 | protected function appendField($fieldName, $fieldValue) |
||
135 | |||
136 | /** |
||
137 | * Get the value of the field in the local cache |
||
138 | * |
||
139 | * @param string $fieldName The name of the field to retrieve |
||
140 | * |
||
141 | * @return mixed the value of the field |
||
142 | */ |
||
143 | private function getFieldLocal($fieldName) |
||
155 | |||
156 | /** |
||
157 | * Get the value of the field in the server object |
||
158 | * |
||
159 | * @param string $fieldName The name of the field to retrieve |
||
160 | * |
||
161 | * @return mixed the value of the field |
||
162 | */ |
||
163 | private function getFieldServer($fieldName) |
||
172 | |||
173 | /** |
||
174 | * Get the value of the specified field from the local cache |
||
175 | * |
||
176 | * @param string $fieldName The name of the field to retrieve |
||
177 | * |
||
178 | * @return string the value of the field |
||
179 | */ |
||
180 | private function getFieldLocalSingleValue($fieldName) |
||
196 | |||
197 | /** |
||
198 | * Get the value of the specified field from the server |
||
199 | * |
||
200 | * @param string $fieldName The name of the field to retrieve |
||
201 | * |
||
202 | * @return string the value of the field |
||
203 | */ |
||
204 | private function getFieldServerSingleValue($fieldName) |
||
218 | |||
219 | /** |
||
220 | * Set the specified field in the server |
||
221 | * |
||
222 | * @param string $fieldName The name of the field to set |
||
223 | * @param mixed $fieldValue The value to write to the field |
||
224 | * |
||
225 | * @return boolean true if the field is set and false otherwise |
||
226 | */ |
||
227 | private function setFieldServer($fieldName, $fieldValue) |
||
242 | |||
243 | /** |
||
244 | * Append a value of the specified field in the server |
||
245 | * |
||
246 | * @param string $fieldName The name of the field to set |
||
247 | * @param mixed $fieldValue The value to append to the field |
||
248 | * |
||
249 | * @return boolean true if the field is set and false otherwise |
||
250 | */ |
||
251 | private function appendFieldServer($fieldName, $fieldValue) |
||
266 | |||
267 | /** |
||
268 | * Set the specified field in the local cache |
||
269 | * |
||
270 | * @param string $fieldName The name of the field to set |
||
271 | * @param mixed $fieldValue The value to write to the field |
||
272 | * |
||
273 | * @return boolean true if the field is set and false otherwise |
||
274 | */ |
||
275 | private function setFieldLocal($fieldName, $fieldValue) |
||
292 | |||
293 | /** |
||
294 | * Append a value of the specified field in the local cache |
||
295 | * |
||
296 | * @param string $fieldName The name of the field to set |
||
297 | * @param mixed $fieldValue The value to append to the field |
||
298 | * |
||
299 | * @return boolean true if the field is set and false otherwise |
||
300 | */ |
||
301 | private function appendFieldLocal($fieldName, $fieldValue) |
||
314 | } |
||
315 | |||
317 |
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: