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 |
||
47 | abstract class AbstractMigration implements MigrationInterface |
||
48 | { |
||
49 | /** |
||
50 | * @var float |
||
51 | */ |
||
52 | protected $version; |
||
53 | |||
54 | /** |
||
55 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
56 | */ |
||
57 | protected $adapter; |
||
58 | |||
59 | /** |
||
60 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
61 | */ |
||
62 | protected $output; |
||
63 | |||
64 | /** |
||
65 | * @var \Symfony\Component\Console\Input\InputInterface |
||
66 | */ |
||
67 | protected $input; |
||
68 | |||
69 | /** |
||
70 | * @var Environment |
||
71 | */ |
||
72 | protected $environment; |
||
73 | |||
74 | /** |
||
75 | * Whether this migration is being applied or reverted |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $isMigratingUp = true; |
||
80 | |||
81 | /** |
||
82 | 406 | * Class Constructor. |
|
83 | * |
||
84 | 406 | * @param int $version Migration Version |
|
85 | 406 | * @param \Symfony\Component\Console\Input\InputInterface|null $input |
|
86 | 384 | * @param \Symfony\Component\Console\Output\OutputInterface|null $output |
|
87 | 384 | * @param Environment|null $environment |
|
88 | 406 | */ |
|
89 | 384 | final public function __construct($version, InputInterface $input = null, OutputInterface $output = null, Environment $environment = null) |
|
104 | |||
105 | /** |
||
106 | * Initialize method. |
||
107 | 1 | * |
|
108 | * @return void |
||
109 | 1 | */ |
|
110 | protected function init() |
||
113 | |||
114 | 1 | /** |
|
115 | * {@inheritdoc} |
||
116 | 1 | */ |
|
117 | public function up() |
||
120 | |||
121 | 21 | /** |
|
122 | * {@inheritdoc} |
||
123 | 21 | */ |
|
124 | 21 | public function down() |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | 13 | */ |
|
131 | public function setAdapter(AdapterInterface $adapter) |
||
137 | |||
138 | 384 | /** |
|
139 | * {@inheritdoc} |
||
140 | 384 | */ |
|
141 | 384 | public function getAdapter() |
|
145 | |||
146 | /** |
||
147 | 2 | * {@inheritdoc} |
|
148 | */ |
||
149 | 2 | public function setEnvironment(Environment $environment) |
|
154 | |||
155 | 385 | /** |
|
156 | * {@inheritdoc} |
||
157 | 385 | */ |
|
158 | 385 | public function getEnvironment() |
|
162 | |||
163 | /** |
||
164 | 3 | * {@inheritdoc} |
|
165 | */ |
||
166 | 3 | public function setInput(InputInterface $input) |
|
167 | { |
||
168 | $this->input = $input; |
||
169 | |||
170 | return $this; |
||
171 | } |
||
172 | 140 | ||
173 | /** |
||
174 | 140 | * {@inheritdoc} |
|
175 | */ |
||
176 | public function getInput() |
||
177 | { |
||
178 | return $this->input; |
||
179 | } |
||
180 | 1 | ||
181 | /** |
||
182 | 1 | * {@inheritdoc} |
|
183 | 1 | */ |
|
184 | public function setOutput(OutputInterface $output) |
||
190 | |||
191 | 279 | /** |
|
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function getOutput() |
||
198 | |||
199 | 10 | /** |
|
200 | 10 | * {@inheritdoc} |
|
201 | */ |
||
202 | public function getName() |
||
206 | 2 | ||
207 | /** |
||
208 | 2 | * {@inheritdoc} |
|
209 | */ |
||
210 | public function setVersion($version) |
||
216 | 3 | ||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function getVersion() |
||
224 | 1 | ||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function setMigratingUp($isMigratingUp) |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | 1 | public function isMigratingUp() |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | 3 | public function execute($sql) |
|
250 | 2 | ||
251 | 2 | /** |
|
252 | 3 | * {@inheritdoc} |
|
253 | 3 | */ |
|
254 | public function query($sql) |
||
258 | 1 | ||
259 | /** |
||
260 | 1 | * {@inheritdoc} |
|
261 | 1 | */ |
|
262 | public function fetchRow($sql) |
||
266 | 1 | ||
267 | /** |
||
268 | 1 | * {@inheritdoc} |
|
269 | 1 | */ |
|
270 | public function fetchAll($sql) |
||
274 | 1 | ||
275 | /** |
||
276 | 1 | * {@inheritdoc} |
|
277 | */ |
||
278 | View Code Duplication | public function insert($table, $data) |
|
279 | { |
||
280 | // convert to table object |
||
281 | if (is_string($table)) { |
||
282 | 5 | $table = new Table($table, [], $this->getAdapter()); |
|
283 | } |
||
284 | 5 | $table->insert($data)->save(); |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | public function createDatabase($name, $options) |
||
294 | |||
295 | 1 | /** |
|
296 | 1 | * {@inheritdoc} |
|
297 | */ |
||
298 | public function dropDatabase($name) |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function hasTable($tableName) |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function table($tableName, $options = []) |
||
318 | |||
319 | /** |
||
320 | * A short-hand method to drop the given database table. |
||
321 | * |
||
322 | * @param string $tableName Table Name |
||
323 | * @return void |
||
324 | */ |
||
325 | public function dropTable($tableName) |
||
329 | } |
||
330 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.