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 Anomaly\Streams\Platform\Stream; |
||
14 | class StreamSchema |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * The schema builder. |
||
19 | * |
||
20 | * @var Builder |
||
21 | */ |
||
22 | protected $schema; |
||
23 | |||
24 | /** |
||
25 | * Create a new StreamSchema instance. |
||
26 | */ |
||
27 | public function __construct() |
||
31 | |||
32 | /** |
||
33 | * Create a table. |
||
34 | * |
||
35 | * @param StreamInterface |
||
36 | */ |
||
37 | public function createTable(StreamInterface $stream) |
||
38 | { |
||
39 | $table = $stream->getEntryTableName(); |
||
40 | |||
41 | $this->schema->dropIfExists($table); |
||
42 | |||
43 | $this->schema->create( |
||
44 | $table, |
||
45 | View Code Duplication | function (Blueprint $table) use ($stream) { |
|
|
|||
46 | |||
47 | $table->engine = $stream->getConfig('database.engine'); |
||
48 | |||
49 | $table->increments('id'); |
||
50 | $table->integer('sort_order')->nullable(); |
||
51 | $table->datetime('created_at'); |
||
52 | $table->integer('created_by_id')->nullable(); |
||
53 | $table->datetime('updated_at')->nullable(); |
||
54 | $table->integer('updated_by_id')->nullable(); |
||
55 | |||
56 | if ($stream->isTrashable()) { |
||
57 | $table->datetime('deleted_at')->nullable(); |
||
58 | } |
||
59 | } |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Create translations table. |
||
65 | * |
||
66 | * @param StreamInterface $stream |
||
67 | */ |
||
68 | public function createTranslationsTable(StreamInterface $stream) |
||
69 | { |
||
70 | $this->schema->dropIfExists($stream->getEntryTranslationsTableName()); |
||
71 | |||
72 | $this->schema->create( |
||
73 | $stream->getEntryTranslationsTableName(), |
||
74 | View Code Duplication | function (Blueprint $table) use ($stream) { |
|
75 | |||
76 | $table->engine = $stream->getConfig('database.engine'); |
||
77 | |||
78 | $table->increments('id'); |
||
79 | $table->integer('entry_id'); |
||
80 | $table->datetime('created_at'); |
||
81 | $table->integer('created_by_id')->nullable(); |
||
82 | $table->datetime('updated_at')->nullable(); |
||
83 | $table->integer('updated_by_id')->nullable(); |
||
84 | $table->string('locale')->index(); |
||
85 | } |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Rename a table. |
||
91 | * |
||
92 | * @param StreamInterface $from |
||
93 | * @param StreamInterface $to |
||
94 | */ |
||
95 | View Code Duplication | public function renameTable(StreamInterface $from, StreamInterface $to) |
|
103 | |||
104 | /** |
||
105 | * Rename a translations table. |
||
106 | * |
||
107 | * @param StreamInterface $from |
||
108 | * @param StreamInterface $to |
||
109 | */ |
||
110 | View Code Duplication | public function renameTranslationsTable(StreamInterface $from, StreamInterface $to) |
|
118 | |||
119 | /** |
||
120 | * Drop a table. |
||
121 | * |
||
122 | * @param $table |
||
123 | */ |
||
124 | public function dropTable($table) |
||
128 | } |
||
129 |
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.