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 |
||
19 | class Table extends Generic |
||
20 | { |
||
21 | /** |
||
22 | * Query constructor. |
||
23 | * |
||
24 | * @param Generator $generator |
||
25 | * @param $metadata |
||
26 | */ |
||
27 | public function __construct(Generator $generator, $metadata) |
||
33 | |||
34 | /** |
||
35 | * Class uses generation part. |
||
36 | * |
||
37 | * @param \samsoncms\api\generator\metadata\Gallery $metadata Entity metadata |
||
38 | */ |
||
39 | protected function createUses($metadata) |
||
47 | |||
48 | /** |
||
49 | * Class definition generation part. |
||
50 | * |
||
51 | * @param Virtual $metadata Entity metadata |
||
52 | */ |
||
53 | protected function createDefinition($metadata) |
||
54 | { |
||
55 | $this->generator |
||
56 | ->multiComment(array( |
||
57 | 'Class for rendering "' . $metadata->entityRealName . '" table', |
||
58 | )) |
||
59 | ->defClass($this->className, '\\'.\samsoncms\api\field\Table::class) |
||
60 | ->newLine('use \\'.\samsoncms\api\Renderable::class.';') |
||
61 | ->newLine(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Class constants generation part. |
||
66 | * |
||
67 | * @param Virtual $metadata Entity metadata |
||
68 | */ |
||
69 | protected function createConstants($metadata) |
||
79 | |||
80 | /** |
||
81 | * Class static fields generation part. |
||
82 | * |
||
83 | * @param Virtual $metadata Entity metadata |
||
84 | */ |
||
85 | protected function createStaticFields($metadata) |
||
91 | |||
92 | /** |
||
93 | * Class methods generation part. |
||
94 | * |
||
95 | * @param Virtual $metadata Entity metadata |
||
96 | */ |
||
97 | View Code Duplication | protected function createMethods($metadata) |
|
121 | |||
122 | /** |
||
123 | * Class constructor generation part. |
||
124 | * |
||
125 | * @param Virtual $metadata Entity metadata |
||
126 | */ |
||
127 | protected function createConstructor($metadata) |
||
143 | } |
||
144 | //[PHPCOMPRESSOR(remove,end)] |
||
145 |
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.