Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class schema extends Command |
||
| 29 | { |
||
| 30 | public $connected = false; |
||
| 31 | |||
| 32 | protected function configure() |
||
| 33 | { |
||
| 34 | $this->setName('schema') |
||
| 35 | ->setDescription('(Re)generate mapping information from MgdSchema XMLs') |
||
| 36 | ->addArgument('config', InputArgument::OPTIONAL, 'Full path to midgard-portable config file') |
||
| 37 | ->addOption('force', null, InputOption::VALUE_NONE, 'Ignore errors from DB'); |
||
| 38 | } |
||
| 39 | |||
| 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 | |||
| 118 | View Code Duplication | private function generate_proxyfiles(array $cms) |
|
| 132 | |||
| 133 | private function process_updates(array $to_update, OutputInterface $output, $force) |
||
| 177 | } |
||
| 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.