| Conditions | 17 |
| Paths | 359 |
| Total Lines | 77 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 40 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 41 | { |
||
| 42 | if (!$this->connected) { |
||
| 43 | $path = $input->getArgument('config'); |
||
| 44 | if (empty($path)) { |
||
| 45 | if (file_exists(OPENPSA_PROJECT_BASEDIR . 'config/midgard-portable.inc.php')) { |
||
| 46 | $path = OPENPSA_PROJECT_BASEDIR . 'config/midgard-portable.inc.php'; |
||
| 47 | } else { |
||
| 48 | $dialog = $this->getHelper('question'); |
||
| 49 | $path = $dialog->ask($input, $output, new Question('<question>Enter path to config file</question>')); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | if (!file_exists($path)) { |
||
| 53 | throw new \RuntimeException('Config file ' . $path . ' not found'); |
||
| 54 | } |
||
| 55 | //we have to delay startup so that we can delete the entity class file before it gets included |
||
| 56 | connection::set_autostart(false); |
||
| 57 | require $path; |
||
| 58 | } |
||
| 59 | |||
| 60 | $mgd_config = midgard_connection::get_instance()->config; |
||
| 61 | $mgdschema_file = $mgd_config->vardir . '/mgdschema_classes.php'; |
||
| 62 | if ( file_exists($mgdschema_file) |
||
| 63 | && !unlink($mgdschema_file)) { |
||
| 64 | throw new \RuntimeException('Could not unlink ' . $mgdschema_file); |
||
| 65 | } |
||
| 66 | if (connection::get_parameter('dev_mode') !== true) { |
||
| 67 | $driver = connection::get_parameter('driver'); |
||
| 68 | $classgenerator = new classgenerator($driver->get_manager(), $mgdschema_file); |
||
| 69 | $classgenerator->write($driver->get_namespace()); |
||
| 70 | } |
||
| 71 | if (!file_exists($mgd_config->blobdir . '/0/0')) { |
||
| 72 | $mgd_config->create_blobdir(); |
||
| 73 | } |
||
| 74 | connection::startup(); |
||
| 75 | $em = connection::get_em(); |
||
| 76 | connection::invalidate_cache(); |
||
| 77 | $cms = $em->getMetadataFactory()->getAllMetadata(); |
||
| 78 | |||
| 79 | // create storage |
||
| 80 | if ( !midgard_storage::create_base_storage() |
||
| 81 | && midgard_connection::get_instance()->get_error_string() != 'MGD_ERR_OK') { |
||
| 82 | throw new \Exception("Failed to create base database structures" . midgard_connection::get_instance()->get_error_string()); |
||
| 83 | } |
||
| 84 | $force = $input->getOption('force'); |
||
| 85 | $to_update = array(); |
||
| 86 | $to_create = array(); |
||
| 87 | |||
| 88 | foreach ($cms as $cm) { |
||
| 89 | if (!$em->getConnection()->getSchemaManager()->tablesExist(array($cm->getTableName()))) { |
||
| 90 | $to_create[] = $cm; |
||
| 91 | } else { |
||
| 92 | $to_update[] = $cm; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!empty($to_create)) { |
||
| 97 | $output->writeln('Creating <info>' . count($to_create) . '</info> new tables'); |
||
| 98 | $tool = new SchemaTool($em); |
||
| 99 | try { |
||
| 100 | $tool->createSchema($to_create); |
||
| 101 | } catch (\Exception $e) { |
||
| 102 | if (!$force) { |
||
| 103 | throw $e; |
||
| 104 | } else { |
||
| 105 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | if (!empty($to_update)) { |
||
| 110 | $this->process_updates($to_update, $output, $force); |
||
| 111 | } |
||
| 112 | $output->writeln('Generating proxies'); |
||
| 113 | $this->generate_proxyfiles($cms); |
||
| 114 | |||
| 115 | $output->writeln('Done'); |
||
| 116 | } |
||
| 117 | |||
| 178 |
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.