Completed
Push — update/anchor-fm-render-enrich... ( 895076 )
by
unknown
214:16 queued 205:30
created

Jetpack_Podcast_Helper   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 373
Duplicated Lines 8.04 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 30
loc 373
rs 9.0399
c 0
b 0
f 0
wmc 42
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B get_player_data() 0 44 8
B get_track_data() 0 32 8
A get_track_list() 0 14 2
A get_plain_text() 15 15 2
A get_rich_text() 15 15 2
A load_feed() 0 14 3
A set_podcast_locator() 0 7 2
A setup_tracks_callback() 0 38 5
A get_episode_image_url() 0 7 2
A get_audio_enclosure() 0 9 3
A format_track_duration() 0 5 2
A get_player_data_schema() 0 24 1
A get_tracks_schema() 0 37 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
/**
3
 * Helper to massage Podcast data to be used in the Podcast block.
4
 *
5
 * @package jetpack
6
 */
7
8
/**
9
 * Class Jetpack_Podcast_Helper
10
 */
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 ) {
25
		$this->feed = esc_url_raw( $feed );
26
	}
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
	 * @return array|WP_Error  The player data or a error object.
35
	 */
36
	public function get_player_data() {
37
		// Try loading data from the cache.
38
		$transient_key = 'jetpack_podcast_' . md5( $this->feed );
39
		$player_data   = get_transient( $transient_key );
40
41
		// Fetch data if we don't have any cached.
42
		if ( false === $player_data || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
43
			// Load feed.
44
			$rss = $this->load_feed();
45
46
			if ( is_wp_error( $rss ) ) {
47
				return $rss;
48
			}
49
50
			// Get tracks.
51
			$tracks = $this->get_track_list();
52
53
			if ( empty( $tracks ) ) {
54
				return new WP_Error( 'no_tracks', __( 'Your Podcast couldn\'t be embedded as it doesn\'t contain any tracks. Please double check your URL.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'no_tracks'.

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.

Loading history...
55
			}
56
57
			// Get podcast meta.
58
			$title = $rss->get_title();
59
			$title = $this->get_plain_text( $title );
60
61
			$cover = $rss->get_image_url();
62
			$cover = ! empty( $cover ) ? esc_url( $cover ) : null;
63
64
			$link = $rss->get_link();
65
			$link = ! empty( $link ) ? esc_url( $link ) : null;
66
67
			$player_data = array(
68
				'title'  => $title,
69
				'link'   => $link,
70
				'cover'  => $cover,
71
				'tracks' => $tracks,
72
			);
73
74
			// Cache for 1 hour.
75
			set_transient( $transient_key, $player_data, HOUR_IN_SECONDS );
76
		}
77
78
		return $player_data;
79
	}
80
81
	/**
82
	 * Gets a specific track from the supplied feed URL.
83
	 *
84
	 * @param string $guid     The GUID of the track.
85
	 * @return array|WP_Error  The track object or an error object.
86
	 */
87
	public function get_track_data( $guid ) {
88
		// Try loading track data from the cache.
89
		$transient_key = 'jetpack_podcast_' . md5( "$this->feed::$guid" );
90
		$track_data    = get_transient( $transient_key );
91
92
		// Fetch data if we don't have any cached.
93
		if ( false === $track_data || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
94
			// Load feed.
95
			$rss = $this->load_feed();
96
97
			if ( is_wp_error( $rss ) ) {
98
				return $rss;
99
			}
100
101
			// Loop over all tracks to find the one.
102
			foreach ( $rss->get_items() as $track ) {
0 ignored issues
show
Bug introduced by
The method get_items does only exist in SimplePie, but not in WP_Error.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
103
				if ( $guid === $track->get_id() ) {
104
					$track_data = $this->setup_tracks_callback( $track );
105
					break;
106
				}
107
			}
108
109
			if ( false === $track_data ) {
110
				return new WP_Error( 'no_track', __( 'The track was not found.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'no_track'.

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.

Loading history...
111
			}
112
113
			// Cache for 1 hour.
114
			set_transient( $transient_key, $track_data, HOUR_IN_SECONDS );
115
		}
116
117
		return $track_data;
118
	}
119
120
	/**
121
	 * Gets a list of tracks for the supplied RSS feed.
122
	 *
123
	 * @return array|WP_Error The feed's tracks or a error object.
124
	 */
125
	public function get_track_list() {
126
		$rss = $this->load_feed();
127
128
		if ( is_wp_error( $rss ) ) {
129
			return $rss;
130
		}
131
132
		// Get first ten items and format them.
133
		$track_list = array_map( array( __CLASS__, 'setup_tracks_callback' ), $rss->get_items( 0, 10 ) );
0 ignored issues
show
Unused Code introduced by
The call to SimplePie::get_items() has too many arguments starting with 0.

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.

Loading history...
Bug introduced by
The method get_items does only exist in SimplePie, but not in WP_Error.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
134
135
		// Filter out any tracks that are empty.
136
		// Reset the array indicies.
137
		return array_values( array_filter( $track_list ) );
138
	}
139
140
	/**
141
	 * Formats string as pure plaintext, with no HTML tags or entities present.
142
	 * This is ready to be used in React, innerText but needs to be escaped
143
	 * using standard `esc_html` when generating markup on server.
144
	 *
145
	 * @param string $str Input string.
146
	 * @return string Plain text string.
147
	 */
148 View Code Duplication
	protected function get_plain_text( $str ) {
149
		// Trim string and return if empty.
150
		$str = trim( (string) $str );
151
		if ( empty( $str ) ) {
152
			return '';
153
		}
154
155
		// Make sure there are no tags.
156
		$str = wp_strip_all_tags( $str );
157
158
		// Replace all entities with their characters, including all types of quotes.
159
		$str = html_entity_decode( $str, ENT_QUOTES );
160
161
		return $str;
162
	}
163
164
	/**
165
	 * Formats strings as safe HTML.
166
	 *
167
	 * @param string $str Input string.
168
	 * @return string HTML text string.
169
	 */
170 View Code Duplication
	protected function get_rich_text( $str ) {
171
		// Trim string and return if empty.
172
		$str = trim( (string) $str );
173
		if ( empty( $str ) ) {
174
			return '';
175
		}
176
177
		// Make sure HTML is safe.
178
		$str = wp_kses_post( $str );
179
180
		// Replace all entities with their characters, including all types of quotes.
181
		$str = html_entity_decode( $str, ENT_QUOTES );
182
183
		return $str;
184
	}
185
186
	/**
187
	 * Loads an RSS feed using `fetch_feed`.
188
	 *
189
	 * @return SimplePie|WP_Error The RSS object or error.
190
	 */
191
	public function load_feed() {
192
		add_action( 'wp_feed_options', array( __CLASS__, 'set_podcast_locator' ) );
193
		$rss = fetch_feed( $this->feed );
194
		remove_action( 'wp_feed_options', array( __CLASS__, 'set_podcast_locator' ) );
195
		if ( is_wp_error( $rss ) ) {
196
			return new WP_Error( 'invalid_url', __( 'Your podcast couldn\'t be embedded. Please double check your URL.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_url'.

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.

Loading history...
197
		}
198
199
		if ( ! $rss->get_item_quantity() ) {
200
			return new WP_Error( 'no_tracks', __( 'Podcast audio RSS feed has no tracks.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'no_tracks'.

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.

Loading history...
201
		}
202
203
		return $rss;
204
	}
205
206
	/**
207
	 * Action handler to set our podcast specific feed locator class on the SimplePie object.
208
	 *
209
	 * @param SimplePie $feed The SimplePie object, passed by reference.
210
	 */
211
	public static function set_podcast_locator( &$feed ) {
212
		if ( ! class_exists( 'Jetpack_Podcast_Feed_Locator' ) ) {
213
			jetpack_require_lib( 'class-jetpack-podcast-feed-locator' );
214
		}
215
216
		$feed->set_locator_class( 'Jetpack_Podcast_Feed_Locator' );
0 ignored issues
show
Bug introduced by
The method set_locator_class() does not seem to exist on object<SimplePie>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
	}
218
219
	/**
220
	 * Prepares Episode data to be used by the Podcast Player block.
221
	 *
222
	 * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode.
223
	 * @return array
224
	 */
225
	protected function setup_tracks_callback( SimplePie_Item $episode ) {
226
		$enclosure = $this->get_audio_enclosure( $episode );
227
228
		// If the audio enclosure is empty then it is not playable.
229
		// We therefore return an empty array for this track.
230
		// It will be filtered out later.
231
		if ( is_wp_error( $enclosure ) ) {
232
			return array();
233
		}
234
235
		// If there is no link return an empty array. We will filter out later.
236
		if ( empty( $enclosure->link ) ) {
237
			return array();
238
		}
239
240
		// Build track data.
241
		$track = array(
242
			'id'                 => wp_unique_id( 'podcast-track-' ),
243
			'link'               => esc_url( $episode->get_link() ),
0 ignored issues
show
Bug introduced by
The method get_link() does not seem to exist on object<SimplePie_Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
244
			'src'                => esc_url( $enclosure->link ),
245
			'type'               => esc_attr( $enclosure->type ),
246
			'description'        => $this->get_plain_text( $episode->get_description() ),
0 ignored issues
show
Bug introduced by
The method get_description() does not seem to exist on object<SimplePie_Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
			'enrich_description' => $this->get_rich_text( $episode->get_description() ),
0 ignored issues
show
Bug introduced by
The method get_description() does not seem to exist on object<SimplePie_Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
248
			'title'              => $this->get_plain_text( $episode->get_title() ),
0 ignored issues
show
Bug introduced by
The method get_title() does not seem to exist on object<SimplePie_Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
249
			'image'              => esc_url( $this->get_episode_image_url( $episode ) ),
250
			'guid'               => $this->get_plain_text( $episode->get_id() ),
251
		);
252
253
		if ( empty( $track['title'] ) ) {
254
			$track['title'] = esc_html__( '(no title)', 'jetpack' );
255
		}
256
257
		if ( ! empty( $enclosure->duration ) ) {
258
			$track['duration'] = esc_html( $this->format_track_duration( $enclosure->duration ) );
259
		}
260
261
		return $track;
262
	}
263
264
	/**
265
	 * Retrieves an episode's image URL, if it's available.
266
	 *
267
	 * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode.
268
	 * @param string         $itunes_ns The itunes namespace, defaulted to the standard 1.0 version.
269
	 * @return string|null The image URL or null if not found.
270
	 */
271
	protected function get_episode_image_url( SimplePie_Item $episode, $itunes_ns = 'http://www.itunes.com/dtds/podcast-1.0.dtd' ) {
272
		$image = $episode->get_item_tags( $itunes_ns, 'image' );
0 ignored issues
show
Bug introduced by
The method get_item_tags() does not seem to exist on object<SimplePie_Item>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
273
		if ( isset( $image[0]['attribs']['']['href'] ) ) {
274
			return $image[0]['attribs']['']['href'];
275
		}
276
		return null;
277
	}
278
279
	/**
280
	 * Retrieves an audio enclosure.
281
	 *
282
	 * @param SimplePie_Item $episode SimplePie_Item object, representing a podcast episode.
283
	 * @return SimplePie_Enclosure|null
284
	 */
285
	protected function get_audio_enclosure( SimplePie_Item $episode ) {
286
		foreach ( (array) $episode->get_enclosures() as $enclosure ) {
287
			if ( 0 === strpos( $enclosure->type, 'audio/' ) ) {
288
				return $enclosure;
289
			}
290
		}
291
292
		return new WP_Error( 'invalid_audio', __( 'Podcast audio is an invalid type.', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_audio'.

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.

Loading history...
293
	}
294
295
	/**
296
	 * Returns the track duration as a formatted string.
297
	 *
298
	 * @param number $duration of the track in seconds.
299
	 * @return string
300
	 */
301
	protected function format_track_duration( $duration ) {
302
		$format = $duration > HOUR_IN_SECONDS ? 'H:i:s' : 'i:s';
303
304
		return date_i18n( $format, $duration );
305
	}
306
307
	/**
308
	 * Gets podcast player data schema.
309
	 *
310
	 * Useful for json schema in REST API endpoints.
311
	 *
312
	 * @return array Player data json schema.
313
	 */
314
	public static function get_player_data_schema() {
315
		return array(
316
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
317
			'title'      => 'jetpack-podcast-player-data',
318
			'type'       => 'object',
319
			'properties' => array(
320
				'title'  => array(
321
					'description' => __( 'The title of the podcast.', 'jetpack' ),
322
					'type'        => 'string',
323
				),
324
				'link'   => array(
325
					'description' => __( 'The URL of the podcast website.', 'jetpack' ),
326
					'type'        => 'string',
327
					'format'      => 'uri',
328
				),
329
				'cover'  => array(
330
					'description' => __( 'The URL of the podcast cover image.', 'jetpack' ),
331
					'type'        => 'string',
332
					'format'      => 'uri',
333
				),
334
				'tracks' => self::get_tracks_schema(),
335
			),
336
		);
337
	}
338
339
	/**
340
	 * Gets tracks data schema.
341
	 *
342
	 * Useful for json schema in REST API endpoints.
343
	 *
344
	 * @return array Tracks json schema.
345
	 */
346
	public static function get_tracks_schema() {
347
		return array(
348
			'description' => __( 'Latest episodes of the podcast.', 'jetpack' ),
349
			'type'        => 'array',
350
			'items'       => array(
351
				'type'       => 'object',
352
				'properties' => array(
353
					'id'          => array(
354
						'description' => __( 'The episode id. Generated per request, not globally unique.', 'jetpack' ),
355
						'type'        => 'string',
356
					),
357
					'link'        => array(
358
						'description' => __( 'The external link for the episode.', 'jetpack' ),
359
						'type'        => 'string',
360
						'format'      => 'uri',
361
					),
362
					'src'         => array(
363
						'description' => __( 'The audio file URL of the episode.', 'jetpack' ),
364
						'type'        => 'string',
365
						'format'      => 'uri',
366
					),
367
					'type'        => array(
368
						'description' => __( 'The mime type of the episode.', 'jetpack' ),
369
						'type'        => 'string',
370
					),
371
					'description' => array(
372
						'description' => __( 'The episode description, in plaintext.', 'jetpack' ),
373
						'type'        => 'string',
374
					),
375
					'title'       => array(
376
						'description' => __( 'The episode title.', 'jetpack' ),
377
						'type'        => 'string',
378
					),
379
				),
380
			),
381
		);
382
	}
383
}
384