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 ) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Gets a list of tracks for the supplied RSS feed. |
||
| 65 | * |
||
| 66 | * @param string $rss The RSS feed to load and list tracks for. |
||
| 67 | * @return array|WP_Error The feed's tracks or a error object. |
||
| 68 | */ |
||
| 69 | private static function get_track_list( $rss ) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Formats string as pure plaintext, with no HTML tags or entities present. |
||
| 79 | * This is ready to be used in React, innerText but needs to be escaped |
||
| 80 | * using standard `esc_html` when generating markup on server. |
||
| 81 | * |
||
| 82 | * @param string $str Input string. |
||
| 83 | * @return string Plain text string. |
||
| 84 | */ |
||
| 85 | private static function get_plain_text( $str ) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Loads an RSS feed using `fetch_feed`. |
||
| 103 | * |
||
| 104 | * @param string $feed The RSS feed URL to load. |
||
| 105 | * @return SimplePie|WP_Error The RSS object or error. |
||
| 106 | */ |
||
| 107 | View Code Duplication | private static function load_feed( $feed ) { |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Prepares Episode data to be used by the Podcast Player block. |
||
| 123 | * |
||
| 124 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | private static function setup_tracks_callback( SimplePie_Item $episode ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Retrieves an audio enclosure. |
||
| 158 | * |
||
| 159 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
| 160 | * @return SimplePie_Enclosure|null |
||
| 161 | */ |
||
| 162 | private static function get_audio_enclosure( SimplePie_Item $episode ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the track duration as a formatted string. |
||
| 175 | * |
||
| 176 | * @param number $duration of the track in seconds. |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | private static function format_track_duration( $duration ) { |
||
| 184 | } |
||
| 185 |
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.