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 |
||
20 | class ProcessEventCommand extends ContainerAwareCommand |
||
21 | { |
||
22 | /** |
||
23 | * @var EventRepository |
||
24 | */ |
||
25 | private $eventRepository; |
||
26 | |||
27 | /** |
||
28 | * @var EventProcessor |
||
29 | */ |
||
30 | private $eventProcessor; |
||
31 | |||
32 | /** |
||
33 | * @var LockHandlerInterface |
||
34 | */ |
||
35 | private $lockHandler; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $channels; |
||
41 | |||
42 | /** |
||
43 | * @param EventRepository $eventRepository |
||
44 | * @param EventProcessor $eventProcessor |
||
45 | * @param LockHandlerInterface $lockHandler |
||
46 | * @param array $channels |
||
47 | * @param null|string $name |
||
48 | */ |
||
49 | public function __construct( |
||
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | View Code Duplication | protected function configure() |
|
|
|||
69 | { |
||
70 | $this |
||
71 | ->setName('itkg_delay_event:process') |
||
72 | ->setDescription('Process async events') |
||
73 | ->addOption( |
||
74 | 'channel', |
||
75 | 'c', |
||
76 | InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
||
77 | 'Specify the channels to process (default: [\'default\'])', |
||
78 | ['default'] |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param InputInterface $input |
||
84 | * @param OutputInterface $output |
||
85 | * |
||
86 | * @throws LockException |
||
87 | * @throws \Exception |
||
88 | * |
||
89 | * @return int|null|void |
||
90 | */ |
||
91 | protected function execute(InputInterface $input, OutputInterface $output) |
||
138 | } |
||
139 |
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.