| Conditions | 16 |
| Paths | 142 |
| Total Lines | 90 |
| Lines | 12 |
| Ratio | 13.33 % |
| 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 |
||
| 43 | public function get_messages( $message_path, $query, $full_jp_logo_exists ) { |
||
| 44 | $jitm_engine = new Engine(); |
||
| 45 | |||
| 46 | $query_string = array(); |
||
| 47 | if ( isset( $query ) ) { |
||
| 48 | foreach ( explode( ',', $query ) as $query_item ) { |
||
| 49 | $query_item = explode( '=', $query_item ); |
||
| 50 | $query_string[ $query_item[0] ] = isset( $query_item[1] ) ? $query_item[1] : null; |
||
| 51 | } |
||
| 52 | unset( $query_item ); |
||
| 53 | } |
||
| 54 | |||
| 55 | $mobile_browser = jetpack_is_mobile( 'smart' ); |
||
| 56 | $user = wp_get_current_user(); |
||
| 57 | |||
| 58 | // Unauthenticated or invalid requests just bail. |
||
| 59 | if ( ! $user ) { |
||
| 60 | return array(); |
||
| 61 | } |
||
| 62 | |||
| 63 | $user_roles = implode( ',', $user->roles ); |
||
| 64 | |||
| 65 | $envelopes = $jitm_engine->get_top_messages( $message_path, $user->ID, $user_roles, $query_string, $mobile_browser ); |
||
| 66 | |||
| 67 | if ( ! is_array( $envelopes ) ) { |
||
| 68 | return array(); |
||
| 69 | } |
||
| 70 | |||
| 71 | $hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' ); |
||
| 72 | |||
| 73 | unset( $envelopes['last_response_time'] ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Allow adding your own custom JITMs after a set of JITMs has been received. |
||
| 77 | * |
||
| 78 | * @since 6.9.0 |
||
| 79 | * @since 8.3.0 - Added Message path. |
||
| 80 | * |
||
| 81 | * @param array $envelopes array of existing JITMs. |
||
| 82 | * @param string $message_path The message path to ask for. |
||
| 83 | */ |
||
| 84 | $envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path ); |
||
| 85 | |||
| 86 | foreach ( $envelopes as $idx => &$envelope ) { |
||
| 87 | |||
| 88 | $dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null; |
||
| 89 | |||
| 90 | // If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed. |
||
| 91 | View Code Duplication | if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) { |
|
| 92 | unset( $envelopes[ $idx ] ); |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | $normalized_site_url = \Jetpack::build_raw_urls( get_home_url() ); |
||
| 97 | |||
| 98 | $url_params = array( |
||
| 99 | 'source' => "jitm-$envelope->id", |
||
| 100 | 'site' => $normalized_site_url, |
||
| 101 | 'u' => $user->ID, |
||
| 102 | ); |
||
| 103 | |||
| 104 | // Get affiliate code and add it to the array of URL parameters. |
||
| 105 | $aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ); |
||
| 106 | if ( '' !== $aff ) { |
||
| 107 | $url_params['aff'] = $aff; |
||
| 108 | } |
||
| 109 | |||
| 110 | $envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' ); |
||
| 111 | |||
| 112 | View Code Duplication | if ( $envelope->cta['hook'] ) { |
|
| 113 | $envelope->url = apply_filters( 'jitm_' . $envelope->cta['hook'], $envelope->url ); |
||
| 114 | unset( $envelope->cta['hook'] ); |
||
| 115 | } |
||
| 116 | |||
| 117 | View Code Duplication | if ( isset( $envelope->content['hook'] ) ) { |
|
| 118 | $envelope->content = apply_filters( 'jitm_' . $envelope->content['hook'], $envelope->content ); |
||
| 119 | unset( $envelope->content['hook'] ); |
||
| 120 | } |
||
| 121 | |||
| 122 | // No point in showing an empty message. |
||
| 123 | if ( empty( $envelope->content['message'] ) ) { |
||
| 124 | unset( $envelopes[ $idx ] ); |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | |||
| 128 | $envelope->content['icon'] = $this->generate_icon( $envelope->content['icon'], $full_jp_logo_exists ); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $envelopes; |
||
| 132 | } |
||
| 133 | |||
| 135 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: