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 |
||
25 | class CreateCardCommand extends AbstractCommand |
||
26 | { |
||
27 | /** |
||
28 | * An instance of card entity |
||
29 | * |
||
30 | * @var Entity\Card |
||
31 | */ |
||
32 | protected $entity; |
||
33 | |||
34 | /** |
||
35 | * Get a card entity |
||
36 | * |
||
37 | * @return Entity\Card |
||
38 | */ |
||
39 | 6 | protected function getEntity() |
|
40 | { |
||
41 | 6 | if (null === $this->entity) { |
|
42 | 6 | $this->entity = new Entity\Card(); |
|
43 | } |
||
44 | |||
45 | 6 | return $this->entity; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Command configuration |
||
50 | * Create a new card |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | 9 | protected function configure() |
|
55 | { |
||
56 | $this |
||
57 | 9 | ->setName('flashcard:card:create') |
|
58 | 9 | ->setDescription('Create a card') |
|
59 | 9 | ->addArgument('title', InputArgument::REQUIRED, 'The title of the card.') |
|
60 | 9 | ->addArgument('content', InputArgument::REQUIRED, 'The content of the card.') |
|
61 | 9 | ->addArgument('category', InputArgument::REQUIRED, 'The category ID the card is belong to.') |
|
62 | 9 | ->addArgument('keywords', InputArgument::OPTIONAL, 'Comma seperated keywords for the metadata tag.', null) |
|
63 | 9 | ->addArgument('description', InputArgument::OPTIONAL, 'The metadata description.', null) |
|
64 | 9 | ->addArgument('slug', InputArgument::OPTIONAL, 'The url slug of the card.', null) |
|
65 | 9 | ->addOption('active', null, InputOption::VALUE_NONE, 'If set, the card is going to be active.'); |
|
66 | 9 | } |
|
67 | |||
68 | /** |
||
69 | * Enable interaction |
||
70 | * |
||
71 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
72 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
73 | */ |
||
74 | 4 | protected function interact(InputInterface $input, OutputInterface $output) |
|
75 | { |
||
76 | 4 | View Code Duplication | if (!$input->getArgument('title')) { |
|
|||
77 | 4 | $value = $this->getHelper('dialog')->askAndValidate( |
|
78 | $output, |
||
79 | 4 | 'Please enter (Title): ', |
|
80 | 4 | [$this, 'validateTitle'], |
|
81 | 4 | 1 |
|
82 | ); |
||
83 | 3 | $input->setArgument('title', $value); |
|
84 | } |
||
85 | |||
86 | 3 | View Code Duplication | if (!$input->getArgument('content')) { |
87 | 3 | $value = $this->getHelper('dialog')->askAndValidate( |
|
88 | $output, |
||
89 | 3 | 'Please enter (Content): ', |
|
90 | 3 | [$this, 'validateContent'], |
|
91 | 3 | 1 |
|
92 | ); |
||
93 | 2 | $input->setArgument('content', $value); |
|
94 | } |
||
95 | |||
96 | 2 | View Code Duplication | if (!$input->getArgument('category')) { |
97 | 2 | $value = $this->getHelper('dialog')->askAndValidate( |
|
98 | $output, |
||
99 | 2 | 'Please enter (Category ID): ', |
|
100 | 2 | [$this, 'validateCategory'], |
|
101 | 2 | 1 |
|
102 | ); |
||
103 | 1 | $input->setArgument('category', $value); |
|
104 | } |
||
105 | 1 | } |
|
106 | |||
107 | /** |
||
108 | * Validate the card title |
||
109 | * |
||
110 | * @param string $value |
||
111 | * |
||
112 | * @return string |
||
113 | * |
||
114 | * @throws \Exception |
||
115 | */ |
||
116 | 4 | View Code Duplication | public function validateTitle($value) |
117 | { |
||
118 | 4 | $this->getEntity()->setTitle($value); |
|
119 | |||
120 | 4 | $error = $this->validate($this->entity, 'title'); |
|
121 | 4 | if ($error !== true) { |
|
122 | 1 | throw new \InvalidArgumentException($error); |
|
123 | } |
||
124 | |||
125 | 3 | return $value; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Validate the card content |
||
130 | * |
||
131 | * @param string $value |
||
132 | * |
||
133 | * @return string |
||
134 | * |
||
135 | * @throws \Exception |
||
136 | */ |
||
137 | 3 | View Code Duplication | public function validateContent($value) |
148 | |||
149 | /** |
||
150 | * Validate the card category |
||
151 | * |
||
152 | * @param int $value |
||
153 | * |
||
154 | * @return Entity\Category |
||
155 | * |
||
156 | * @throws \Exception |
||
157 | */ |
||
158 | 2 | public function validateCategory($value) |
|
174 | |||
175 | /** |
||
176 | * Execute the command line to create a new card. |
||
177 | * |
||
178 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
179 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 3 | protected function execute(InputInterface $input, OutputInterface $output) |
|
220 | } |
||
221 |
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.