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 |
||
26 | class IndexCreatorCommand extends Command |
||
27 | { |
||
28 | /** @var string */ |
||
29 | protected $name = 'couchbase:create-index'; |
||
30 | |||
31 | /** @var string */ |
||
32 | protected $description = 'Create a secondary index for the current bucket.'; |
||
33 | |||
34 | /** @var DatabaseManager */ |
||
35 | protected $databaseManager; |
||
36 | |||
37 | /** @var string */ |
||
38 | protected $defaultDatabase = 'couchbase'; |
||
39 | |||
40 | /** |
||
41 | * IndexFinderCommand constructor. |
||
42 | * |
||
43 | * @param DatabaseManager $databaseManager |
||
44 | */ |
||
45 | 1 | public function __construct(DatabaseManager $databaseManager) |
|
50 | |||
51 | /** |
||
52 | * @return string[] |
||
53 | */ |
||
54 | 1 | protected function getArguments() |
|
62 | |||
63 | /** |
||
64 | * Get the console command options. |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | 1 | View Code Duplication | protected function getOptions() |
|
|||
69 | { |
||
70 | return [ |
||
71 | 1 | ['database', 'db', InputOption::VALUE_REQUIRED, 'The database connection to use.', $this->defaultDatabase], |
|
72 | [ |
||
73 | 1 | 'where', |
|
74 | null, |
||
75 | 1 | InputOption::VALUE_REQUIRED, |
|
76 | 1 | 'the WHERE clause of the index.', |
|
77 | 1 | '', |
|
78 | ], |
||
79 | [ |
||
80 | 1 | 'ignore', |
|
81 | 1 | 'ig', |
|
82 | 1 | InputOption::VALUE_NONE, |
|
83 | 1 | 'if a primary index already exists, an exception will be thrown unless this is set to true.', |
|
84 | ], |
||
85 | [ |
||
86 | 1 | 'defer', |
|
87 | 1 | 'd', |
|
88 | 1 | InputOption::VALUE_NONE, |
|
89 | 1 | 'true to defer building of the index until buildN1qlDeferredIndexes()}is called (or a direct call to the corresponding query service API)', |
|
90 | ], |
||
91 | ]; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Execute the console command |
||
96 | */ |
||
97 | 1 | public function fire() |
|
122 | } |
||
123 |
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.