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 | * Imports a post into wordpress |
||
| 169 | * @param Writing_On_GitHub_Blob $blob |
||
| 170 | * @param bool $is_remove |
||
| 171 | * @return WP_Error|bool |
||
| 172 | */ |
||
| 173 | protected function import_post( Writing_On_GitHub_Blob $blob, $is_remove ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * import raw file |
||
| 216 | * @param Writing_On_GitHub_Blob $blob |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Imports a raw file content into file system. |
||
| 234 | * @param Writing_On_GitHub_Blob $blob |
||
| 235 | * @param bool $is_remove |
||
| 236 | */ |
||
| 237 | protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Imports a single blob content into matching post. |
||
| 257 | * |
||
| 258 | * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
||
| 259 | * |
||
| 260 | * @return Writing_On_GitHub_Post|false |
||
| 261 | */ |
||
| 262 | protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
||
| 316 | } |
||
| 317 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.