Complex classes like ContentImporter 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 ContentImporter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class ContentImporter extends MslsRegistryInstance { |
||
20 | |||
21 | /** |
||
22 | * @var MslsMain |
||
23 | */ |
||
24 | protected $main; |
||
25 | |||
26 | /** |
||
27 | * @var ImportLogger |
||
28 | */ |
||
29 | protected $logger; |
||
30 | /** |
||
31 | * @var Relations |
||
32 | */ |
||
33 | protected $relations; |
||
34 | |||
35 | /** |
||
36 | * @var bool Whether the class should handle requests or not. |
||
37 | */ |
||
38 | protected $handle = true; |
||
39 | |||
40 | /** |
||
41 | * @var int The ID of the post the class created while handling the request, if any. |
||
42 | */ |
||
43 | protected $has_created_post = 0; |
||
44 | |||
45 | /** |
||
46 | * ContentImporter constructor. |
||
47 | * |
||
48 | * @param \lloc\Msls\MslsMain|null $main |
||
49 | */ |
||
50 | public function __construct( MslsMain $main = null ) { |
||
53 | |||
54 | /** |
||
55 | * @return \lloc\Msls\ContentImport\ImportLogger |
||
56 | */ |
||
57 | public function get_logger() { |
||
60 | |||
61 | /** |
||
62 | * @param \lloc\Msls\ContentImport\ImportLogger $logger |
||
63 | */ |
||
64 | public function set_logger( $logger ) { |
||
67 | |||
68 | /** |
||
69 | * @return \lloc\Msls\ContentImport\Relations |
||
70 | */ |
||
71 | public function get_relations() { |
||
74 | |||
75 | /** |
||
76 | * @param \lloc\Msls\ContentImport\Relations $relations |
||
77 | */ |
||
78 | public function set_relations( $relations ) { |
||
81 | |||
82 | /** |
||
83 | * Handles an import request happening during a post save or a template redirect. |
||
84 | * |
||
85 | * @param array|null $data |
||
86 | * |
||
87 | * @return array The updated, if needed, data array. |
||
88 | */ |
||
89 | public function handle_import( array $data = [] ) { |
||
139 | |||
140 | /** |
||
141 | * Whether the importer should run or not. |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | protected function pre_flight_check( array $data = [] ) { |
||
160 | |||
161 | /** |
||
162 | * Parses the source blog and post IDs from the $_POST array validating them. |
||
163 | * |
||
164 | * @return array|bool |
||
165 | */ |
||
166 | public function parse_sources() { |
||
179 | |||
180 | protected function get_the_blog_post_ID( $blog_id ) { |
||
197 | |||
198 | protected function insert_blog_post( $blog_id, array $data = [] ) { |
||
219 | |||
220 | public function handle( $handle ) { |
||
230 | |||
231 | /** |
||
232 | * Imports content according to the provided coordinates. |
||
233 | * |
||
234 | * @param ImportCoordinates $import_coordinates |
||
235 | * @param array $post_fields An optional array of post fields; this can be |
||
236 | * left empty if the method is not called as a consequence |
||
237 | * of filtering the `wp_insert_post_data` filter. |
||
238 | * |
||
239 | * @return array An array of modified post fields. |
||
240 | */ |
||
241 | public function import_content( ImportCoordinates $import_coordinates, array $post_fields = [] ) { |
||
319 | |||
320 | /** |
||
321 | * @param array $data |
||
322 | * @param int $post_id |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | protected function update_inserted_blog_post_data($blog_id, $post_id, array $data ) { |
||
335 | |||
336 | protected function redirect_to_blog_post( $dest_blog_id, $post_id ) { |
||
342 | |||
343 | /** |
||
344 | * Filters whether the post should be considered empty or not. |
||
345 | * |
||
346 | * Empty posts would not be saved to database but it's fine if in |
||
347 | * the context of a content import as it will be populated. |
||
348 | * |
||
349 | * @param bool $empty |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function filter_empty( $empty ) { |
||
364 | } |