Conditions | 4 |
Paths | 8 |
Total Lines | 261 |
Code Lines | 189 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
208 | private static function initSchema(Connection $connection): void |
||
209 | { |
||
210 | $fromSchema = $connection->getSchemaManager()->createSchema(); |
||
211 | $toSchema = clone $fromSchema; |
||
212 | |||
213 | $db = new FluidSchema($toSchema); |
||
214 | |||
215 | $db->table('country') |
||
216 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
217 | ->column('label')->string(255); |
||
218 | |||
219 | $db->table('person') |
||
220 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
221 | ->column('name')->string(255); |
||
222 | |||
223 | if ($connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
224 | $toSchema->getTable('person') |
||
225 | ->addColumn( |
||
226 | 'created_at', |
||
227 | 'datetime', |
||
228 | ['columnDefinition' => 'TIMESTAMP(0) DEFAULT SYSDATE NOT NULL'] |
||
229 | ); |
||
230 | } else { |
||
231 | $toSchema->getTable('person') |
||
232 | ->addColumn( |
||
233 | 'created_at', |
||
234 | 'datetime', |
||
235 | ['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
||
236 | ); |
||
237 | } |
||
238 | |||
239 | $db->table('person') |
||
240 | ->column('modified_at')->datetime()->null() |
||
241 | ->column('order')->integer()->null(); |
||
242 | |||
243 | |||
244 | $db->table('contact') |
||
245 | ->extends('person') |
||
246 | ->column('email')->string(255) |
||
247 | ->column('manager_id')->references('contact')->null(); |
||
248 | |||
249 | $db->table('users') |
||
250 | ->extends('contact') |
||
251 | ->column('login')->string(255) |
||
252 | ->column('password')->string(255)->null() |
||
253 | ->column('status')->string(10)->null()->default(null) |
||
254 | ->column('country_id')->references('country'); |
||
255 | |||
256 | $db->table('rights') |
||
257 | ->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
||
258 | |||
259 | $db->table('roles') |
||
260 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
261 | ->column('name')->string(255) |
||
262 | ->column('created_at')->date()->null() |
||
263 | ->column('status')->boolean()->null()->default(1); |
||
264 | |||
265 | $db->table('roles_rights') |
||
266 | ->column('role_id')->references('roles') |
||
267 | ->column('right_label')->references('rights')->then() |
||
268 | ->primaryKey(['role_id', 'right_label']); |
||
269 | |||
270 | $db->table('users_roles') |
||
271 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
272 | ->column('user_id')->references('users') |
||
273 | ->column('role_id')->references('roles'); |
||
274 | |||
275 | $db->table('all_nullable') |
||
276 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
277 | ->column('label')->string(255)->null() |
||
278 | ->column('country_id')->references('country')->null(); |
||
279 | |||
280 | $db->table('animal') |
||
281 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
282 | ->column('name')->string(45)->index() |
||
283 | ->column('order')->integer()->null(); |
||
284 | |||
285 | $db->table('dog') |
||
286 | ->extends('animal') |
||
287 | ->column('race')->string(45)->null(); |
||
288 | |||
289 | $db->table('cat') |
||
290 | ->extends('animal') |
||
291 | ->column('cuteness_level')->integer()->null(); |
||
292 | |||
293 | $db->table('boats') |
||
294 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
295 | ->column('name')->string(255) |
||
296 | ->column('anchorage_country')->references('country')->notNull(); |
||
297 | |||
298 | $db->table('sailed_countries') |
||
299 | ->column('boat_id')->references('boats') |
||
300 | ->column('country_id')->references('country') |
||
301 | ->then()->primaryKey(['boat_id', 'country_id']); |
||
302 | |||
303 | $db->table('category') |
||
304 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
305 | ->column('label')->string(255) |
||
306 | ->column('parent_id')->references('category')->null(); |
||
307 | |||
308 | $db->table('article') |
||
309 | ->column('id')->string(36)->primaryKey()->comment('@UUID') |
||
310 | ->column('content')->string(255); |
||
311 | |||
312 | $db->table('article2') |
||
313 | ->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
||
314 | ->column('content')->string(255); |
||
315 | |||
316 | $toSchema->getTable('users') |
||
317 | ->addUniqueIndex(['login'], 'users_login_idx') |
||
318 | ->addIndex(['status', 'country_id'], 'users_status_country_idx'); |
||
319 | |||
320 | // We create the same index twice |
||
321 | // except for Oracle that won't let us create twice the same index. |
||
322 | if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
323 | $toSchema->getTable('users') |
||
324 | ->addUniqueIndex(['login'], 'users_login_idx_2'); |
||
325 | } |
||
326 | |||
327 | |||
328 | $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
||
329 | |||
330 | foreach ($sqlStmts as $sqlStmt) { |
||
331 | $connection->exec($sqlStmt); |
||
332 | } |
||
333 | |||
334 | $connection->insert('country', [ |
||
335 | 'label' => 'France', |
||
336 | ]); |
||
337 | $connection->insert('country', [ |
||
338 | 'label' => 'UK', |
||
339 | ]); |
||
340 | $connection->insert('country', [ |
||
341 | 'label' => 'Jamaica', |
||
342 | ]); |
||
343 | |||
344 | $connection->insert('person', [ |
||
345 | 'name' => 'John Smith', |
||
346 | 'created_at' => '2015-10-24 11:57:13', |
||
347 | ]); |
||
348 | $connection->insert('person', [ |
||
349 | 'name' => 'Jean Dupont', |
||
350 | 'created_at' => '2015-10-24 11:57:13', |
||
351 | ]); |
||
352 | $connection->insert('person', [ |
||
353 | 'name' => 'Robert Marley', |
||
354 | 'created_at' => '2015-10-24 11:57:13', |
||
355 | ]); |
||
356 | $connection->insert('person', [ |
||
357 | 'name' => 'Bill Shakespeare', |
||
358 | 'created_at' => '2015-10-24 11:57:13', |
||
359 | ]); |
||
360 | |||
361 | $connection->insert('contact', [ |
||
362 | 'id' => 1, |
||
363 | 'email' => '[email protected]', |
||
364 | 'manager_id' => null, |
||
365 | ]); |
||
366 | $connection->insert('contact', [ |
||
367 | 'id' => 2, |
||
368 | 'email' => '[email protected]', |
||
369 | 'manager_id' => null, |
||
370 | ]); |
||
371 | $connection->insert('contact', [ |
||
372 | 'id' => 3, |
||
373 | 'email' => '[email protected]', |
||
374 | 'manager_id' => null, |
||
375 | ]); |
||
376 | $connection->insert('contact', [ |
||
377 | 'id' => 4, |
||
378 | 'email' => '[email protected]', |
||
379 | 'manager_id' => 1, |
||
380 | ]); |
||
381 | |||
382 | $connection->insert('rights', [ |
||
383 | 'label' => 'CAN_SING', |
||
384 | ]); |
||
385 | $connection->insert('rights', [ |
||
386 | 'label' => 'CAN_WRITE', |
||
387 | ]); |
||
388 | |||
389 | $connection->insert('roles', [ |
||
390 | 'name' => 'Admins', |
||
391 | 'created_at' => '2015-10-24' |
||
392 | ]); |
||
393 | $connection->insert('roles', [ |
||
394 | 'name' => 'Writers', |
||
395 | 'created_at' => '2015-10-24' |
||
396 | ]); |
||
397 | $connection->insert('roles', [ |
||
398 | 'name' => 'Singers', |
||
399 | 'created_at' => '2015-10-24' |
||
400 | ]); |
||
401 | |||
402 | $connection->insert('roles_rights', [ |
||
403 | 'role_id' => 1, |
||
404 | 'right_label' => 'CAN_SING' |
||
405 | ]); |
||
406 | $connection->insert('roles_rights', [ |
||
407 | 'role_id' => 3, |
||
408 | 'right_label' => 'CAN_SING' |
||
409 | ]); |
||
410 | $connection->insert('roles_rights', [ |
||
411 | 'role_id' => 1, |
||
412 | 'right_label' => 'CAN_WRITE' |
||
413 | ]); |
||
414 | $connection->insert('roles_rights', [ |
||
415 | 'role_id' => 2, |
||
416 | 'right_label' => 'CAN_WRITE' |
||
417 | ]); |
||
418 | |||
419 | $connection->insert('users', [ |
||
420 | 'id' => 1, |
||
421 | 'login' => 'john.smith', |
||
422 | 'password' => null, |
||
423 | 'status' => 'on', |
||
424 | 'country_id' => 2 |
||
425 | ]); |
||
426 | $connection->insert('users', [ |
||
427 | 'id' => 2, |
||
428 | 'login' => 'jean.dupont', |
||
429 | 'password' => null, |
||
430 | 'status' => 'on', |
||
431 | 'country_id' => 1 |
||
432 | ]); |
||
433 | $connection->insert('users', [ |
||
434 | 'id' => 3, |
||
435 | 'login' => 'robert.marley', |
||
436 | 'password' => null, |
||
437 | 'status' => 'off', |
||
438 | 'country_id' => 3 |
||
439 | ]); |
||
440 | $connection->insert('users', [ |
||
441 | 'id' => 4, |
||
442 | 'login' => 'bill.shakespeare', |
||
443 | 'password' => null, |
||
444 | 'status' => 'off', |
||
445 | 'country_id' => 2 |
||
446 | ]); |
||
447 | |||
448 | $connection->insert('users_roles', [ |
||
449 | 'user_id' => 1, |
||
450 | 'role_id' => 1, |
||
451 | ]); |
||
452 | $connection->insert('users_roles', [ |
||
453 | 'user_id' => 2, |
||
454 | 'role_id' => 1, |
||
455 | ]); |
||
456 | $connection->insert('users_roles', [ |
||
457 | 'user_id' => 3, |
||
458 | 'role_id' => 3, |
||
459 | ]); |
||
460 | $connection->insert('users_roles', [ |
||
461 | 'user_id' => 4, |
||
462 | 'role_id' => 2, |
||
463 | ]); |
||
464 | $connection->insert('users_roles', [ |
||
465 | 'user_id' => 3, |
||
466 | 'role_id' => 2, |
||
467 | ]); |
||
468 | } |
||
469 | } |
||
470 |