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 ) { |
||
28 | |||
29 | /** |
||
30 | * Imports a payload. |
||
31 | * @param Writing_On_GitHub_Payload $payload |
||
32 | * |
||
33 | * @return string|WP_Error |
||
34 | */ |
||
35 | View Code Duplication | 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 | View Code Duplication | public function master() { |
|
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 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.