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 Jetpack_Podcast_Helper { |
||
| 12 | /** |
||
| 13 | * Gets podcast data formatted to be used by the Podcast Player block in both server-side |
||
| 14 | * block rendering and in API `WPCOM_REST_API_V2_Endpoint_Podcast_Player`. |
||
| 15 | * |
||
| 16 | * The result is cached for one hour. |
||
| 17 | * |
||
| 18 | * @param string $feed The RSS feed to load and list tracks for. |
||
| 19 | * @return array|WP_Error The player data or a error object. |
||
| 20 | */ |
||
| 21 | public static function get_player_data( $feed ) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Gets a list of tracks for the supplied RSS feed. |
||
| 70 | * |
||
| 71 | * @param string $rss The RSS feed to load and list tracks for. |
||
| 72 | * @return array|WP_Error The feed's tracks or a error object. |
||
| 73 | */ |
||
| 74 | private static function get_track_list( $rss ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Formats string as pure plaintext, with no HTML tags or entities present. |
||
| 85 | * This is ready to be used in React, innerText but needs to be escaped |
||
| 86 | * using standard `esc_html` when generating markup on server. |
||
| 87 | * |
||
| 88 | * @param string $str Input string. |
||
| 89 | * @return string Plain text string. |
||
| 90 | */ |
||
| 91 | private static function get_plain_text( $str ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Loads an RSS feed using `fetch_feed`. |
||
| 109 | * |
||
| 110 | * @param string $feed The RSS feed URL to load. |
||
| 111 | * @return SimplePie|WP_Error The RSS object or error. |
||
| 112 | */ |
||
| 113 | View Code Duplication | private static function load_feed( $feed ) { |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Prepares Episode data to be used by the Podcast Player block. |
||
| 129 | * |
||
| 130 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | private static function setup_tracks_callback( SimplePie_Item $episode ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Retrieves an audio enclosure. |
||
| 171 | * |
||
| 172 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
| 173 | * @return SimplePie_Enclosure|null |
||
| 174 | */ |
||
| 175 | private static function get_audio_enclosure( SimplePie_Item $episode ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the track duration as a formatted string. |
||
| 187 | * |
||
| 188 | * @param number $duration of the track in seconds. |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | private static function format_track_duration( $duration ) { |
||
| 196 | } |
||
| 197 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.