Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 ) { |
||
26 | $this->app = $app; |
||
27 | } |
||
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 ) { |
||
36 | |||
37 | $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
||
38 | |||
39 | if ( is_wp_error( $result ) ) { |
||
40 | /* @var WP_Error $result */ |
||
41 | return $result; |
||
42 | } |
||
43 | |||
44 | if ( is_array( $result ) ) { |
||
45 | $result = $this->import_files( $result ); |
||
46 | } |
||
47 | |||
48 | if ( is_wp_error( $result ) ) { |
||
49 | return $files; |
||
50 | } |
||
51 | |||
52 | return __( 'Payload processed', 'writing-on-github' ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * import blob by files |
||
57 | * @param Writing_On_GitHub_File_Info[] $files |
||
58 | * @param boolean $force |
||
59 | * |
||
60 | * @return true|WP_Error |
||
61 | */ |
||
62 | protected function import_files( $files, $force = false ) { |
||
63 | |||
64 | $error = true; |
||
65 | |||
66 | foreach ( $files as $file ) { |
||
67 | if ( ! $this->importable_file( $file ) ) { |
||
68 | continue; |
||
69 | } |
||
70 | |||
71 | $blob = $this->app->api()->fetch()->blob( $file ); |
||
72 | // network error ? |
||
73 | if ( ! $blob instanceof Writing_On_GitHub_Blob ) { |
||
74 | continue; |
||
75 | } |
||
76 | |||
77 | $is_remove = 'removed' == $file->status; |
||
78 | |||
79 | $result = false; |
||
80 | if ( $this->importable_raw_file( $blob ) ) { |
||
81 | $result = $this->import_raw_file( $blob, $is_remove ); |
||
82 | } elseif ( $this->importable_post( $blob ) ) { |
||
83 | if ( $is_remove ) { |
||
84 | $result = $this->delete_post( $blob ); |
||
85 | } else { |
||
86 | $result = $this->import_post( $blob, $force ); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | if ( is_wp_error( $result ) ) { |
||
91 | /* @var WP_Error $result */ |
||
92 | $error = wogh_append_error( $error, $result ); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return $error; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Imports the latest commit on the master branch. |
||
101 | * |
||
102 | * @param boolean $force |
||
103 | * @return string|WP_Error |
||
104 | */ |
||
105 | public function master( $force = false ) { |
||
106 | $result = $this->app->api()->fetch()->tree_recursive(); |
||
107 | |||
108 | if ( is_wp_error( $result ) ) { |
||
109 | /* @var WP_Error $result */ |
||
110 | return $result; |
||
111 | } |
||
112 | |||
113 | if ( is_array( $result ) ) { |
||
114 | $result = $this->import_files( $result, $force ); |
||
115 | } |
||
116 | |||
117 | if ( is_wp_error( $result ) ) { |
||
118 | /* @var WP_Error $result */ |
||
119 | return $result; |
||
120 | } |
||
121 | |||
122 | return __( 'Payload processed', 'writing-on-github' ); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Checks whether the provided blob should be imported. |
||
127 | * |
||
128 | * @param Writing_On_GitHub_File_Info $file |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
||
133 | |||
134 | $path = $file->path; |
||
135 | |||
136 | // only _pages, _posts and images |
||
137 | $prefixs = array( '_pages/', '_posts/', 'images/'); |
||
|
|||
138 | foreach ($prefixs as $prefix) { |
||
139 | if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) { |
||
140 | return true; |
||
141 | } |
||
142 | } |
||
143 | return false; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Checks whether the provided blob should be imported. |
||
148 | * |
||
149 | * @param Writing_On_GitHub_Blob $blob Blob to validate. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | protected function importable_post( Writing_On_GitHub_Blob $blob ) { |
||
154 | // global $wpdb; |
||
155 | |||
156 | // // Skip the repo's readme. |
||
157 | // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) { |
||
158 | // return false; |
||
159 | // } |
||
160 | |||
161 | // // If the blob sha already matches a post, then move on. |
||
162 | // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) { |
||
163 | // return false; |
||
164 | // } |
||
165 | |||
166 | if ( ! $blob->has_frontmatter() ) { |
||
167 | return false; |
||
168 | } |
||
169 | |||
170 | return true; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Delete post |
||
175 | * @param Writing_On_GitHub_Blob $blob |
||
176 | * @return WP_Error|bool |
||
177 | */ |
||
178 | protected function delete_post( Writing_On_GitHub_Blob $blob ) { |
||
179 | $id = $blob->id(); |
||
180 | if ( empty( $id ) ) { |
||
181 | return false; |
||
182 | } |
||
183 | $result = $this->app->database()->delete_post( $id ); |
||
184 | if ( is_wp_error( $result ) ) { |
||
185 | /* @var WP_Error $result */ |
||
186 | return $result; |
||
187 | } |
||
188 | return true; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Imports a post into wordpress |
||
193 | * @param Writing_On_GitHub_Blob $blob |
||
194 | * @param boolean $force |
||
195 | * @return WP_Error|bool |
||
196 | */ |
||
197 | protected function import_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
||
198 | $post = $this->blob_to_post( $blob, $force ); |
||
199 | |||
200 | if ( ! $post instanceof Writing_On_GitHub_Post ) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | $result = $this->app->database()->save_post( $post ); |
||
205 | if ( is_wp_error( $result ) ) { |
||
206 | /** @var WP_Error $result */ |
||
207 | return $result; |
||
208 | } |
||
209 | |||
210 | if ( $post->is_new() || |
||
211 | ! wogh_equal_front_matter( $post, $blob ) ) { |
||
212 | |||
213 | $result = $this->app->export()->export_post( $post ); |
||
214 | |||
215 | if ( is_wp_error( $result ) ) { |
||
216 | /** @var WP_Error $result */ |
||
217 | return $result; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | clean_post_cache( $post->id() ); |
||
222 | |||
223 | return true; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * import raw file |
||
228 | * @param Writing_On_GitHub_Blob $blob |
||
229 | * @return bool |
||
230 | */ |
||
231 | protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) { |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Imports a raw file content into file system. |
||
246 | * @param Writing_On_GitHub_Blob $blob |
||
247 | * @param bool $is_remove |
||
248 | */ |
||
249 | protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) { |
||
250 | $arr = wp_upload_dir(); |
||
251 | $path = $arr['basedir'] . '/writing-on-github/' . $blob->path(); |
||
252 | if ( $is_remove ) { |
||
253 | if ( file_exists($path) ) { |
||
254 | unlink($path); |
||
255 | } |
||
256 | } else { |
||
257 | $dirname = dirname($path); |
||
258 | if ( ! file_exists($dirname) ) { |
||
259 | wp_mkdir_p($dirname); |
||
260 | } |
||
261 | |||
262 | file_put_contents($path, $blob->content()); |
||
263 | } |
||
264 | return true; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Imports a single blob content into matching post. |
||
269 | * |
||
270 | * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post. |
||
271 | * @param boolean $force |
||
272 | * |
||
273 | * @return Writing_On_GitHub_Post|false |
||
274 | */ |
||
275 | protected function blob_to_post( Writing_On_GitHub_Blob $blob, $force = false ) { |
||
329 | } |
||
330 | } |
||
331 |