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