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 |
||
| 11 | class save implements api_action { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Process the post save |
||
| 15 | * |
||
| 16 | * @since 0.9.2 |
||
| 17 | * |
||
| 18 | * @param array $data Sanitized data to use for saving. |
||
| 19 | * |
||
| 20 | * @return bool Always returns true. |
||
| 21 | */ |
||
| 22 | View Code Duplication | public function content( $data ) { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Process the post save |
||
| 50 | * |
||
| 51 | * @since 0.9.2 |
||
| 52 | * |
||
| 53 | * @param array $data Sanitized data to use for saving. |
||
| 54 | * |
||
| 55 | * @return bool Always returns true. |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function publish_content( $data ) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * The keys required for the actions of this class. |
||
| 83 | * |
||
| 84 | * @since 0.9.2 |
||
| 85 | * |
||
| 86 | * @return array Array of keys to pull from $_POST per action and their sanitization callback |
||
| 87 | */ |
||
| 88 | public static function params(){ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Additional auth callbacks to check. |
||
| 104 | * |
||
| 105 | * @since 0.9.2 |
||
| 106 | * |
||
| 107 | * @return array Array of additional functions to use to authorize action. |
||
| 108 | */ |
||
| 109 | public static function auth_callbacks() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check if saving post is disabled. |
||
| 122 | * |
||
| 123 | * @since 0.9.2 |
||
| 124 | * |
||
| 125 | * @access protected |
||
| 126 | * |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | protected function save_to_post_disables() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Replace shortcodes from other plugins with shortcode tags. |
||
| 138 | * |
||
| 139 | * @since 0.9.9 |
||
| 140 | * |
||
| 141 | * @access protected |
||
| 142 | * |
||
| 143 | * @param string $content |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | protected function replace_rendered_shortcodes( $content ) { |
||
| 165 | |||
| 166 | protected function remove_comments($content) { |
||
| 169 | |||
| 170 | } |
||
| 171 |
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.