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