| Conditions | 8 |
| Paths | 120 |
| Total Lines | 76 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 43 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 44 | { |
||
| 45 | $app = $this->app; |
||
| 46 | |||
| 47 | $removeExistingData = $input->getOption('remove-existing-data'); |
||
| 48 | |||
| 49 | if ($removeExistingData) { |
||
| 50 | try { |
||
| 51 | $app['db']->query('SET foreign_key_checks = 0;'); |
||
| 52 | |||
| 53 | $tables = $app['db']->getSchemaManager()->listTables(); |
||
| 54 | |||
| 55 | foreach ($tables as $table) { |
||
| 56 | $table = $table->getName(); |
||
| 57 | |||
| 58 | $app['db']->query('TRUNCATE TABLE '.$table.';'); |
||
| 59 | } |
||
| 60 | |||
| 61 | $app['db']->query('SET foreign_key_checks = 1;'); |
||
| 62 | |||
| 63 | $output->writeln('<info>All tables were successfully truncated!</info>'); |
||
| 64 | } catch (\Exception $e) { |
||
| 65 | $output->writeln('<error>'.$e->getMessage().'</error>'); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /***** Users *****/ |
||
| 70 | $users = include APP_DIR.'/fixtures/users.php'; |
||
| 71 | |||
| 72 | foreach ($users as $user) { |
||
| 73 | $userEntity = new UserEntity(); |
||
| 74 | $profileEntity = new ProfileEntity(); |
||
| 75 | |||
| 76 | // Profile |
||
| 77 | $profileEntity |
||
| 78 | ->setFirstName($user['profile']['firstName']) |
||
| 79 | ->setLastName($user['profile']['lastName']) |
||
| 80 | ; |
||
| 81 | |||
| 82 | if (isset($user['profile']['gender'])) { |
||
| 83 | $profileEntity |
||
| 84 | ->setGender($user['profile']['gender']) |
||
| 85 | ; |
||
| 86 | } |
||
| 87 | |||
| 88 | if (isset($user['profile']['birthdate'])) { |
||
| 89 | $profileEntity |
||
| 90 | ->setBirthdate($user['profile']['birthdate']) |
||
| 91 | ; |
||
| 92 | } |
||
| 93 | |||
| 94 | // User |
||
| 95 | $userEntity |
||
| 96 | ->setId($user['id']) |
||
| 97 | ->setUsername($user['username']) |
||
| 98 | ->setEmail($user['email']) |
||
| 99 | ->setPlainPassword( |
||
| 100 | $user['plainPassword'], |
||
| 101 | $app['security.encoder_factory'] |
||
| 102 | ) |
||
| 103 | ->setRoles($user['roles']) |
||
| 104 | ->setProfile($profileEntity) |
||
| 105 | ->enable() |
||
| 106 | ; |
||
| 107 | |||
| 108 | $app['orm.em']->persist($userEntity); |
||
| 109 | } |
||
| 110 | |||
| 111 | try { |
||
| 112 | $app['orm.em']->flush(); |
||
| 113 | |||
| 114 | $output->writeln('<info>Data was successfully hydrated!</info>'); |
||
| 115 | } catch (\Exception $e) { |
||
| 116 | $output->writeln('<error>'.$e->getMessage().'</error>'); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 |