| Conditions | 24 |
| Paths | 10 |
| Total Lines | 97 |
| 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 |
||
| 91 | function wpcom_shortcodereverse_getty( $content ) { |
||
| 92 | if ( ! is_string( $content ) || false === stripos( $content, '.gettyimages.com/' ) ) { |
||
| 93 | return $content; |
||
| 94 | } |
||
| 95 | |||
| 96 | $regexp = '!<iframe\s+src=[\'"](https?:)?//embed\.gettyimages\.com/embed(/|/?\?assets=)([a-z0-9_-]+(,[a-z0-9_-]+)*)[^\'"]*?[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i'; |
||
| 97 | $regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
||
| 98 | |||
| 99 | // Markup pattern for 2017 embed syntax with significant differences from the prior pattern. |
||
| 100 | $regexp_2017 = '!<a.+?class=\'gie-(single|slideshow)\'.+?gie\.widgets\.load\({([^}]+)}\).+?embed-cdn\.gettyimages\.com/widgets\.js.+?</script>!'; |
||
| 101 | $regexp_2017_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp_2017, ENT_NOQUOTES ) ); |
||
| 102 | |||
| 103 | foreach ( compact( 'regexp_2017', 'regexp_2017_ent', 'regexp', 'regexp_ent' ) as $reg => $regexp ) { |
||
| 104 | if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) { |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | foreach ( $matches as $match ) { |
||
| 109 | if ( 'regexp_2017' === $reg || 'regexp_2017_ent' === $reg ) { |
||
| 110 | // Extract individual keys from the matched JavaScript object. |
||
| 111 | $params = $match[2]; |
||
| 112 | if ( ! preg_match_all( '!(?P<key>\w+)\s*:\s*([\'"](?P<value>[^\'"]*?)(px)?[\'"])!', $params, $key_matches, PREG_SET_ORDER ) ) { |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ( $key_matches as $key_match ) { |
||
| 117 | switch ( $key_match['key'] ) { |
||
| 118 | case 'items': |
||
| 119 | $ids = $key_match['value']; |
||
| 120 | break; |
||
| 121 | case 'w': |
||
| 122 | $width = (int) $key_match['value']; |
||
| 123 | break; |
||
| 124 | case 'h': |
||
| 125 | $height = (int) $key_match['value']; |
||
| 126 | break; |
||
| 127 | case 'tld': |
||
| 128 | $tld = $key_match['value']; |
||
| 129 | break; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | $params = $match[5]; |
||
| 134 | if ( 'regexp_ent' === $reg ) { |
||
| 135 | $params = html_entity_decode( $params ); |
||
| 136 | } |
||
| 137 | $params = wp_kses_hair( $params, array( 'http' ) ); |
||
| 138 | |||
| 139 | $ids = esc_html( $match[3] ); |
||
| 140 | $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; |
||
| 141 | $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; |
||
| 142 | } |
||
| 143 | |||
| 144 | if ( empty( $ids ) ) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $shortcode = '[getty src="' . esc_attr( $ids ) . '"'; |
||
| 149 | if ( ! empty( $width ) ) { |
||
| 150 | $shortcode .= ' width="' . esc_attr( $width ) . '"'; |
||
| 151 | } |
||
| 152 | if ( ! empty( $height ) ) { |
||
| 153 | $shortcode .= ' height="' . esc_attr( $height ) . '"'; |
||
| 154 | } |
||
| 155 | |||
| 156 | /* |
||
| 157 | * While it does not appear to have any practical impact, Getty has |
||
| 158 | * requested that we include TLD in the embed request |
||
| 159 | */ |
||
| 160 | if ( ! empty( $tld ) ) { |
||
| 161 | $shortcode .= ' tld="' . esc_attr( $tld ) . '"'; |
||
| 162 | } |
||
| 163 | $shortcode .= ']'; |
||
| 164 | |||
| 165 | $content = str_replace( $match[0], $shortcode, $content ); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | // strip out enclosing div and any other markup. |
||
| 170 | $regexp = '%<div class="getty\s[^>]*+>.*?<div[^>]*+>(\[getty[^\]]*+\])\s*</div>.*?</div>%is'; |
||
| 171 | $regexp_ent = str_replace( array( '&#0*58;', '[^>]' ), array( '&#0*58;|�*58;', '[^&]' ), htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
||
| 172 | |||
| 173 | foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) { |
||
| 174 | if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) { |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | foreach ( $matches as $match ) { |
||
| 179 | $content = str_replace( $match[0], $match[1], $content ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
| 184 | do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'getty' ); |
||
| 185 | |||
| 186 | return $content; |
||
| 187 | } |
||
| 188 | |||
| 224 |
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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.