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 |
||
46 | abstract class AbstractSeed implements SeedInterface |
||
47 | { |
||
48 | /** |
||
49 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
50 | */ |
||
51 | protected $adapter; |
||
52 | |||
53 | /** |
||
54 | * @var \Symfony\Component\Console\Input\InputInterface |
||
55 | */ |
||
56 | protected $input; |
||
57 | |||
58 | /** |
||
59 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
60 | */ |
||
61 | protected $output; |
||
62 | |||
63 | /** |
||
64 | * Class Constructor. |
||
65 | * |
||
66 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
67 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
68 | */ |
||
69 | 11 | View Code Duplication | final public function __construct(InputInterface $input = null, OutputInterface $output = null) |
80 | |||
81 | /** |
||
82 | * Initialize method. |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | 11 | protected function init() |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function run() |
||
96 | |||
97 | /** |
||
98 | * Return seeds dependencies. |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getDependencies() |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function setAdapter(AdapterInterface $adapter) |
||
116 | |||
117 | 11 | /** |
|
118 | * {@inheritdoc} |
||
119 | 11 | */ |
|
120 | 11 | public function getAdapter() |
|
124 | |||
125 | /** |
||
126 | 1 | * {@inheritdoc} |
|
127 | */ |
||
128 | 1 | public function setInput(InputInterface $input) |
|
129 | { |
||
130 | $this->input = $input; |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | 11 | ||
135 | /** |
||
136 | 11 | * {@inheritdoc} |
|
137 | 11 | */ |
|
138 | public function getInput() |
||
139 | { |
||
140 | return $this->input; |
||
141 | } |
||
142 | |||
143 | 1 | /** |
|
144 | * {@inheritdoc} |
||
145 | 1 | */ |
|
146 | public function setOutput(OutputInterface $output) |
||
152 | |||
153 | 6 | /** |
|
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function getOutput() |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function getName() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function execute($sql) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function query($sql) |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function fetchRow($sql) |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function fetchAll($sql) |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | View Code Duplication | public function insert($table, $data) |
|
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function hasTable($tableName) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function table($tableName, $options = []) |
||
228 | } |
||
229 |
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.