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