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 QueueCreatorCommand extends Command |
||
29 | { |
||
30 | /** @var string */ |
||
31 | protected $name = 'couchbase:create-queue-index'; |
||
32 | |||
33 | /** @var string */ |
||
34 | protected $description = 'Create primary index, secondary indexes for the queue jobs couchbase bucket.'; |
||
35 | |||
36 | /** @var DatabaseManager */ |
||
37 | protected $databaseManager; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $defaultDatabase = 'couchbase'; |
||
41 | |||
42 | const PRIMARY_KEY = '#job_queue_primary'; |
||
43 | |||
44 | /** @var string[] */ |
||
45 | protected $secondaryIndexes = [ |
||
46 | 'idx_job_queue' => [ // index name |
||
47 | 'queue', // fields |
||
48 | ], |
||
49 | 'idx_job_identifier' => [ |
||
50 | 'id', |
||
51 | ], |
||
52 | 'idx_job_queue_cover' => [ |
||
53 | 'queue', |
||
54 | 'reserved', |
||
55 | 'reserved_at', |
||
56 | 'available_at', |
||
57 | 'id', |
||
58 | ], |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * IndexFinderCommand constructor. |
||
63 | * |
||
64 | * @param DatabaseManager $databaseManager |
||
65 | */ |
||
66 | public function __construct(DatabaseManager $databaseManager) |
||
71 | |||
72 | /** |
||
73 | * @return string[] |
||
74 | */ |
||
75 | protected function getArguments() |
||
81 | |||
82 | /** |
||
83 | * Get the console command options. |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | View Code Duplication | protected function getOptions() |
|
105 | |||
106 | /** |
||
107 | * Execute the console command |
||
108 | */ |
||
109 | public function fire() |
||
145 | } |
||
146 |
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.