| Conditions | 2 |
| Paths | 2 |
| Total Lines | 245 |
| Code Lines | 180 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 123 | private static function initSchema(Connection $connection): void |
||
| 124 | { |
||
| 125 | $fromSchema = $connection->getSchemaManager()->createSchema(); |
||
| 126 | $toSchema = clone $fromSchema; |
||
| 127 | |||
| 128 | $db = new FluidSchema($toSchema); |
||
| 129 | |||
| 130 | $db->table('country') |
||
| 131 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 132 | ->column('label')->string(255); |
||
| 133 | |||
| 134 | $db->table('person') |
||
| 135 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 136 | ->column('name')->string(255); |
||
| 137 | |||
| 138 | $toSchema->getTable('person') |
||
| 139 | ->addColumn( |
||
| 140 | 'created_at', |
||
| 141 | 'datetime', |
||
| 142 | ['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
||
| 143 | ); |
||
| 144 | |||
| 145 | $db->table('person') |
||
| 146 | ->column('modified_at')->datetime()->null() |
||
| 147 | ->column('order')->integer()->null(); |
||
| 148 | |||
| 149 | |||
| 150 | $db->table('contact') |
||
| 151 | ->extends('person') |
||
| 152 | ->column('email')->string(255) |
||
| 153 | ->column('manager_id')->references('contact')->null(); |
||
| 154 | |||
| 155 | $db->table('users') |
||
| 156 | ->extends('contact') |
||
| 157 | ->column('login')->string(255) |
||
| 158 | ->column('password')->string(255)->null() |
||
| 159 | ->column('status')->string(10)->null()->default(null) |
||
| 160 | ->column('country_id')->references('country'); |
||
| 161 | |||
| 162 | $db->table('rights') |
||
| 163 | ->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
||
| 164 | |||
| 165 | $db->table('roles') |
||
| 166 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 167 | ->column('name')->string(255) |
||
| 168 | ->column('created_at')->date()->null() |
||
| 169 | ->column('status')->boolean()->null()->default(1); |
||
| 170 | |||
| 171 | $db->table('roles_rights') |
||
| 172 | ->column('role_id')->references('roles') |
||
| 173 | ->column('right_label')->references('rights')->then() |
||
| 174 | ->primaryKey(['role_id', 'right_label']); |
||
| 175 | |||
| 176 | $db->table('users_roles') |
||
| 177 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 178 | ->column('user_id')->references('users') |
||
| 179 | ->column('role_id')->references('roles'); |
||
| 180 | |||
| 181 | $db->table('all_nullable') |
||
| 182 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 183 | ->column('label')->string(255)->null() |
||
| 184 | ->column('country_id')->references('country')->null(); |
||
| 185 | |||
| 186 | $db->table('animal') |
||
| 187 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 188 | ->column('name')->string(45)->index() |
||
| 189 | ->column('order')->integer()->null(); |
||
| 190 | |||
| 191 | $db->table('dog') |
||
| 192 | ->extends('animal') |
||
| 193 | ->column('race')->string(45)->null(); |
||
| 194 | |||
| 195 | $db->table('cat') |
||
| 196 | ->extends('animal') |
||
| 197 | ->column('cuteness_level')->integer()->null(); |
||
| 198 | |||
| 199 | $db->table('boats') |
||
| 200 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 201 | ->column('name')->string(255) |
||
| 202 | ->column('anchorage_country')->references('country')->notNull(); |
||
| 203 | |||
| 204 | $db->table('sailed_countries') |
||
| 205 | ->column('boat_id')->references('boats') |
||
| 206 | ->column('country_id')->references('country') |
||
| 207 | ->then()->primaryKey(['boat_id', 'country_id']); |
||
| 208 | |||
| 209 | $db->table('category') |
||
| 210 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 211 | ->column('label')->string(255) |
||
| 212 | ->column('parent_id')->references('category')->null(); |
||
| 213 | |||
| 214 | $db->table('article') |
||
| 215 | ->column('id')->string(36)->primaryKey()->comment('@UUID') |
||
| 216 | ->column('content')->string(255); |
||
| 217 | |||
| 218 | $db->table('article2') |
||
| 219 | ->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
||
| 220 | ->column('content')->string(255); |
||
| 221 | |||
| 222 | $toSchema->getTable('users') |
||
| 223 | ->addUniqueIndex(['login'], 'users_login_idx') |
||
| 224 | ->addUniqueIndex(['login'], 'users_login_idx_2') // We create the same index twice |
||
| 225 | ->addIndex(['status', 'country_id'], 'users_status_country_idx'); |
||
| 226 | |||
| 227 | $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
||
| 228 | |||
| 229 | foreach ($sqlStmts as $sqlStmt) { |
||
| 230 | $connection->exec($sqlStmt); |
||
| 231 | } |
||
| 232 | |||
| 233 | $connection->insert('country', [ |
||
| 234 | 'label' => 'France', |
||
| 235 | ]); |
||
| 236 | $connection->insert('country', [ |
||
| 237 | 'label' => 'UK', |
||
| 238 | ]); |
||
| 239 | $connection->insert('country', [ |
||
| 240 | 'label' => 'Jamaica', |
||
| 241 | ]); |
||
| 242 | |||
| 243 | $connection->insert('person', [ |
||
| 244 | 'name' => 'John Smith', |
||
| 245 | 'created_at' => '2015-10-24 11:57:13', |
||
| 246 | ]); |
||
| 247 | $connection->insert('person', [ |
||
| 248 | 'name' => 'Jean Dupont', |
||
| 249 | 'created_at' => '2015-10-24 11:57:13', |
||
| 250 | ]); |
||
| 251 | $connection->insert('person', [ |
||
| 252 | 'name' => 'Robert Marley', |
||
| 253 | 'created_at' => '2015-10-24 11:57:13', |
||
| 254 | ]); |
||
| 255 | $connection->insert('person', [ |
||
| 256 | 'name' => 'Bill Shakespeare', |
||
| 257 | 'created_at' => '2015-10-24 11:57:13', |
||
| 258 | ]); |
||
| 259 | |||
| 260 | $connection->insert('contact', [ |
||
| 261 | 'id' => 1, |
||
| 262 | 'email' => '[email protected]', |
||
| 263 | 'manager_id' => null, |
||
| 264 | ]); |
||
| 265 | $connection->insert('contact', [ |
||
| 266 | 'id' => 2, |
||
| 267 | 'email' => '[email protected]', |
||
| 268 | 'manager_id' => null, |
||
| 269 | ]); |
||
| 270 | $connection->insert('contact', [ |
||
| 271 | 'id' => 3, |
||
| 272 | 'email' => '[email protected]', |
||
| 273 | 'manager_id' => null, |
||
| 274 | ]); |
||
| 275 | $connection->insert('contact', [ |
||
| 276 | 'id' => 4, |
||
| 277 | 'email' => '[email protected]', |
||
| 278 | 'manager_id' => 1, |
||
| 279 | ]); |
||
| 280 | |||
| 281 | $connection->insert('rights', [ |
||
| 282 | 'label' => 'CAN_SING', |
||
| 283 | ]); |
||
| 284 | $connection->insert('rights', [ |
||
| 285 | 'label' => 'CAN_WRITE', |
||
| 286 | ]); |
||
| 287 | |||
| 288 | $connection->insert('roles', [ |
||
| 289 | 'name' => 'Admins', |
||
| 290 | 'created_at' => '2015-10-24' |
||
| 291 | ]); |
||
| 292 | $connection->insert('roles', [ |
||
| 293 | 'name' => 'Writers', |
||
| 294 | 'created_at' => '2015-10-24' |
||
| 295 | ]); |
||
| 296 | $connection->insert('roles', [ |
||
| 297 | 'name' => 'Singers', |
||
| 298 | 'created_at' => '2015-10-24' |
||
| 299 | ]); |
||
| 300 | |||
| 301 | $connection->insert('roles_rights', [ |
||
| 302 | 'role_id' => 1, |
||
| 303 | 'right_label' => 'CAN_SING' |
||
| 304 | ]); |
||
| 305 | $connection->insert('roles_rights', [ |
||
| 306 | 'role_id' => 3, |
||
| 307 | 'right_label' => 'CAN_SING' |
||
| 308 | ]); |
||
| 309 | $connection->insert('roles_rights', [ |
||
| 310 | 'role_id' => 1, |
||
| 311 | 'right_label' => 'CAN_WRITE' |
||
| 312 | ]); |
||
| 313 | $connection->insert('roles_rights', [ |
||
| 314 | 'role_id' => 2, |
||
| 315 | 'right_label' => 'CAN_WRITE' |
||
| 316 | ]); |
||
| 317 | |||
| 318 | $connection->insert('users', [ |
||
| 319 | 'id' => 1, |
||
| 320 | 'login' => 'john.smith', |
||
| 321 | 'password' => null, |
||
| 322 | 'status' => 'on', |
||
| 323 | 'country_id' => 2 |
||
| 324 | ]); |
||
| 325 | $connection->insert('users', [ |
||
| 326 | 'id' => 2, |
||
| 327 | 'login' => 'jean.dupont', |
||
| 328 | 'password' => null, |
||
| 329 | 'status' => 'on', |
||
| 330 | 'country_id' => 1 |
||
| 331 | ]); |
||
| 332 | $connection->insert('users', [ |
||
| 333 | 'id' => 3, |
||
| 334 | 'login' => 'robert.marley', |
||
| 335 | 'password' => null, |
||
| 336 | 'status' => 'off', |
||
| 337 | 'country_id' => 3 |
||
| 338 | ]); |
||
| 339 | $connection->insert('users', [ |
||
| 340 | 'id' => 4, |
||
| 341 | 'login' => 'bill.shakespeare', |
||
| 342 | 'password' => null, |
||
| 343 | 'status' => 'off', |
||
| 344 | 'country_id' => 2 |
||
| 345 | ]); |
||
| 346 | |||
| 347 | $connection->insert('users_roles', [ |
||
| 348 | 'user_id' => 1, |
||
| 349 | 'role_id' => 1, |
||
| 350 | ]); |
||
| 351 | $connection->insert('users_roles', [ |
||
| 352 | 'user_id' => 2, |
||
| 353 | 'role_id' => 1, |
||
| 354 | ]); |
||
| 355 | $connection->insert('users_roles', [ |
||
| 356 | 'user_id' => 3, |
||
| 357 | 'role_id' => 3, |
||
| 358 | ]); |
||
| 359 | $connection->insert('users_roles', [ |
||
| 360 | 'user_id' => 4, |
||
| 361 | 'role_id' => 2, |
||
| 362 | ]); |
||
| 363 | $connection->insert('users_roles', [ |
||
| 364 | 'user_id' => 3, |
||
| 365 | 'role_id' => 2, |
||
| 366 | ]); |
||
| 367 | } |
||
| 368 | } |
||
| 369 |