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