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 |
||
12 | class Migration |
||
13 | { |
||
14 | /** @var string */ |
||
15 | public $now; |
||
16 | /** @var string|null */ |
||
17 | private $connection; |
||
18 | /** @var string */ |
||
19 | private $name; |
||
20 | |||
21 | /** |
||
22 | * Migration constructor. Pass connection name inside if exist |
||
23 | * @param null $connectionName |
||
24 | */ |
||
25 | public function __construct($migrationName, $connectionName = null) |
||
31 | |||
32 | /** |
||
33 | * Insert data into migration table |
||
34 | */ |
||
35 | View Code Duplication | public function up() |
|
44 | |||
45 | /** |
||
46 | * Remove data from migration table |
||
47 | */ |
||
48 | View Code Duplication | public function down() |
|
56 | |||
57 | /** |
||
58 | * Set connection name |
||
59 | * @param string|null $name |
||
60 | * @return void |
||
61 | */ |
||
62 | public function setConnection($name = null) |
||
66 | |||
67 | /** |
||
68 | * Get database connection instance |
||
69 | * @return \Illuminate\Database\Connection |
||
70 | */ |
||
71 | public function getConnection() |
||
75 | |||
76 | /** |
||
77 | * Get database connection schema builder for current instance of connection |
||
78 | * @return \Illuminate\Database\Schema\Builder |
||
79 | */ |
||
80 | public function getSchema() |
||
84 | } |
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.