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 namespace Xethron\MigrationsGenerator; |
||
20 | class MigrateGenerateCommand extends GeneratorCommand { |
||
21 | |||
22 | /** |
||
23 | * The console command name. |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $name = 'migrate:generate'; |
||
27 | |||
28 | /** |
||
29 | * The console command description. |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $description = 'Generate a migration from an existing table structure.'; |
||
33 | |||
34 | /** |
||
35 | * @var \Way\Generators\Filesystem\Filesystem |
||
36 | */ |
||
37 | protected $file; |
||
38 | |||
39 | /** |
||
40 | * @var \Way\Generators\Compilers\TemplateCompiler |
||
41 | */ |
||
42 | protected $compiler; |
||
43 | |||
44 | /** |
||
45 | * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
46 | */ |
||
47 | protected $repository; |
||
48 | |||
49 | /** |
||
50 | * @var \Illuminate\Config\Repository $config |
||
51 | */ |
||
52 | protected $config; |
||
53 | |||
54 | /** |
||
55 | * @var \Xethron\MigrationsGenerator\Generators\SchemaGenerator |
||
56 | */ |
||
57 | protected $schemaGenerator; |
||
58 | |||
59 | /** |
||
60 | * Array of Fields to create in a new Migration |
||
61 | * Namely: Columns, Indexes and Foreign Keys |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $fields = array(); |
||
65 | |||
66 | /** |
||
67 | * List of Migrations that has been done |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $migrations = array(); |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | protected $log = false; |
||
76 | |||
77 | /** |
||
78 | * @var int |
||
79 | */ |
||
80 | protected $batch; |
||
81 | |||
82 | /** |
||
83 | * Filename date prefix (Y_m_d_His) |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $datePrefix; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $migrationName; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $method; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $table; |
||
102 | |||
103 | /** |
||
104 | * @var string|null |
||
105 | */ |
||
106 | protected $connection = null; |
||
107 | |||
108 | /** |
||
109 | * @param \Way\Generators\Generator $generator |
||
110 | * @param \Way\Generators\Filesystem\Filesystem $file |
||
111 | * @param \Way\Generators\Compilers\TemplateCompiler $compiler |
||
112 | * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
113 | * @param \Illuminate\Config\Repository $config |
||
114 | */ |
||
115 | public function __construct( |
||
130 | |||
131 | /** |
||
132 | * Execute the console command. |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function fire() |
||
181 | |||
182 | /** |
||
183 | * Ask for user input: Yes/No |
||
184 | * @param string $question Question to ask |
||
185 | * @return boolean Answer from user |
||
186 | */ |
||
187 | protected function askYn( $question ) { |
||
194 | |||
195 | /** |
||
196 | * Ask user for a Numeric Value, or blank for default |
||
197 | * @param string $question Question to ask |
||
198 | * @param int|float $default Default Value (optional) |
||
199 | * @return int|float Answer |
||
200 | */ |
||
201 | protected function askNumeric( $question, $default = null ) { |
||
219 | |||
220 | /** |
||
221 | * Generate tables and index migrations. |
||
222 | * |
||
223 | * @param array $tables List of tables to create migrations for |
||
224 | * @return void |
||
225 | */ |
||
226 | View Code Duplication | protected function generateTablesAndIndices( array $tables ) |
|
238 | |||
239 | /** |
||
240 | * Generate foreign key migrations. |
||
241 | * |
||
242 | * @param array $tables List of tables to create migrations for |
||
243 | * @return void |
||
244 | */ |
||
245 | View Code Duplication | protected function generateForeignKeys( array $tables ) |
|
257 | |||
258 | /** |
||
259 | * Generate Migration for the current table. |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | protected function generate() |
||
274 | |||
275 | /** |
||
276 | * The path where the file will be created |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | protected function getFileGenerationPath() |
||
288 | |||
289 | /** |
||
290 | * Get the date prefix for the migration. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function getDatePrefix() |
||
298 | |||
299 | /** |
||
300 | * Fetch the template data |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | protected function getTemplateData() |
||
322 | |||
323 | /** |
||
324 | * Get path to template for generator |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | protected function getTemplatePath() |
||
332 | |||
333 | /** |
||
334 | * Get the console command arguments. |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | protected function getArguments() |
||
344 | |||
345 | /** |
||
346 | * Get the console command options. |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | protected function getOptions() |
||
362 | |||
363 | /** |
||
364 | * Remove all the tables to exclude from the array of tables |
||
365 | * |
||
366 | * @param array $tables |
||
367 | * |
||
368 | * @return array |
||
369 | */ |
||
370 | protected function removeExcludedTables( array $tables ) |
||
377 | |||
378 | /** |
||
379 | * Get a list of tables to exclude |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | protected function getExcludedTables() |
||
393 | |||
394 | } |
||
395 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.