| Conditions | 15 |
| Paths | 23 |
| Total Lines | 136 |
| 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 static function verify( $action, $secret_1, $user_id ) { |
||
| 125 | $allowed_actions = array( 'register', 'authorize', 'publicize' ); |
||
| 126 | if ( ! in_array( $action, $allowed_actions, true ) ) { |
||
| 127 | return new \WP_Error( 'unknown_verification_action', 'Unknown Verification Action', 400 ); |
||
| 128 | } |
||
| 129 | |||
| 130 | $user = get_user_by( 'id', $user_id ); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * We've begun verifying the previously generated secret. |
||
| 134 | * |
||
| 135 | * @since 7.5.0 |
||
| 136 | * |
||
| 137 | * @param string $action The type of secret to verify. |
||
| 138 | * @param \WP_User $user The user object. |
||
| 139 | */ |
||
| 140 | do_action( 'jetpack_verify_secrets_begin', $action, $user ); |
||
| 141 | |||
| 142 | $return_error = function ( \WP_Error $error ) use ( $action, $user ) { |
||
| 143 | /** |
||
| 144 | * Verifying of the previously generated secret has failed. |
||
| 145 | * |
||
| 146 | * @since 7.5.0 |
||
| 147 | * |
||
| 148 | * @param string $action The type of secret to verify. |
||
| 149 | * @param \WP_User $user The user object. |
||
| 150 | * @param \WP_Error $error The error object. |
||
| 151 | */ |
||
| 152 | do_action( 'jetpack_verify_secrets_fail', $action, $user, $error ); |
||
| 153 | |||
| 154 | return $error; |
||
| 155 | }; |
||
| 156 | |||
| 157 | $stored_secrets = self::get( $action, $user_id ); |
||
| 158 | self::delete( $action, $user_id ); |
||
| 159 | |||
| 160 | $error = null; |
||
| 161 | if ( empty( $secret_1 ) ) { |
||
| 162 | $error = $return_error( |
||
| 163 | new \WP_Error( |
||
| 164 | 'verify_secret_1_missing', |
||
| 165 | /* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
||
| 166 | sprintf( __( 'The required "%s" parameter is missing.', 'jetpack' ), 'secret_1' ), |
||
| 167 | 400 |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | } elseif ( ! is_string( $secret_1 ) ) { |
||
| 171 | $error = $return_error( |
||
| 172 | new \WP_Error( |
||
| 173 | 'verify_secret_1_malformed', |
||
| 174 | /* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
||
| 175 | sprintf( __( 'The required "%s" parameter is malformed.', 'jetpack' ), 'secret_1' ), |
||
| 176 | 400 |
||
| 177 | ) |
||
| 178 | ); |
||
| 179 | } elseif ( empty( $user_id ) ) { |
||
| 180 | // $user_id is passed around during registration as "state". |
||
| 181 | $error = $return_error( |
||
| 182 | new \WP_Error( |
||
| 183 | 'state_missing', |
||
| 184 | /* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
||
| 185 | sprintf( __( 'The required "%s" parameter is missing.', 'jetpack' ), 'state' ), |
||
| 186 | 400 |
||
| 187 | ) |
||
| 188 | ); |
||
| 189 | } elseif ( ! ctype_digit( (string) $user_id ) ) { |
||
| 190 | $error = $return_error( |
||
| 191 | new \WP_Error( |
||
| 192 | 'state_malformed', |
||
| 193 | /* translators: "%s" is the name of a paramter. It can be either "secret_1" or "state". */ |
||
| 194 | sprintf( __( 'The required "%s" parameter is malformed.', 'jetpack' ), 'state' ), |
||
| 195 | 400 |
||
| 196 | ) |
||
| 197 | ); |
||
| 198 | } elseif ( self::SECRETS_MISSING === $stored_secrets ) { |
||
| 199 | $error = $return_error( |
||
| 200 | new \WP_Error( |
||
| 201 | 'verify_secrets_missing', |
||
| 202 | __( 'Verification secrets not found', 'jetpack' ), |
||
| 203 | 400 |
||
| 204 | ) |
||
| 205 | ); |
||
| 206 | } elseif ( self::SECRETS_EXPIRED === $stored_secrets ) { |
||
| 207 | $error = $return_error( |
||
| 208 | new \WP_Error( |
||
| 209 | 'verify_secrets_expired', |
||
| 210 | __( 'Verification took too long', 'jetpack' ), |
||
| 211 | 400 |
||
| 212 | ) |
||
| 213 | ); |
||
| 214 | } elseif ( ! $stored_secrets ) { |
||
| 215 | $error = $return_error( |
||
| 216 | new \WP_Error( |
||
| 217 | 'verify_secrets_empty', |
||
| 218 | __( 'Verification secrets are empty', 'jetpack' ), |
||
| 219 | 400 |
||
| 220 | ) |
||
| 221 | ); |
||
| 222 | } elseif ( is_wp_error( $stored_secrets ) ) { |
||
| 223 | $stored_secrets->add_data( 400 ); |
||
| 224 | $error = $return_error( $stored_secrets ); |
||
| 225 | } elseif ( empty( $stored_secrets['secret_1'] ) || empty( $stored_secrets['secret_2'] ) || empty( $stored_secrets['exp'] ) ) { |
||
| 226 | $error = $return_error( |
||
| 227 | new \WP_Error( |
||
| 228 | 'verify_secrets_incomplete', |
||
| 229 | __( 'Verification secrets are incomplete', 'jetpack' ), |
||
| 230 | 400 |
||
| 231 | ) |
||
| 232 | ); |
||
| 233 | } elseif ( ! hash_equals( $secret_1, $stored_secrets['secret_1'] ) ) { |
||
| 234 | $error = $return_error( |
||
| 235 | new \WP_Error( |
||
| 236 | 'verify_secrets_mismatch', |
||
| 237 | __( 'Secret mismatch', 'jetpack' ), |
||
| 238 | 400 |
||
| 239 | ) |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | // Something went wrong during the checks, returning the error. |
||
| 244 | if ( ! empty( $error ) ) { |
||
| 245 | return $error; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * We've succeeded at verifying the previously generated secret. |
||
| 250 | * |
||
| 251 | * @since 7.5.0 |
||
| 252 | * |
||
| 253 | * @param string $action The type of secret to verify. |
||
| 254 | * @param \WP_User $user The user object. |
||
| 255 | */ |
||
| 256 | do_action( 'jetpack_verify_secrets_success', $action, $user ); |
||
| 257 | |||
| 258 | return $stored_secrets['secret_2']; |
||
| 259 | } |
||
| 260 | } |
||
| 261 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.