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