Complex classes like Jetpack_Podcast_Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Podcast_Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Jetpack_Podcast_Helper { |
||
12 | /** |
||
13 | * The RSS feed of the podcast. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $feed = null; |
||
18 | |||
19 | /** |
||
20 | * Initialize class. |
||
21 | * |
||
22 | * @param string $feed The RSS feed of the podcast. |
||
23 | */ |
||
24 | public function __construct( $feed ) { |
||
27 | |||
28 | /** |
||
29 | * Gets podcast data formatted to be used by the Podcast Player block in both server-side |
||
30 | * block rendering and in API `WPCOM_REST_API_V2_Endpoint_Podcast_Player`. |
||
31 | * |
||
32 | * The result is cached for one hour. |
||
33 | * |
||
34 | * @param array $args { |
||
35 | * Optional array of arguments. |
||
36 | * @type string|int $guid The ID of a specific episode to return rather than a list. |
||
37 | * } |
||
38 | * |
||
39 | * @return array|WP_Error The player data or a error object. |
||
40 | */ |
||
41 | public function get_player_data( $args = array() ) { |
||
97 | |||
98 | /** |
||
99 | * Gets a specific track from the supplied feed URL. |
||
100 | * |
||
101 | * @param string $guid The GUID of the track. |
||
102 | * @return array|WP_Error The track object or an error object. |
||
103 | */ |
||
104 | public function get_track_data( $guid ) { |
||
136 | |||
137 | /** |
||
138 | * Gets a list of tracks for the supplied RSS feed. |
||
139 | * |
||
140 | * @return array|WP_Error The feed's tracks or a error object. |
||
141 | */ |
||
142 | public function get_track_list() { |
||
156 | |||
157 | /** |
||
158 | * Formats string as pure plaintext, with no HTML tags or entities present. |
||
159 | * This is ready to be used in React, innerText but needs to be escaped |
||
160 | * using standard `esc_html` when generating markup on server. |
||
161 | * |
||
162 | * @param string $str Input string. |
||
163 | * @return string Plain text string. |
||
164 | */ |
||
165 | protected function get_plain_text( $str ) { |
||
180 | |||
181 | /** |
||
182 | * Loads an RSS feed using `fetch_feed`. |
||
183 | * |
||
184 | * @return SimplePie|WP_Error The RSS object or error. |
||
185 | */ |
||
186 | public function load_feed() { |
||
200 | |||
201 | /** |
||
202 | * Action handler to set our podcast specific feed locator class on the SimplePie object. |
||
203 | * |
||
204 | * @param SimplePie $feed The SimplePie object, passed by reference. |
||
205 | */ |
||
206 | public static function set_podcast_locator( &$feed ) { |
||
213 | |||
214 | /** |
||
215 | * Prepares Episode data to be used by the Podcast Player block. |
||
216 | * |
||
217 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function setup_tracks_callback( SimplePie_Item $episode ) { |
||
257 | |||
258 | /** |
||
259 | * Retrieves an episode's image URL, if it's available. |
||
260 | * |
||
261 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
262 | * @param string $itunes_ns The itunes namespace, defaulted to the standard 1.0 version. |
||
263 | * @return string|null The image URL or null if not found. |
||
264 | */ |
||
265 | protected function get_episode_image_url( SimplePie_Item $episode, $itunes_ns = 'http://www.itunes.com/dtds/podcast-1.0.dtd' ) { |
||
272 | |||
273 | /** |
||
274 | * Retrieves an audio enclosure. |
||
275 | * |
||
276 | * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode. |
||
277 | * @return SimplePie_Enclosure|null |
||
278 | */ |
||
279 | protected function get_audio_enclosure( SimplePie_Item $episode ) { |
||
288 | |||
289 | /** |
||
290 | * Returns the track duration as a formatted string. |
||
291 | * |
||
292 | * @param number $duration of the track in seconds. |
||
293 | * @return string |
||
294 | */ |
||
295 | protected function format_track_duration( $duration ) { |
||
300 | |||
301 | /** |
||
302 | * Gets podcast player data schema. |
||
303 | * |
||
304 | * Useful for json schema in REST API endpoints. |
||
305 | * |
||
306 | * @return array Player data json schema. |
||
307 | */ |
||
308 | public static function get_player_data_schema() { |
||
332 | |||
333 | /** |
||
334 | * Gets tracks data schema. |
||
335 | * |
||
336 | * Useful for json schema in REST API endpoints. |
||
337 | * |
||
338 | * @return array Tracks json schema. |
||
339 | */ |
||
340 | public static function get_tracks_schema() { |
||
377 | } |
||
378 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.