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