Complex classes like WordPress_GitHub_Sync_Import 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 WordPress_GitHub_Sync_Import, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class WordPress_GitHub_Sync_Import { |
||
12 | |||
13 | /** |
||
14 | * Application container. |
||
15 | * |
||
16 | * @var WordPress_GitHub_Sync |
||
17 | */ |
||
18 | protected $app; |
||
19 | |||
20 | /** |
||
21 | * Initializes a new import manager. |
||
22 | * |
||
23 | * @param WordPress_GitHub_Sync $app Application container. |
||
24 | */ |
||
25 | public function __construct( WordPress_GitHub_Sync $app ) { |
||
28 | |||
29 | /** |
||
30 | * Imports a payload. |
||
31 | * |
||
32 | * @param WordPress_GitHub_Sync_Payload $payload GitHub payload object. |
||
33 | * |
||
34 | * @return string|WP_Error |
||
35 | */ |
||
36 | public function payload( WordPress_GitHub_Sync_Payload $payload ) { |
||
72 | |||
73 | /** |
||
74 | * Imports the latest commit on the master branch. |
||
75 | * |
||
76 | * @return string|WP_Error |
||
77 | */ |
||
78 | public function master() { |
||
81 | |||
82 | /** |
||
83 | * Imports a provided commit into the database. |
||
84 | * |
||
85 | * @param WordPress_GitHub_Sync_Commit|WP_Error $commit Commit to import. |
||
86 | * |
||
87 | * @return string|WP_Error |
||
88 | */ |
||
89 | protected function commit( $commit ) { |
||
182 | |||
183 | /** |
||
184 | * Get the function that will be used to import the blob, hookable |
||
185 | * |
||
186 | * @param WordPress_GitHub_Sync_Blob |
||
187 | * @return callable |
||
188 | */ |
||
189 | protected function get_blob_processor( WordPress_GitHub_Sync_Blob $blob ) { |
||
201 | |||
202 | /** |
||
203 | * Returns true if the path is not importable, hookable |
||
204 | * |
||
205 | * @param string |
||
206 | * @return boolean |
||
207 | */ |
||
208 | protected function is_excluded_path( $path ) { |
||
219 | |||
220 | /** |
||
221 | * Returns true if the mine type is not importable, hookable |
||
222 | * |
||
223 | * @param string |
||
224 | * @return boolean |
||
225 | */ |
||
226 | protected function is_excluded_mime_type( $mime_type ) { |
||
233 | |||
234 | /** |
||
235 | * Returns true if the file extension is not importable, hookable |
||
236 | * |
||
237 | * @param string |
||
238 | * @return boolean |
||
239 | */ |
||
240 | protected function is_excluded_file_extension( $file_extension ) { |
||
248 | |||
249 | /** |
||
250 | * Checks whether the provided blob should be imported. |
||
251 | * |
||
252 | * @param WordPress_GitHub_Sync_Blob $blob Blob to validate. |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | protected function importable_blob( WordPress_GitHub_Sync_Blob $blob ) { |
||
269 | |||
270 | /** |
||
271 | * Checks whether the provided blob has changed. |
||
272 | * |
||
273 | * @param WordPress_GitHub_Sync_Blob $blob Blob to validate. |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | protected function blob_changed( WordPress_GitHub_Sync_Blob $blob ) { |
||
290 | |||
291 | /** |
||
292 | * Imports a single blob content into matching post. |
||
293 | * |
||
294 | * @param WordPress_GitHub_Sync_Blob $blob Blob to transform into a Post. |
||
295 | * |
||
296 | * @return WordPress_GitHub_Sync_Post |
||
297 | */ |
||
298 | protected function blob_to_post( WordPress_GitHub_Sync_Blob $blob ) { |
||
335 | } |
||
336 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.