| Conditions | 5 | 
| Paths | 5 | 
| Total Lines | 197 | 
| Code Lines | 74 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 10 | ||
| Bugs | 0 | Features | 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  | 
            ||
| 39 | 	public function execute() { | 
            ||
| 40 | global $wpdb;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * Requirements.  | 
            ||
| 44 | */  | 
            ||
| 45 | require_once ABSPATH . 'wp-admin/includes/upgrade.php';  | 
            ||
| 46 | |||
| 47 | /**  | 
            ||
| 48 | * Other.  | 
            ||
| 49 | */  | 
            ||
| 50 | $charset_collate = $wpdb->get_charset_collate();  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * Queries.  | 
            ||
| 54 | */  | 
            ||
| 55 | $queries = "  | 
            ||
| 56 | CREATE TABLE $wpdb->pronamic_pay_mollie_organizations (  | 
            ||
| 57 | id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,  | 
            ||
| 58 | mollie_id VARCHAR( 16 ) NOT NULL,  | 
            ||
| 59 | name VARCHAR( 128 ) DEFAULT NULL,  | 
            ||
| 60 | email VARCHAR( 100 ) DEFAULT NULL,  | 
            ||
| 61 | PRIMARY KEY ( id ),  | 
            ||
| 62 | UNIQUE KEY mollie_id ( mollie_id )  | 
            ||
| 63 | ) $charset_collate;  | 
            ||
| 64 | CREATE TABLE $wpdb->pronamic_pay_mollie_profiles (  | 
            ||
| 65 | id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,  | 
            ||
| 66 | mollie_id VARCHAR( 16 ) NOT NULL,  | 
            ||
| 67 | organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,  | 
            ||
| 68 | name VARCHAR( 128 ) DEFAULT NULL,  | 
            ||
| 69 | email VARCHAR( 100 ) DEFAULT NULL,  | 
            ||
| 70 | api_key_test VARCHAR( 35 ) DEFAULT NULL,  | 
            ||
| 71 | api_key_live VARCHAR( 35 ) DEFAULT NULL,  | 
            ||
| 72 | PRIMARY KEY ( id ),  | 
            ||
| 73 | UNIQUE KEY mollie_id ( mollie_id ),  | 
            ||
| 74 | KEY organization_id ( organization_id )  | 
            ||
| 75 | ) $charset_collate;  | 
            ||
| 76 | CREATE TABLE $wpdb->pronamic_pay_mollie_customers (  | 
            ||
| 77 | id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,  | 
            ||
| 78 | mollie_id VARCHAR( 16 ) NOT NULL,  | 
            ||
| 79 | organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,  | 
            ||
| 80 | profile_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,  | 
            ||
| 81 | test_mode BOOL NOT NULL,  | 
            ||
| 82 | email VARCHAR( 100 ) DEFAULT NULL,  | 
            ||
| 83 | name VARCHAR( 255 ) DEFAULT NULL,  | 
            ||
| 84 | PRIMARY KEY ( id ),  | 
            ||
| 85 | UNIQUE KEY mollie_id ( mollie_id ),  | 
            ||
| 86 | KEY organization_id ( organization_id ),  | 
            ||
| 87 | KEY profile_id ( profile_id ),  | 
            ||
| 88 | KEY test_mode ( test_mode ),  | 
            ||
| 89 | KEY email ( email )  | 
            ||
| 90 | ) $charset_collate;  | 
            ||
| 91 | CREATE TABLE $wpdb->pronamic_pay_mollie_customer_users (  | 
            ||
| 92 | id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,  | 
            ||
| 93 | customer_id BIGINT( 20 ) UNSIGNED NOT NULL,  | 
            ||
| 94 | user_id BIGINT( 20 ) UNSIGNED NOT NULL,  | 
            ||
| 95 | PRIMARY KEY ( id ),  | 
            ||
| 96 | UNIQUE KEY customer_user ( customer_id, user_id )  | 
            ||
| 97 | ) $charset_collate;  | 
            ||
| 98 | ";  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * Execute.  | 
            ||
| 102 | *  | 
            ||
| 103 | * @link https://developer.wordpress.org/reference/functions/dbdelta/  | 
            ||
| 104 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/upgrade.php#L2538-L2915  | 
            ||
| 105 | */  | 
            ||
| 106 | \dbDelta( $queries );  | 
            ||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * Foreign keys.  | 
            ||
| 110 | *  | 
            ||
| 111 | * @link https://core.trac.wordpress.org/ticket/19207  | 
            ||
| 112 | * @link https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html  | 
            ||
| 113 | */  | 
            ||
| 114 | $data = array(  | 
            ||
| 115 | (object) array(  | 
            ||
| 116 | 'table' => $wpdb->pronamic_pay_mollie_profiles,  | 
            ||
| 117 | 'name' => 'fk_profile_organization_id',  | 
            ||
| 118 | 'query' => "  | 
            ||
| 119 | ALTER TABLE $wpdb->pronamic_pay_mollie_profiles  | 
            ||
| 120 | ADD CONSTRAINT fk_profile_organization_id  | 
            ||
| 121 | FOREIGN KEY ( organization_id )  | 
            ||
| 122 | REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )  | 
            ||
| 123 | ON DELETE RESTRICT  | 
            ||
| 124 | ON UPDATE RESTRICT  | 
            ||
| 125 | ;  | 
            ||
| 126 | ",  | 
            ||
| 127 | ),  | 
            ||
| 128 | (object) array(  | 
            ||
| 129 | 'table' => $wpdb->pronamic_pay_mollie_customers,  | 
            ||
| 130 | 'name' => 'fk_customer_organization_id',  | 
            ||
| 131 | 'query' => "  | 
            ||
| 132 | ALTER TABLE $wpdb->pronamic_pay_mollie_customers  | 
            ||
| 133 | ADD CONSTRAINT fk_customer_organization_id  | 
            ||
| 134 | FOREIGN KEY ( organization_id )  | 
            ||
| 135 | REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )  | 
            ||
| 136 | ON DELETE RESTRICT  | 
            ||
| 137 | ON UPDATE RESTRICT  | 
            ||
| 138 | ;  | 
            ||
| 139 | ",  | 
            ||
| 140 | ),  | 
            ||
