| Conditions | 6 |
| Paths | 5 |
| Total Lines | 59 |
| 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 |
||
| 56 | public function config() { |
||
| 57 | |||
| 58 | $account_info = $this->key_validation_service->get_account_info( (string) $_POST['license'] ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * we need to check if the key is not associated with any account |
||
| 62 | * before setting it, we should check if the url is null. |
||
| 63 | */ |
||
| 64 | if ( is_wp_error( $account_info ) |
||
| 65 | || wp_remote_retrieve_response_code( $account_info ) !== 200 ) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | $account_info_json = $account_info['body']; |
||
| 70 | |||
| 71 | $account_info_data = json_decode( $account_info_json, true ); |
||
| 72 | |||
| 73 | if ( ! $account_info_data ) { |
||
| 74 | // Invalid json returned by api. |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | if ( $account_info_data['url'] !== null ) { |
||
| 79 | // key already associated with another account. |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | $image_string = (string) $_POST['image']; |
||
| 84 | |||
| 85 | $image_decoded_string = base64_decode( $image_string ); |
||
| 86 | |||
| 87 | $upload_dir = wp_upload_dir(); |
||
| 88 | |||
| 89 | $mime_type = $this->get_mime_type_from_string( $image_decoded_string ); |
||
| 90 | |||
| 91 | if ( ! $mime_type ) { |
||
|
|
|||
| 92 | wp_send_json_error( "Image type not valid" ); |
||
| 93 | } |
||
| 94 | |||
| 95 | $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $mime_type; |
||
| 96 | |||
| 97 | file_put_contents( $file_path, $image_decoded_string ); |
||
| 98 | |||
| 99 | $attachment_id = wp_insert_attachment( array(), $file_path ); |
||
| 100 | |||
| 101 | $params = array( |
||
| 102 | 'send_diagnostic' => $_POST['diagnostic'], |
||
| 103 | 'key' => $_POST['license'], |
||
| 104 | 'vocabulary' => $_POST['vocabulary'], |
||
| 105 | 'language' => $_POST['language'], |
||
| 106 | 'name' => $_POST['publisherName'], |
||
| 107 | 'user_type' => $_POST['publisher'], |
||
| 108 | 'logo' => $attachment_id |
||
| 109 | ); |
||
| 110 | |||
| 111 | $this->admin_setup->save_configuration( $params ); |
||
| 112 | |||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | } |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: