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 |
||
20 | class ContentImporter extends MslsRegistryInstance { |
||
21 | use WithRequestPostAttributes; |
||
22 | |||
23 | /** |
||
24 | * @var MslsMain |
||
25 | */ |
||
26 | protected $main; |
||
27 | |||
28 | /** |
||
29 | * @var ImportLogger |
||
30 | */ |
||
31 | protected $logger; |
||
32 | /** |
||
33 | * @var Relations |
||
34 | */ |
||
35 | protected $relations; |
||
36 | |||
37 | /** |
||
38 | * @var bool Whether the class should handle requests or not. |
||
39 | */ |
||
40 | protected $handle = true; |
||
41 | |||
42 | /** |
||
43 | * @var int The ID of the post the class created while handling the request, if any. |
||
44 | */ |
||
45 | protected $has_created_post = 0; |
||
46 | |||
47 | /** |
||
48 | * ContentImporter constructor. |
||
49 | * |
||
50 | * @param \lloc\Msls\MslsMain|null $main |
||
51 | */ |
||
52 | public function __construct( MslsMain $main = null ) { |
||
55 | |||
56 | /** |
||
57 | * @return \lloc\Msls\ContentImport\ImportLogger |
||
58 | */ |
||
59 | public function get_logger() { |
||
62 | |||
63 | /** |
||
64 | * @param \lloc\Msls\ContentImport\ImportLogger $logger |
||
65 | */ |
||
66 | public function set_logger( $logger ) { |
||
69 | |||
70 | /** |
||
71 | * @return \lloc\Msls\ContentImport\Relations |
||
72 | */ |
||
73 | public function get_relations() { |
||
76 | |||
77 | /** |
||
78 | * @param \lloc\Msls\ContentImport\Relations $relations |
||
79 | */ |
||
80 | public function set_relations( $relations ) { |
||
83 | |||
84 | /** |
||
85 | * Handles an import request happening during a post save or a template redirect. |
||
86 | * |
||
87 | * @param array|null $data |
||
88 | * |
||
89 | * @return array The updated, if needed, data array. |
||
90 | */ |
||
91 | public function handle_import( array $data = [] ) { |
||
141 | |||
142 | /** |
||
143 | * Whether the importer should run or not. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected function pre_flight_check( array $data = [] ) { |
||
162 | |||
163 | /** |
||
164 | * Parses the source blog and post IDs from the $_POST array validating them. |
||
165 | * |
||
166 | * @return array|bool |
||
167 | */ |
||
168 | public function parse_sources() { |
||
181 | |||
182 | protected function get_the_blog_post_ID( $blog_id ) { |
||
204 | |||
205 | protected function insert_blog_post( $blog_id, array $data = [] ) { |
||
226 | |||
227 | public function handle( $handle ) { |
||
237 | |||
238 | /** |
||
239 | * Imports content according to the provided coordinates. |
||
240 | * |
||
241 | * @param ImportCoordinates $import_coordinates |
||
242 | * @param array $post_fields An optional array of post fields; this can be |
||
243 | * left empty if the method is not called as a consequence |
||
244 | * of filtering the `wp_insert_post_data` filter. |
||
245 | * |
||
246 | * @return array An array of modified post fields. |
||
247 | */ |
||
248 | public function import_content( ImportCoordinates $import_coordinates, array $post_fields = [] ) { |
||
326 | |||
327 | /** |
||
328 | * @param array $data |
||
329 | * @param int $post_id |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | protected function update_inserted_blog_post_data($blog_id, $post_id, array $data ) { |
||
342 | |||
343 | protected function redirect_to_blog_post( $dest_blog_id, $post_id ) { |
||
349 | |||
350 | /** |
||
351 | * Filters whether the post should be considered empty or not. |
||
352 | * |
||
353 | * Empty posts would not be saved to database but it's fine if in |
||
354 | * the context of a content import as it will be populated. |
||
355 | * |
||
356 | * @param bool $empty |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function filter_empty( $empty ) { |
||
371 | } |
||
372 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.