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 |
||
| 30 | class DropCommand extends AbstractCommand |
||
| 31 | { |
||
| 32 | private $dropOrder = array(self::INDEX, self::COLLECTION, self::DB); |
||
| 33 | |||
| 34 | protected function configure() |
||
| 35 | { |
||
| 36 | $this |
||
| 37 | ->setName('odm:schema:drop') |
||
| 38 | ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
||
| 39 | ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases') |
||
| 40 | ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections') |
||
| 41 | ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes') |
||
| 42 | ->setDescription('Drop databases, collections and indexes for your documents') |
||
| 43 | ; |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 47 | { |
||
| 48 | foreach ($this->dropOrder as $option) { |
||
| 49 | if ($input->getOption($option)) { |
||
| 50 | $drop[] = $option; |
||
|
|
|||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | // Default to the full drop order if no options were specified |
||
| 55 | $drop = empty($drop) ? $this->dropOrder : $drop; |
||
| 56 | |||
| 57 | $class = $input->getOption('class'); |
||
| 58 | $sm = $this->getSchemaManager(); |
||
| 59 | $isErrored = false; |
||
| 60 | |||
| 61 | View Code Duplication | foreach ($drop as $option) { |
|
| 62 | try { |
||
| 63 | if (isset($class)) { |
||
| 64 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
| 65 | } else { |
||
| 66 | $this->{'process' . ucfirst($option)}($sm); |
||
| 67 | } |
||
| 68 | $output->writeln(sprintf( |
||
| 69 | 'Dropped <comment>%s%s</comment> for <info>%s</info>', |
||
| 70 | $option, |
||
| 71 | (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
||
| 72 | (isset($class) ? $class : 'all classes') |
||
| 73 | )); |
||
| 74 | } catch (\Exception $e) { |
||
| 75 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
| 76 | $isErrored = true; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | return ($isErrored) ? 255 : 0; |
||
| 81 | } |
||
| 82 | |||
| 83 | protected function processDocumentCollection(SchemaManager $sm, $document) |
||
| 87 | |||
| 88 | protected function processCollection(SchemaManager $sm) |
||
| 92 | |||
| 93 | protected function processDocumentDb(SchemaManager $sm, $document) |
||
| 97 | |||
| 98 | protected function processDb(SchemaManager $sm) |
||
| 102 | |||
| 103 | protected function processDocumentIndex(SchemaManager $sm, $document) |
||
| 107 | |||
| 108 | protected function processIndex(SchemaManager $sm) |
||
| 112 | } |
||
| 113 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.