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 |
||
| 17 | class Wordlift_Post_Adapter { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The post id to which the adopter relates. |
||
| 21 | * |
||
| 22 | * @since 3.14.0 |
||
| 23 | * |
||
| 24 | * @var integer $post_id . |
||
| 25 | */ |
||
| 26 | private $post_id; |
||
| 27 | |||
| 28 | const TYPE_ENTITY_LINK = 0; |
||
| 29 | const TYPE_TERM_LINK = 1; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create the {@link Wordlift_Post_Adatpter} instance. |
||
| 33 | * |
||
| 34 | * @param integer $post_id the post ID of the post the adopter relates to. |
||
| 35 | * |
||
| 36 | * @since 3.14.0 |
||
| 37 | * |
||
| 38 | */ |
||
| 39 | public function __construct( $post_id ) { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get the word count of the post content. |
||
| 47 | * |
||
| 48 | * The count is calculated over the post content after stripping shortcodes and html tags. |
||
| 49 | * |
||
| 50 | * @return integer the number of words in the content after stripping shortcodes and html tags.. |
||
| 51 | * @since 3.14.0 |
||
| 52 | * |
||
| 53 | */ |
||
| 54 | public function word_count() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Count words in the string, taking into account UTF-8 characters. |
||
| 71 | * |
||
| 72 | * @see https://github.com/insideout10/wordlift-plugin/issues/884 |
||
| 73 | * |
||
| 74 | * @since 3.20.0 |
||
| 75 | * |
||
| 76 | * @param string $str The target string. |
||
| 77 | * |
||
| 78 | * @return int The number of words. |
||
| 79 | */ |
||
| 80 | private static function str_word_count_utf8( $str ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
||
| 87 | * |
||
| 88 | * @param int $post_id Post ID. |
||
| 89 | * |
||
| 90 | * @return string The post permalink. |
||
| 91 | * @since 3.20.0 |
||
| 92 | * |
||
| 93 | */ |
||
| 94 | public static function get_production_permalink( $post_id ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get comma separated tags to be used as keywords |
||
| 171 | * |
||
| 172 | * @return string|void Comma separated tags |
||
| 173 | * |
||
| 174 | * @since 3.27.2 |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function keywords() { |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get comma separated categories to be used as article section |
||
| 190 | * |
||
| 191 | * @return string|void Comma separated categories |
||
| 192 | * |
||
| 193 | * @since 3.27.2 |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function article_section() { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Get comment count |
||
| 209 | * |
||
| 210 | * @return string|void Comment count |
||
| 211 | * |
||
| 212 | * @since 3.27.2 |
||
| 213 | */ |
||
| 214 | public function comment_count() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get Language |
||
| 220 | * Try WPML, Polylang for post specific languages, else fallback on get_locale() |
||
| 221 | * |
||
| 222 | * @return string|void Language code (locale) |
||
| 223 | * |
||
| 224 | * @since 3.27.2 |
||
| 225 | */ |
||
| 226 | public function locale() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Add mentions to an article. |
||
| 245 | * To add mentions we just push the referenced entities to mentions. |
||
| 246 | * |
||
| 247 | * @param $post_id int |
||
| 248 | * @param $references int[] |
||
| 249 | */ |
||
| 250 | public function add_mentions( $post_id, &$references ) { |
||
| 272 | |||
| 273 | } |
||
| 274 |
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.