Conditions | 10 |
Paths | 10 |
Total Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
124 | public function render_video_block_with_videopress( $attributes, $content ) { |
||
125 | if ( ! isset( $attributes['id'] ) || isset( $attributes['guid'] ) ) { |
||
126 | return $content; |
||
127 | } |
||
128 | |||
129 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
130 | $blog_id = get_current_blog_id(); |
||
131 | } elseif ( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) { |
||
132 | /** |
||
133 | * We're intentionally not using `get_current_blog_id` because it was returning unexpected values. |
||
134 | * |
||
135 | * @see https://github.com/Automattic/jetpack/pull/11193#issuecomment-457883886 |
||
136 | * @see https://github.com/Automattic/jetpack/pull/11193/commits/215cf789f3d8bd03ff9eb1bbdb693acb8831d273 |
||
137 | */ |
||
138 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
139 | } |
||
140 | |||
141 | if ( ! isset( $blog_id ) ) { |
||
142 | return $content; |
||
143 | } |
||
144 | |||
145 | $post_id = absint( $attributes['id'] ); |
||
146 | $videopress_id = video_get_info_by_blogpostid( $blog_id, $post_id )->guid; |
||
147 | $videopress_data = videopress_get_video_details( $videopress_id ); |
||
148 | |||
149 | if ( empty( $videopress_data->file_url_base->https ) || empty( $videopress_data->files->hd->mp4 ) ) { |
||
150 | return $content; |
||
151 | } |
||
152 | |||
153 | $videopress_url = $videopress_data->file_url_base->https . $videopress_data->files->hd->mp4; |
||
154 | |||
155 | $pattern = '/(\s)src=([\'"])(?:(?!\2).)+?\2/'; |
||
156 | |||
157 | return preg_replace( |
||
158 | $pattern, |
||
159 | sprintf( |
||
160 | '\1src="%1$s"', |
||
161 | esc_url_raw( $videopress_url ) |
||
162 | ), |
||
163 | $content, |
||
164 | 1 |
||
165 | ); |
||
166 | } |
||
167 | } |
||
170 |
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.