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:
| 1 | <?php |
||
| 10 | class Writing_On_GitHub_Persist_Client extends Writing_On_GitHub_Base_Client { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Get the data for the current user. |
||
| 14 | * |
||
| 15 | * @return array |
||
| 16 | */ |
||
| 17 | 1 | protected function export_user() { |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Delete the file. |
||
| 33 | * |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | 1 | public function delete_file( $path, $sha, $message ) { |
|
| 37 | 1 | $body = new stdClass(); |
|
| 38 | 1 | $body->message = $message; |
|
| 39 | 1 | $body->sha = $sha; |
|
| 40 | 1 | $body->branch = $this->branch(); |
|
| 41 | |||
| 42 | 1 | if ( $author = $this->export_user() ) { |
|
| 43 | $body->author = $author; |
||
| 44 | } |
||
| 45 | |||
| 46 | 1 | return $this->call( 'DELETE', $this->content_endpoint( $path ), $body ); |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Create the file. |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public function create_file( $blob, $message ) { |
||
| 55 | $body = $blob->to_body(); |
||
| 56 | $body->message = $message; |
||
| 57 | $body->branch = $this->branch(); |
||
| 58 | unset($body->sha); |
||
| 59 | |||
| 60 | if ( $author = $this->export_user() ) { |
||
| 61 | $body->author = $author; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->call( 'PUT', $this->content_endpoint( $blob->path() ), $body ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Update the file. |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function update_file( $blob, $message ) { |
||
| 82 | } |
||
| 83 | } |
||
| 84 |