| 141 | (object) array(  | 
            ||
| 142 | 'table' => $wpdb->pronamic_pay_mollie_customers,  | 
            ||
| 143 | 'name' => 'fk_customer_profile_id',  | 
            ||
| 144 | 'query' => "  | 
            ||
| 145 | ALTER TABLE $wpdb->pronamic_pay_mollie_customers  | 
            ||
| 146 | ADD CONSTRAINT fk_customer_profile_id  | 
            ||
| 147 | FOREIGN KEY ( profile_id )  | 
            ||
| 148 | REFERENCES $wpdb->pronamic_pay_mollie_profiles ( id )  | 
            ||
| 149 | ON DELETE RESTRICT  | 
            ||
| 150 | ON UPDATE RESTRICT  | 
            ||
| 151 | ;  | 
            ||
| 152 | ",  | 
            ||
| 153 | ),  | 
            ||
| 154 | (object) array(  | 
            ||
| 155 | 'table' => $wpdb->pronamic_pay_mollie_customer_users,  | 
            ||
| 156 | 'name' => 'fk_customer_id',  | 
            ||
| 157 | 'query' => "  | 
            ||
| 158 | ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users  | 
            ||
| 159 | ADD CONSTRAINT fk_customer_id  | 
            ||
| 160 | FOREIGN KEY customer_id ( customer_id )  | 
            ||
| 161 | REFERENCES $wpdb->pronamic_pay_mollie_customers ( id )  | 
            ||
| 162 | ON DELETE RESTRICT  | 
            ||
| 163 | ON UPDATE RESTRICT  | 
            ||
| 164 | ;  | 
            ||
| 165 | ",  | 
            ||
| 166 | ),  | 
            ||
| 167 | (object) array(  | 
            ||
| 168 | 'table' => $wpdb->pronamic_pay_mollie_customer_users,  | 
            ||
| 169 | 'name' => 'fk_customer_user_id',  | 
            ||
| 170 | 'query' => "  | 
            ||
| 171 | ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users  | 
            ||
| 172 | ADD CONSTRAINT fk_customer_user_id  | 
            ||
| 173 | FOREIGN KEY user_id ( user_id )  | 
            ||
| 174 | REFERENCES $wpdb->users ( id )  | 
            ||
| 175 | ON DELETE CASCADE  | 
            ||
| 176 | ON UPDATE CASCADE  | 
            ||
| 177 | ;  | 
            ||
| 178 | ",  | 
            ||
| 179 | ),  | 
            ||
| 180 | );  | 
            ||
| 181 | |||
| 182 | 		foreach ( $data as $item ) { | 
            ||
| 183 | /**  | 
            ||
| 184 | * Check if foreign key exists  | 
            ||
| 185 | *  | 
            ||
| 186 | * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-install.php#L663-L681  | 
            ||
| 187 | */  | 
            ||
| 188 | $result = $wpdb->get_var(  | 
            ||
| 189 | $wpdb->prepare(  | 
            ||
| 190 | "  | 
            ||
| 191 | SELECT COUNT(*)  | 
            ||
| 192 | FROM information_schema.TABLE_CONSTRAINTS  | 
            ||
| 193 | WHERE CONSTRAINT_SCHEMA = %s  | 
            ||
| 194 | AND CONSTRAINT_NAME = %s  | 
            ||
| 195 | AND CONSTRAINT_TYPE = 'FOREIGN KEY'  | 
            ||
| 196 | AND TABLE_NAME = %s  | 
            ||
| 197 | ",  | 
            ||
| 198 | $wpdb->dbname,  | 
            ||
| 199 | $item->name,  | 
            ||
| 200 | $item->table  | 
            ||
| 201 | )  | 
            ||
| 202 | );  | 
            ||
| 203 | |||
| 204 | 			if ( null === $result ) { | 
            ||
| 205 | throw new \Exception(  | 
            ||
| 206 | \sprintf(  | 
            ||
| 207 | 'Could not count foreign keys: %s, database error: %s.',  | 
            ||
| 208 | $$item->name,  | 
            ||
| 209 | $wpdb->last_error  | 
            ||
| 210 | )  | 
            ||
| 211 | );  | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | $number_constraints = \intval( $result );  | 
            ||
| 215 | |||
| 216 | 			if ( 0 === $number_constraints ) { | 
            ||
| 217 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.  | 
            ||
| 218 | $result = $wpdb->query( $item->query );  | 
            ||
| 219 | |||
| 220 | 				if ( false === $result ) { | 
            ||
| 221 | throw new \Exception(  | 
            ||
| 222 | \sprintf(  | 
            ||
| 223 | 'Could not add foreign key: %s, database error: %s.',  | 
            ||
| 224 | $item->name,  | 
            ||
| 225 | $wpdb->last_error  | 
            ||
| 226 | )  | 
            ||
| 227 | );  | 
            ||
| 228 | }  | 
            ||
| 229 | }  | 
            ||
| 230 | }  | 
            ||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * Convert user meta.  | 
            ||
| 234 | */  | 
            ||
| 235 | $this->convert_user_meta();  | 
            ||
| 236 | }  | 
            ||
| 317 |