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 |
||
35 | class QueueBuildCommand extends Command implements ContainerAwareInterface |
||
36 | { |
||
37 | /** |
||
38 | * @var ContainerInterface |
||
39 | * |
||
40 | * @api |
||
41 | */ |
||
42 | protected $container; |
||
43 | |||
44 | /** |
||
45 | * Sets the Container associated with this Controller. |
||
46 | * |
||
47 | * @param ContainerInterface $container A ContainerInterface instance |
||
48 | * |
||
49 | * @api |
||
50 | */ |
||
51 | public function setContainer(ContainerInterface $container = null) |
||
52 | { |
||
53 | $this->container = $container; |
||
54 | } |
||
55 | |||
56 | protected $output; |
||
57 | |||
58 | protected function configure() |
||
59 | { |
||
60 | $this |
||
61 | ->setName('uecode:qpush:build') |
||
62 | ->setDescription('Builds the configured Queues') |
||
63 | ->addArgument( |
||
64 | 'name', |
||
65 | InputArgument::OPTIONAL, |
||
66 | 'Name of a specific queue to build', |
||
67 | null |
||
68 | ) |
||
69 | ; |
||
70 | } |
||
71 | |||
72 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|||
73 | { |
||
74 | $this->output = $output; |
||
75 | $registry = $this->container->get('uecode_qpush'); |
||
76 | |||
77 | $name = $input->getArgument('name'); |
||
78 | |||
79 | if (null !== $name) { |
||
80 | return $this->buildQueue($registry, $name); |
||
81 | } |
||
82 | |||
83 | foreach ($registry->all() as $queue) { |
||
84 | $this->buildQueue($registry, $queue->getName()); |
||
85 | } |
||
86 | |||
87 | return 0; |
||
88 | } |
||
89 | |||
90 | private function buildQueue($registry, $name) |
||
103 | } |
||
104 |
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.