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 |
||
37 | class QueueDestroyCommand extends Command implements ContainerAwareInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var ContainerInterface |
||
41 | * |
||
42 | * @api |
||
43 | */ |
||
44 | protected $container; |
||
45 | |||
46 | /** |
||
47 | * Sets the Container associated with this Controller. |
||
48 | * |
||
49 | * @param ContainerInterface $container A ContainerInterface instance |
||
50 | * |
||
51 | * @api |
||
52 | */ |
||
53 | public function setContainer(ContainerInterface $container = null) |
||
54 | { |
||
55 | $this->container = $container; |
||
56 | } |
||
57 | |||
58 | protected $output; |
||
59 | |||
60 | protected function configure() |
||
61 | { |
||
62 | $this |
||
63 | ->setName('uecode:qpush:destroy') |
||
64 | ->setDescription('Destroys the configured Queues and cleans Cache') |
||
65 | ->addArgument( |
||
66 | 'name', |
||
67 | InputArgument::OPTIONAL, |
||
68 | 'Name of a specific queue to destroy', |
||
69 | null |
||
70 | ) |
||
71 | ->addOption( |
||
72 | 'force', |
||
73 | null, |
||
74 | InputOption::VALUE_NONE, |
||
75 | 'Set this parameter to force this action' |
||
76 | ) |
||
77 | ; |
||
78 | } |
||
79 | |||
80 | protected function execute(InputInterface $input, OutputInterface $output) |
||
81 | { |
||
82 | $this->output = $output; |
||
83 | $registry = $this->container->get('uecode_qpush'); |
||
84 | $questionHelper = $this->getHelperSet()->get('question'); |
||
85 | $name = $input->getArgument('name'); |
||
86 | |||
87 | if (null !== $name) { |
||
88 | View Code Duplication | if (!$input->getOption('force')) { |
|
|
|||
89 | $question = new ConfirmationQuestion(sprintf( |
||
90 | '<comment>This will remove the %s queue, even if it has messages! Are you sure? </comment>', |
||
91 | $name |
||
92 | )); |
||
93 | $confirmation = $questionHelper->ask($input, $output, $question); |
||
94 | |||
95 | if (!$confirmation) { |
||
96 | return 0; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return $this->destroyQueue($registry, $name); |
||
101 | } |
||
102 | |||
103 | View Code Duplication | if (!$input->getOption('force')) { |
|
104 | $question = new ConfirmationQuestion('<comment>This will remove ALL queues, even if they have messages. Are you sure? </comment>'); |
||
105 | $confirmation = $questionHelper->ask($input, $output, $question); |
||
106 | |||
107 | if (!$confirmation) { |
||
108 | return 0; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | foreach ($registry->all() as $queue) { |
||
113 | $this->destroyQueue($registry, $queue->getName()); |
||
114 | } |
||
115 | |||
116 | return 0; |
||
117 | } |
||
118 | |||
119 | private function destroyQueue($registry, $name) |
||
132 | } |
||
133 |
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.