Conditions | 2 |
Paths | 2 |
Total Lines | 111 |
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 |
||
152 | function render_modal_block( $widget_id, $is_amp, $attr, $content ) { |
||
153 | |||
154 | if ( $is_amp ) { |
||
155 | $lightbox_id = "{$widget_id}-lightbox"; |
||
156 | |||
157 | // Add CSS to for lightbox. |
||
158 | Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); |
||
159 | |||
160 | $content = preg_replace( |
||
161 | '/\shref="#" target="_blank/', |
||
162 | sprintf( ' on="%s" ', esc_attr( "tap:{$lightbox_id}.open" ) ), |
||
163 | $content |
||
164 | ); |
||
165 | |||
166 | $iframe_src = add_query_arg( |
||
167 | array( |
||
168 | // Note that modal=1 is intentionally omitted here since we need to put the close button inside the amp-lightbox. |
||
169 | 'eid' => $attr['eventId'], |
||
170 | 'parent' => rawurlencode( get_current_url() ), |
||
171 | ), |
||
172 | 'https://www.eventbrite.com/checkout-external' |
||
173 | ); |
||
174 | |||
175 | $lightbox = sprintf( |
||
176 | '<amp-lightbox id="%1$s" on="%2$s" class="eventbrite__lightbox" layout="nodisplay">%3$s</amp-lightbox>', |
||
177 | esc_attr( $lightbox_id ), |
||
178 | esc_attr( "tap:{$lightbox_id}.close" ), |
||
179 | sprintf( |
||
180 | ' |
||
181 | <div class="eventbrite__lighbox-inside"> |
||
182 | <div class="eventbrite__lighbox-iframe-wrapper"> |
||
183 | <amp-iframe class="eventbrite__lighbox-iframe" src="%s" layout="fill" sandbox="allow-scripts allow-same-origin allow-forms"> |
||
184 | <span placeholder=""></span> |
||
185 | </amp-iframe> |
||
186 | <span class="eventbrite__lighbox-close" on="%s" role="button" tabindex="0" aria-label="%s"> |
||
187 | <svg viewBox="0 0 24 24"> |
||
188 | <path d="M13.4 12l3.5-3.5-1.4-1.4-3.5 3.5-3.5-3.5-1.4 1.4 3.5 3.5-3.5 3.5 1.4 1.4 3.5-3.5 3.5 3.5 1.4-1.4z"></path> |
||
189 | </svg> |
||
190 | </span> |
||
191 | </div> |
||
192 | </div> |
||
193 | ', |
||
194 | esc_url( $iframe_src ), |
||
195 | esc_attr( "tap:{$lightbox_id}.close" ), |
||
196 | esc_attr__( 'Close', 'jetpack' ) |
||
197 | ) |
||
198 | ); |
||
199 | |||
200 | $content = preg_replace( |
||
201 | ':(?=</div>\s*$):', |
||
202 | $lightbox, |
||
203 | $content |
||
204 | ); |
||
205 | |||
206 | return $content; |
||
207 | } |
||
208 | |||
209 | wp_enqueue_script( 'eventbrite-widget', 'https://www.eventbrite.com/static/widgets/eb_widgets.js', array(), JETPACK__VERSION, true ); |
||
210 | |||
211 | // Show the modal version. |
||
212 | wp_add_inline_script( |
||
213 | 'eventbrite-widget', |
||
214 | "window.EBWidgets.createWidget( { |
||
215 | widgetType: 'checkout', |
||
216 | eventId: " . absint( $attr['eventId'] ) . ", |
||
217 | modal: true, |
||
218 | modalTriggerElementId: '" . esc_js( $widget_id ) . "', |
||
219 | } );" |
||
220 | ); |
||
221 | |||
222 | // Modal button is saved as an `<a>` element with `role="button"` because `<button>` is not allowed |
||
223 | // by WordPress.com wp_kses. This javascript adds the necessary event handling for button-like behavior. |
||
224 | // @link https://www.w3.org/TR/wai-aria-practices/examples/button/button.html. |
||
225 | wp_add_inline_script( |
||
226 | 'eventbrite-widget', |
||
227 | "( function() { |
||
228 | var widget = document.getElementById( '" . esc_js( $widget_id ) . "' ); |
||
229 | if ( widget ) { |
||
230 | widget.addEventListener( 'click', function( event ) { |
||
231 | event.preventDefault(); |
||
232 | } ); |
||
233 | |||
234 | widget.addEventListener( 'keydown', function( event ) { |
||
235 | // Enter and space keys. |
||
236 | if ( event.keyCode === 13 || event.keyCode === 32 ) { |
||
237 | event.preventDefault(); |
||
238 | event.target && event.target.click(); |
||
239 | } |
||
240 | } ); |
||
241 | } |
||
242 | } )();" |
||
243 | ); |
||
244 | |||
245 | // Replace the placeholder id saved in the post_content with a unique id used by widget.js. |
||
246 | $content = str_replace( 'eventbrite-widget-id', esc_attr( $widget_id ), $content ); |
||
247 | |||
248 | // Fallback for block version deprecated/v2. |
||
249 | $content = preg_replace( '/eventbrite-widget-\d+/', esc_attr( $widget_id ), $content ); |
||
250 | |||
251 | // Inject URL to event in case the JS for the lightbox fails to load. |
||
252 | $content = preg_replace( |
||
253 | '/\shref="#"/', |
||
254 | sprintf( |
||
255 | ' href="%s" rel="noopener noreferrer" target="_blank"', |
||
256 | esc_url( $attr['url'] ) |
||
257 | ), |
||
258 | $content |
||
259 | ); |
||
260 | |||
261 | return $content; |
||
262 | } |
||
263 |
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.