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 CreateCommand extends AbstractCommand |
||
31 | { |
||
32 | private $createOrder = array(self::DB, self::COLLECTION, self::INDEX); |
||
33 | |||
34 | private $timeout; |
||
35 | |||
36 | protected function configure() |
||
37 | { |
||
38 | $this |
||
39 | ->setName('odm:schema:create') |
||
40 | ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
||
41 | ->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation') |
||
42 | ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Create databases') |
||
43 | ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections') |
||
44 | ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes') |
||
45 | ->setDescription('Create databases, collections and indexes for your documents') |
||
46 | ; |
||
47 | } |
||
48 | |||
49 | protected function execute(InputInterface $input, OutputInterface $output) |
||
50 | { |
||
51 | foreach ($this->createOrder as $option) { |
||
52 | if ($input->getOption($option)) { |
||
53 | $create[] = $option; |
||
|
|||
54 | } |
||
55 | } |
||
56 | |||
57 | // Default to the full creation order if no options were specified |
||
58 | $create = empty($create) ? $this->createOrder : $create; |
||
59 | |||
60 | $class = $input->getOption('class'); |
||
61 | |||
62 | $timeout = $input->getOption('timeout'); |
||
63 | $this->timeout = isset($timeout) ? (int) $timeout : null; |
||
64 | |||
65 | $sm = $this->getSchemaManager(); |
||
66 | $isErrored = false; |
||
67 | |||
68 | View Code Duplication | foreach ($create as $option) { |
|
69 | try { |
||
70 | if (isset($class)) { |
||
71 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
72 | } else { |
||
73 | $this->{'process' . ucfirst($option)}($sm); |
||
74 | } |
||
75 | $output->writeln(sprintf( |
||
76 | 'Created <comment>%s%s</comment> for <info>%s</info>', |
||
77 | $option, |
||
78 | (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
||
79 | (isset($class) ? $class : 'all classes') |
||
80 | )); |
||
81 | } catch (\Exception $e) { |
||
82 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
83 | $isErrored = true; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | return ($isErrored) ? 255 : 0; |
||
88 | } |
||
89 | |||
90 | protected function processDocumentCollection(SchemaManager $sm, $document) |
||
94 | |||
95 | protected function processCollection(SchemaManager $sm) |
||
99 | |||
100 | protected function processDocumentDb(SchemaManager $sm, $document) |
||
104 | |||
105 | protected function processDb(SchemaManager $sm) |
||
109 | |||
110 | protected function processDocumentIndex(SchemaManager $sm, $document) |
||
114 | |||
115 | protected function processIndex(SchemaManager $sm) |
||
119 | |||
120 | protected function processDocumentProxy(SchemaManager $sm, $document) |
||
124 | |||
125 | protected function processProxy(SchemaManager $sm) |
||
129 | } |
||
130 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.