| Conditions | 55 |
| Paths | 14112 |
| Total Lines | 266 |
| Lines | 18 |
| Ratio | 6.77 % |
| 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 // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 106 | public static function extract_from_content( $content, $what_to_extract = self::ALL, $already_extracted = array() ) { |
||
| 107 | $stripped_content = self::get_stripped_content( $content ); |
||
| 108 | |||
| 109 | // Maybe start with some previously extracted things (e.g. images from extract(). |
||
| 110 | $extracted = $already_extracted; |
||
| 111 | |||
| 112 | // Embedded media objects will have already been converted to shortcodes by pre_kses hooks on save. |
||
| 113 | |||
| 114 | if ( self::IMAGES & $what_to_extract ) { |
||
| 115 | $images = self::extract_images_from_content( $stripped_content, array() ); |
||
| 116 | $extracted = array_merge( $extracted, $images ); |
||
| 117 | } |
||
| 118 | |||
| 119 | // ----------------------------------- MENTIONS ------------------------------ |
||
| 120 | |||
| 121 | View Code Duplication | if ( self::MENTIONS & $what_to_extract ) { |
|
| 122 | if ( preg_match_all( '/(^|\s)@(\w+)/u', $stripped_content, $matches ) ) { |
||
| 123 | $mentions = array_values( array_unique( $matches[2] ) ); // array_unique() retains the keys! |
||
| 124 | $mentions = array_map( 'strtolower', $mentions ); |
||
| 125 | $extracted['mention'] = array( 'name' => $mentions ); |
||
| 126 | if ( ! isset( $extracted['has'] ) ) { |
||
| 127 | $extracted['has'] = array(); |
||
| 128 | } |
||
| 129 | $extracted['has']['mention'] = count( $mentions ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | // ----------------------------------- HASHTAGS ------------------------------ |
||
| 134 | /** |
||
| 135 | * Some hosts may not compile with --enable-unicode-properties and kick a warning: |
||
| 136 | * Warning: preg_match_all() [function.preg-match-all]: Compilation failed: support for \P, \p, and \X has not been compiled |
||
| 137 | * Therefore, we only run this code block on wpcom, not in Jetpack. |
||
| 138 | */ |
||
| 139 | if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && ( self::HASHTAGS & $what_to_extract ) ) { |
||
| 140 | // This regex does not exactly match Twitter's |
||
| 141 | // if there are problems/complaints we should implement this: |
||
| 142 | // https://github.com/twitter/twitter-text/blob/master/java/src/com/twitter/Regex.java . |
||
| 143 | View Code Duplication | if ( preg_match_all( '/(?:^|\s)#(\w*\p{L}+\w*)/u', $stripped_content, $matches ) ) { |
|
| 144 | $hashtags = array_values( array_unique( $matches[1] ) ); // array_unique() retains the keys! |
||
| 145 | $hashtags = array_map( 'strtolower', $hashtags ); |
||
| 146 | $extracted['hashtag'] = array( 'name' => $hashtags ); |
||
| 147 | if ( ! isset( $extracted['has'] ) ) { |
||
| 148 | $extracted['has'] = array(); |
||
| 149 | } |
||
| 150 | $extracted['has']['hashtag'] = count( $hashtags ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | // ----------------------------------- SHORTCODES ------------------------------ |
||
| 155 | |||
| 156 | // Always look for shortcodes. |
||
| 157 | // If we don't want them, we'll just remove them, so we don't grab them as links below. |
||
| 158 | $shortcode_pattern = '/' . get_shortcode_regex() . '/s'; |
||
| 159 | if ( preg_match_all( $shortcode_pattern, $content, $matches ) ) { |
||
| 160 | |||
| 161 | $shortcode_total_count = 0; |
||
| 162 | $shortcode_type_counts = array(); |
||
| 163 | $shortcode_types = array(); |
||
| 164 | $shortcode_details = array(); |
||
| 165 | |||
| 166 | if ( self::SHORTCODES & $what_to_extract ) { |
||
| 167 | |||
| 168 | foreach ( $matches[2] as $key => $shortcode ) { |
||
| 169 | // Elasticsearch (and probably other things) doesn't deal well with some chars as key names. |
||
| 170 | $shortcode_name = preg_replace( '/[.,*"\'\/\\\\#+ ]/', '_', $shortcode ); |
||
| 171 | |||
| 172 | $attr = shortcode_parse_atts( $matches[3][ $key ] ); |
||
| 173 | |||
| 174 | $shortcode_total_count++; |
||
| 175 | if ( ! isset( $shortcode_type_counts[ $shortcode_name ] ) ) { |
||
| 176 | $shortcode_type_counts[ $shortcode_name ] = 0; |
||
| 177 | } |
||
| 178 | $shortcode_type_counts[ $shortcode_name ]++; |
||
| 179 | |||
| 180 | // Store (uniquely) presence of all shortcode regardless of whether it's a keeper (for those, get ID below) |
||
| 181 | // @todo Store number of occurrences? |
||
| 182 | if ( ! in_array( $shortcode_name, $shortcode_types, true ) ) { |
||
| 183 | $shortcode_types[] = $shortcode_name; |
||
| 184 | } |
||
| 185 | |||
| 186 | // For keeper shortcodes, also store the id/url of the object (e.g. youtube video, TED talk, etc.). |
||
| 187 | if ( in_array( $shortcode, self::$keeper_shortcodes, true ) ) { |
||
| 188 | // Clear shortcode ID data left from the last shortcode. |
||
| 189 | $id = null; |
||
| 190 | // We'll try to get the salient ID from the function jetpack_shortcode_get_xyz_id(). |
||
| 191 | // If the shortcode is a class, we'll call XyzShortcode::get_xyz_id(). |
||
| 192 | $shortcode_get_id_func = "jetpack_shortcode_get_{$shortcode}_id"; |
||
| 193 | $shortcode_class_name = ucfirst( $shortcode ) . 'Shortcode'; |
||
| 194 | $shortcode_get_id_method = "get_{$shortcode}_id"; |
||
| 195 | if ( function_exists( $shortcode_get_id_func ) ) { |
||
| 196 | $id = call_user_func( $shortcode_get_id_func, $attr ); |
||
| 197 | } elseif ( method_exists( $shortcode_class_name, $shortcode_get_id_method ) ) { |
||
| 198 | $id = call_user_func( array( $shortcode_class_name, $shortcode_get_id_method ), $attr ); |
||
| 199 | } |
||
| 200 | if ( ! empty( $id ) |
||
| 201 | && ( ! isset( $shortcode_details[ $shortcode_name ] ) || ! in_array( $id, $shortcode_details[ $shortcode_name ], true ) ) ) { |
||
| 202 | $shortcode_details[ $shortcode_name ][] = $id; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if ( $shortcode_total_count > 0 ) { |
||
| 208 | // Add the shortcode info to the $extracted array. |
||
| 209 | if ( ! isset( $extracted['has'] ) ) { |
||
| 210 | $extracted['has'] = array(); |
||
| 211 | } |
||
| 212 | $extracted['has']['shortcode'] = $shortcode_total_count; |
||
| 213 | $extracted['shortcode'] = array(); |
||
| 214 | foreach ( $shortcode_type_counts as $type => $count ) { |
||
| 215 | $extracted['shortcode'][ $type ] = array( 'count' => $count ); |
||
| 216 | } |
||
| 217 | if ( ! empty( $shortcode_types ) ) { |
||
| 218 | $extracted['shortcode_types'] = $shortcode_types; |
||
| 219 | } |
||
| 220 | foreach ( $shortcode_details as $type => $id ) { |
||
| 221 | $extracted['shortcode'][ $type ]['id'] = $id; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | // Remove the shortcodes form our copy of $content, so we don't count links in them as links below. |
||
| 227 | $content = preg_replace( $shortcode_pattern, ' ', $content ); |
||
| 228 | } |
||
| 229 | |||
| 230 | // ----------------------------------- LINKS ------------------------------ |
||
| 231 | |||
| 232 | if ( self::LINKS & $what_to_extract ) { |
||
| 233 | |||
| 234 | // To hold the extracted stuff we find. |
||
| 235 | $links = array(); |
||
| 236 | |||
| 237 | // @todo Get the text inside the links? |
||
| 238 | |||
| 239 | // Grab any links, whether in <a href="..." or not, but subtract those from shortcodes and images. |
||
| 240 | // (we treat embed links as just another link). |
||
| 241 | if ( preg_match_all( '#(?:^|\s|"|\')(https?://([^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))))#', $content, $matches ) ) { |
||
| 242 | |||
| 243 | foreach ( $matches[1] as $link_raw ) { |
||
| 244 | $url = wp_parse_url( $link_raw ); |
||
| 245 | |||
| 246 | // Data URI links. |
||
| 247 | if ( ! isset( $url['scheme'] ) || 'data' === $url['scheme'] ) { |
||
| 248 | continue; |
||
| 249 | } |
||
| 250 | |||
| 251 | // Reject invalid URLs. |
||
| 252 | if ( ! isset( $url['host'] ) ) { |
||
| 253 | continue; |
||
| 254 | } |
||
| 255 | |||
| 256 | // Remove large (and likely invalid) links. |
||
| 257 | if ( 4096 < strlen( $link_raw ) ) { |
||
| 258 | continue; |
||
| 259 | } |
||
| 260 | |||
| 261 | // Build a simple form of the URL so we can compare it to ones we found in IMAGES or SHORTCODES and exclude those. |
||
| 262 | $simple_url = $url['scheme'] . '://' . $url['host'] . ( ! empty( $url['path'] ) ? $url['path'] : '' ); |
||
| 263 | if ( isset( $extracted['image']['url'] ) ) { |
||
| 264 | if ( in_array( $simple_url, (array) $extracted['image']['url'], true ) ) { |
||
| 265 | continue; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | list( $proto, $link_all_but_proto ) = explode( '://', $link_raw ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 270 | |||
| 271 | // Build a reversed hostname. |
||
| 272 | $host_parts = array_reverse( explode( '.', $url['host'] ) ); |
||
| 273 | $host_reversed = ''; |
||
| 274 | foreach ( $host_parts as $part ) { |
||
| 275 | $host_reversed .= ( ! empty( $host_reversed ) ? '.' : '' ) . $part; |
||
| 276 | } |
||
| 277 | |||
| 278 | $link_analyzed = ''; |
||
| 279 | if ( ! empty( $url['path'] ) ) { |
||
| 280 | // The whole path (no query args or fragments). |
||
| 281 | $path = substr( $url['path'], 1 ); // strip the leading '/'. |
||
| 282 | $link_analyzed .= ( ! empty( $link_analyzed ) ? ' ' : '' ) . $path; |
||
| 283 | |||
| 284 | // The path split by /. |
||
| 285 | $path_split = explode( '/', $path ); |
||
| 286 | if ( count( $path_split ) > 1 ) { |
||
| 287 | $link_analyzed .= ' ' . implode( ' ', $path_split ); |
||
| 288 | } |
||
| 289 | |||
| 290 | // The fragment. |
||
| 291 | if ( ! empty( $url['fragment'] ) ) { |
||
| 292 | $link_analyzed .= ( ! empty( $link_analyzed ) ? ' ' : '' ) . $url['fragment']; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | // @todo Check unique before adding |
||
| 297 | $links[] = array( |
||
| 298 | 'url' => $link_all_but_proto, |
||
| 299 | 'host_reversed' => $host_reversed, |
||
| 300 | 'host' => $url['host'], |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | $link_count = count( $links ); |
||
| 306 | if ( $link_count ) { |
||
| 307 | $extracted['link'] = $links; |
||
| 308 | if ( ! isset( $extracted['has'] ) ) { |
||
| 309 | $extracted['has'] = array(); |
||
| 310 | } |
||
| 311 | $extracted['has']['link'] = $link_count; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | // ----------------------------------- EMBEDS ------------------------------ |
||
| 316 | |||
| 317 | // Embeds are just individual links on their own line. |
||
| 318 | if ( self::EMBEDS & $what_to_extract ) { |
||
| 319 | |||
| 320 | if ( ! function_exists( '_wp_oembed_get_object' ) ) { |
||
| 321 | include ABSPATH . WPINC . '/class-oembed.php'; |
||
| 322 | } |
||
| 323 | |||
| 324 | // get an oembed object. |
||
| 325 | $oembed = _wp_oembed_get_object(); |
||
| 326 | |||
| 327 | // Grab any links on their own lines that may be embeds. |
||
| 328 | if ( preg_match_all( '|^\s*(https?://[^\s"]+)\s*$|im', $content, $matches ) ) { |
||
| 329 | |||
| 330 | // To hold the extracted stuff we find. |
||
| 331 | $embeds = array(); |
||
| 332 | |||
| 333 | foreach ( $matches[1] as $link_raw ) { |
||
| 334 | $url = wp_parse_url( $link_raw ); |
||
| 335 | |||
| 336 | list( $proto, $link_all_but_proto ) = explode( '://', $link_raw ); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 337 | |||
| 338 | // Check whether this "link" is really an embed. |
||
| 339 | foreach ( $oembed->providers as $matchmask => $data ) { |
||
| 340 | list( $providerurl, $regex ) = $data; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 341 | |||
| 342 | // Turn the asterisk-type provider URLs into regex. |
||
| 343 | if ( ! $regex ) { |
||
| 344 | $matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i'; |
||
| 345 | $matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask ); |
||
| 346 | } |
||
| 347 | |||
| 348 | if ( preg_match( $matchmask, $link_raw ) ) { |
||
| 349 | $embeds[] = $link_all_but_proto; // @todo Check unique before adding |
||
| 350 | |||
| 351 | // @todo Try to get ID's for the ones we care about (shortcode_keepers) |
||
| 352 | break; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | if ( ! empty( $embeds ) ) { |
||
| 358 | if ( ! isset( $extracted['has'] ) ) { |
||
| 359 | $extracted['has'] = array(); |
||
| 360 | } |
||
| 361 | $extracted['has']['embed'] = count( $embeds ); |
||
| 362 | $extracted['embed'] = array( 'url' => array() ); |
||
| 363 | foreach ( $embeds as $e ) { |
||
| 364 | $extracted['embed']['url'][] = $e; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | return $extracted; |
||
| 371 | } |
||
| 372 | |||
| 519 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.