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