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:
Complex classes like GenerateCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GenerateCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class GenerateCommand extends AbstractCommand |
||
| 15 | { |
||
| 16 | const DIR_CREATE_PERMISSIONS = 0755; |
||
| 17 | |||
| 18 | private $availableMigrationFormats = array('yml', 'php', 'sql', 'json'); |
||
| 19 | private $availableModes = array('create', 'update', 'delete'); |
||
| 20 | private $availableTypes = array('role', 'content', 'content_type', 'content_type_group', 'object_state_group', 'section', 'generic', 'db', 'php'); |
||
| 21 | private $thisBundle = 'EzMigrationBundle'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Configure the console command |
||
| 25 | */ |
||
| 26 | 69 | protected function configure() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Run the command and display the results. |
||
| 75 | * |
||
| 76 | * @param InputInterface $input |
||
| 77 | * @param OutputInterface $output |
||
| 78 | * @return null|int null or 0 if everything went fine, or an error code |
||
| 79 | * @throws \InvalidArgumentException When an unsupported file type is selected |
||
| 80 | */ |
||
| 81 | 31 | public function execute(InputInterface $input, OutputInterface $output) |
|
| 82 | { |
||
| 83 | 31 | $bundleName = $input->getArgument('bundle'); |
|
| 84 | 31 | $name = $input->getArgument('name'); |
|
| 85 | 31 | $fileType = $input->getOption('format'); |
|
| 86 | 31 | $migrationType = $input->getOption('type'); |
|
| 87 | 31 | $role = $input->getOption('role'); |
|
| 88 | 31 | $matchType = $input->getOption('match-type'); |
|
| 89 | 31 | $matchValue = $input->getOption('match-value'); |
|
| 90 | 31 | $matchExcept = $input->getOption('match-except'); |
|
| 91 | 31 | $mode = $input->getOption('mode'); |
|
| 92 | 31 | $dbServer = $input->getOption('dbserver'); |
|
| 93 | |||
| 94 | 31 | if ($role != '') { |
|
| 95 | $output->writeln('<error>The "role" option is deprecated since version 3.2 and will be removed in 4.0. Use "type=role", "match-type=identifier" and "match-value" instead.</error>'); |
||
| 96 | $migrationType = 'role'; |
||
| 97 | $matchType = 'identifier'; |
||
| 98 | $matchValue = $role; |
||
| 99 | if ($mode == '') { |
||
| 100 | $mode = 'update'; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | 31 | if ($bundleName == $this->thisBundle) { |
|
| 105 | throw new \InvalidArgumentException("It is not allowed to create migrations in bundle '$bundleName'"); |
||
| 106 | } |
||
| 107 | |||
| 108 | // be kind to lazy users |
||
| 109 | 31 | if ($migrationType == '') { |
|
| 110 | 6 | if ($fileType == 'sql') { |
|
| 111 | 2 | $migrationType = 'db'; |
|
| 112 | 4 | } elseif ($fileType == 'php') { |
|
| 113 | 2 | $migrationType = 'php'; |
|
| 114 | } else { |
||
| 115 | 2 | $migrationType = 'generic'; |
|
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | 31 | if (!in_array($fileType, $this->availableMigrationFormats)) { |
|
| 120 | throw new \InvalidArgumentException('Unsupported migration file format ' . $fileType); |
||
| 121 | } |
||
| 122 | |||
| 123 | 31 | if (!in_array($mode, $this->availableModes)) { |
|
| 124 | throw new \InvalidArgumentException('Unsupported migration mode ' . $mode); |
||
| 125 | } |
||
| 126 | |||
| 127 | 31 | $migrationDirectory = $this->getMigrationDirectory($bundleName); |
|
| 128 | |||
| 129 | if (!is_dir($migrationDirectory)) { |
||
| 130 | $output->writeln(sprintf( |
||
| 131 | "Migrations directory <info>%s</info> does not exist. I will create it now....", |
||
| 132 | $migrationDirectory |
||
| 133 | )); |
||
| 134 | |||
| 135 | if (mkdir($migrationDirectory, self::DIR_CREATE_PERMISSIONS, true)) { |
||
| 136 | $output->writeln(sprintf( |
||
| 137 | "Migrations directory <info>%s</info> has been created", |
||
| 138 | $migrationDirectory |
||
| 139 | )); |
||
| 140 | } else { |
||
| 141 | throw new FileException(sprintf( |
||
| 142 | "Failed to create migrations directory %s.", |
||
| 143 | $migrationDirectory |
||
| 144 | )); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // allow to generate migrations for many entities |
||
| 149 | if (strpos($matchValue, ',') !== false ) { |
||
| 150 | $matchValue = explode(',', $matchValue); |
||
| 151 | } |
||
| 152 | |||
| 153 | $parameters = array( |
||
| 154 | 'dbserver' => $dbServer, |
||
| 155 | 'matchType' => $matchType, |
||
| 156 | 'matchValue' => $matchValue, |
||
| 157 | 'matchExcept' => $matchExcept, |
||
| 158 | 'mode' => $mode, |
||
| 159 | 'lang' => $input->getOption('lang') |
||
| 160 | ); |
||
| 161 | |||
| 162 | $date = date('YmdHis'); |
||
| 163 | |||
| 164 | switch ($fileType) { |
||
| 165 | case 'sql': |
||
| 166 | /// @todo this logic should come from the DefinitionParser, really |
||
| 167 | if ($name != '') { |
||
| 168 | $name = '_' . ltrim($name, '_'); |
||
| 169 | } |
||
| 170 | $fileName = $date . '_' . $dbServer . $name . '.sql'; |
||
| 171 | break; |
||
| 172 | |||
| 173 | case 'php': |
||
| 174 | /// @todo this logic should come from the DefinitionParser, really |
||
| 175 | $className = ltrim($name, '_'); |
||
| 176 | if ($className == '') { |
||
| 177 | $className = 'Migration'; |
||
| 178 | } |
||
| 179 | // Make sure that php class names are unique, not only migration definition file names |
||
| 180 | $existingMigrations = count(glob($migrationDirectory . '/*_' . $className . '*.php')); |
||
| 181 | if ($existingMigrations) { |
||
| 182 | $className = $className . sprintf('%03d', $existingMigrations + 1); |
||
| 183 | } |
||
| 184 | $parameters = array_merge($parameters, array( |
||
| 185 | 'class_name' => $className |
||
| 186 | )); |
||
| 187 | $fileName = $date . '_' . $className . '.php'; |
||
| 188 | break; |
||
| 189 | |||
| 190 | default: |
||
| 191 | if ($name == '') { |
||
| 192 | $name = 'placeholder'; |
||
| 193 | } |
||
| 194 | $fileName = $date . '_' . $name . '.' . $fileType; |
||
| 195 | } |
||
| 196 | |||
| 197 | $path = $migrationDirectory . '/' . $fileName; |
||
| 198 | |||
| 199 | $warning = $this->generateMigrationFile($path, $fileType, $migrationType, $parameters); |
||
| 200 | |||
| 201 | $output->writeln(sprintf("Generated new migration file: <info>%s</info>", $path)); |
||
| 202 | |||
| 203 | if ($warning != '') { |
||
| 204 | $output->writeln("<comment>$warning</comment>"); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Generates a migration definition file. |
||
| 210 | * |
||
| 211 | * @param string $path filename to file to generate (full path) |
||
| 212 | * @param string $fileType The type of migration file to generate |
||
| 213 | * @param string $migrationType The type of migration to generate |
||
| 214 | * @param array $parameters passed on to twig |
||
| 215 | * @return string A warning message in case file generation was OK but there was something weird |
||
| 216 | * @throws \Exception |
||
| 217 | */ |
||
| 218 | protected function generateMigrationFile($path, $fileType, $migrationType, array $parameters = array()) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $bundleName a bundle name or filesystem path to a directory |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 31 | protected function getMigrationDirectory($bundleName) |
|
| 281 | { |
||
| 282 | // Allow direct usage of a directory path instead of a bundle name |
||
| 283 | 31 | if (strpos($bundleName, '/') !== false && is_dir($bundleName)) { |
|
| 284 | return rtrim($bundleName, '/'); |
||
| 285 | } |
||
| 286 | |||
| 287 | 31 | $activeBundles = array(); |
|
| 288 | 31 | foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { |
|
| 289 | 31 | $activeBundles[] = $bundle->getName(); |
|
| 290 | } |
||
| 291 | 31 | asort($activeBundles); |
|
| 292 | 31 | if (!in_array($bundleName, $activeBundles)) { |
|
| 293 | throw new \InvalidArgumentException("Bundle '$bundleName' does not exist or it is not enabled. Try with one of:\n" . implode(', ', $activeBundles)); |
||
| 294 | } |
||
| 295 | |||
| 296 | 31 | $bundle = $this->getApplication()->getKernel()->getBundle($bundleName); |
|
| 297 | 31 | $migrationDirectory = $bundle->getPath() . '/' . $this->getContainer()->get('ez_migration_bundle.helper.config.resolver')->getParameter('kaliop_bundle_migration.version_directory'); |
|
| 298 | |||
| 299 | return $migrationDirectory; |
||
| 300 | } |
||
| 301 | |||
| 302 | /// @todo move somewhere else. Maybe to the MigrationService itself ? |
||
| 303 | View Code Duplication | protected function getGeneratingExecutors() |
|
| 315 | } |
||
| 316 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: