Complex classes like Writing_On_GitHub_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.
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 Writing_On_GitHub_Import, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Writing_On_GitHub_Import { |
||
12 | |||
13 | /** |
||
14 | * Application container. |
||
15 | * |
||
16 | * @var Writing_On_GitHub |
||
17 | */ |
||
18 | protected $app; |
||
19 | |||
20 | /** |
||
21 | * Initializes a new import manager. |
||
22 | * |
||
23 | * @param Writing_On_GitHub $app Application container. |
||
24 | */ |
||
25 | public function __construct( Writing_On_GitHub $app ) { |
||
28 | |||
29 | /** |
||
30 | * Imports a payload. |
||
31 | * @param Writing_On_GitHub_Payload $payload |
||
32 | * |
||
33 | * @return string|WP_Error |
||
34 | */ |
||
35 | public function payload( Writing_On_GitHub_Payload $payload ) { |
||
54 | |||
55 | /** |
||
56 | * import blob by files |
||
57 | * @param Writing_On_GitHub_File_Info[] $files |
||
58 | * |
||
59 | * @return true|WP_Error |
||
|
|||
60 | */ |
||
61 | protected function import_files( $files ) { |
||
93 | |||
94 | /** |
||
95 | * Imports the latest commit on the master branch. |
||
96 | * |
||
97 | * @return string|WP_Error |
||
98 | */ |
||
99 | public function master() { |
||
100 | $result = $this->app->api()->fetch()->tree_recursive(); |
||
101 | |||
102 | if ( is_wp_error( $result ) ) { |
||
103 | /* @var WP_Error $result */ |
||
104 | return $result; |
||
105 | } |
||
106 | |||
107 | if ( is_array( $result ) ) { |
||
108 | $result = $this->import_files( $result ); |
||
109 | } |
||
110 | |||
111 | if ( is_wp_error( $result ) ) { |
||
112 | /* @var WP_Error $result */ |
||
113 | return $result; |
||
114 | } |
||
115 | |||
116 | return __( 'Payload processed', 'writing-on-github' ); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Checks whether the provided blob should be imported. |
||
121 | * |
||
122 | * @param Writing_On_GitHub_File_Info $file |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
||
139 | |||
140 | /** |
||
141 | * Checks whether the provided blob should be imported. |
||
142 | * |
||
143 | * @param Writing_On_GitHub_Blob $blob Blob to validate. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
||
166 | |||
167 | /** |
||
168 | * Delete post |
||
169 | * @param Writing_On_GitHub_Blob $blob |
||
170 | * @return WP_Error|bool |
||
171 | */ |
||
172 | protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
||
184 | |||
185 | /** |
||
186 | * Imports a post into wordpress |
||
187 | * @param Writing_On_GitHub_Blob $blob |
||
188 | * @return WP_Error|bool |
||
189 | */ |
||
190 | protected function import_post( Writing_On_GitHub_Blob $blob ) { |
||
218 | |||
219 | /** |
||
220 | * import raw file |
||
221 | * @param Writing_On_GitHub_Blob $blob |
||
222 | * @return bool |
||
223 | */ |
||
224 | protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
||
236 | |||
237 | /** |
||
238 | * Imports a raw file content into file system. |
||
239 | * @param Writing_On_GitHub_Blob $blob |
||
240 | * @param bool $is_remove |
||
241 | */ |
||
242 | protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
||
259 | |||
260 | /** |
||
261 | * Imports a single blob content into matching post. |
||
262 | * |
||
263 | * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
||
264 | * |
||
265 | * @return Writing_On_GitHub_Post|false |
||
266 | */ |
||
267 | protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
||
321 | } |
||
322 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.