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 specific track from the supplied feed URL. |
||
70 | * |
||
71 | * @param string $feed The RSS feed to find the track in. |
||
72 | * @param string $guid The GUID of the track. |
||
73 | * @return array|WP_Error The track object or an error object. |
||
74 | */ |
||
75 | public static function get_track_data( $feed, $guid ) { |
||
109 | |||
110 | /** |
||
111 | * Gets a list of tracks for the supplied RSS feed. |
||
112 | * |
||
113 | * @param string $rss The RSS feed to load and list tracks for. |
||
114 | * @return array|WP_Error The feed's tracks or a error object. |
||
115 | */ |
||
116 | private static function get_track_list( $rss ) { |
||
124 | |||
125 | /** |
||
126 | * Formats string as pure plaintext, with no HTML tags or entities present. |
||
127 | * This is ready to be used in React, innerText but needs to be escaped |
||
128 | * using standard `esc_html` when generating markup on server. |
||
129 | * |
||
130 | * @param string $str Input string. |
||
131 | * @return string Plain text string. |
||
132 | */ |
||
133 | private static function get_plain_text( $str ) { |
||
148 | |||
149 | /** |
||
150 | * Loads an RSS feed using `fetch_feed`. |
||
151 | * |
||
152 | * @param string $feed The RSS feed URL to load. |
||
153 | * @return SimplePie|WP_Error The RSS object or error. |
||
154 | */ |
||
155 | View Code Duplication | private static function load_feed( $feed ) { |
|
168 | |||
169 | /** |
||
170 | * Prepares Episode data to be used by the Podcast Player block. |
||
171 | * |
||
172 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
173 | * @return array |
||
174 | */ |
||
175 | private static function setup_tracks_callback( SimplePie_Item $episode ) { |
||
210 | |||
211 | /** |
||
212 | * Retrieves an audio enclosure. |
||
213 | * |
||
214 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
215 | * @return SimplePie_Enclosure|null |
||
216 | */ |
||
217 | private static function get_audio_enclosure( SimplePie_Item $episode ) { |
||
226 | |||
227 | /** |
||
228 | * Returns the track duration as a formatted string. |
||
229 | * |
||
230 | * @param number $duration of the track in seconds. |
||
231 | * @return string |
||
232 | */ |
||
233 | private static function format_track_duration( $duration ) { |
||
238 | } |
||
239 |
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.