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 |
||
16 | class LoadOrderData extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var array Orders |
||
20 | */ |
||
21 | protected $orderData = [ |
||
22 | [ |
||
23 | 'status' => 'canceled', |
||
24 | 'incrementId' => '1', |
||
25 | 'customerEmail' => '[email protected]', |
||
26 | 'createdSub' => 'P1D', |
||
27 | 'updatedSub' => 'P1D', |
||
28 | 'discountAmount' => 4.40, |
||
29 | 'taxAmount' => 12.47, |
||
30 | 'shippingAmount' => 5, |
||
31 | 'totalPaidAmount' => 17.85, |
||
32 | 'subtotalAmount' => 17.85, |
||
33 | 'totalInvoicedAmount' => 11, |
||
34 | 'totalRefundedAmount' => 4, |
||
35 | 'totalCanceledAmount' => 0, |
||
36 | 'shippingMethod' => 'some unique shipping method', |
||
37 | 'remoteIp' => 'unique ip', |
||
38 | 'giftMessage' => 'some very unique gift message', |
||
39 | 'dataChannel' => 'Channel.CustomerChannel', |
||
40 | 'customer' => 'Channel.CustomerIdentity.CustomerIdentity', |
||
41 | |||
42 | 'reference' => 'order_1' |
||
43 | ], |
||
44 | [ |
||
45 | 'status' => 'done', |
||
46 | 'incrementId' => '2', |
||
47 | 'customerEmail' => '[email protected]', |
||
48 | 'createdSub' => 'P6D', |
||
49 | 'updatedSub' => 'P6D', |
||
50 | 'discountAmount' => 4.40, |
||
51 | 'taxAmount' => 12.47, |
||
52 | 'shippingAmount' => 5, |
||
53 | 'totalPaidAmount' => 17.85, |
||
54 | 'subtotalAmount' => 15.5, |
||
55 | 'totalInvoicedAmount' => 11, |
||
56 | 'totalRefundedAmount' => 4, |
||
57 | 'totalCanceledAmount' => 0, |
||
58 | 'shippingMethod' => 'some unique shipping method', |
||
59 | 'remoteIp' => 'unique ip', |
||
60 | 'giftMessage' => 'some very unique gift message', |
||
61 | 'dataChannel' => 'Channel.CustomerChannel', |
||
62 | 'customer' => 'Channel.CustomerIdentity.CustomerIdentity', |
||
63 | |||
64 | 'reference' => 'order_2' |
||
65 | ], |
||
66 | [ |
||
67 | 'status' => 'done', |
||
68 | 'incrementId' => '3', |
||
69 | 'customerEmail' => '[email protected]', |
||
70 | 'createdSub' => 'P1D', |
||
71 | 'updatedSub' => 'P1D', |
||
72 | 'discountAmount' => 4.40, |
||
73 | 'taxAmount' => 12.47, |
||
74 | 'shippingAmount' => 5, |
||
75 | 'totalPaidAmount' => 6.85, |
||
76 | 'subtotalAmount' => 15.5, |
||
77 | 'totalInvoicedAmount' => 11, |
||
78 | 'totalRefundedAmount' => 4, |
||
79 | 'totalCanceledAmount' => 0, |
||
80 | 'shippingMethod' => 'some unique shipping method', |
||
81 | 'remoteIp' => 'unique ip', |
||
82 | 'giftMessage' => 'some very unique gift message', |
||
83 | 'dataChannel' => 'Channel.CustomerChannel', |
||
84 | 'customer' => 'Channel.CustomerChannel.Customer', |
||
85 | |||
86 | 'reference' => 'order_3' |
||
87 | ], |
||
88 | [ |
||
89 | 'status' => 'done', |
||
90 | 'incrementId' => '4', |
||
91 | 'customerEmail' => '[email protected]', |
||
92 | 'createdSub' => 'P364D', |
||
93 | 'updatedSub' => 'P364D', |
||
94 | 'discountAmount' => 4.40, |
||
95 | 'taxAmount' => 12.47, |
||
96 | 'shippingAmount' => 5, |
||
97 | 'totalPaidAmount' => 17.85, |
||
98 | 'subtotalAmount' => 5.85, |
||
99 | 'totalInvoicedAmount' => 11, |
||
100 | 'totalRefundedAmount' => 4, |
||
101 | 'totalCanceledAmount' => 0, |
||
102 | 'shippingMethod' => 'some unique shipping method', |
||
103 | 'remoteIp' => 'unique ip', |
||
104 | 'giftMessage' => 'some very unique gift message', |
||
105 | 'dataChannel' => 'Channel.CustomerChannel', |
||
106 | 'customer' => 'Channel.CustomerChannel.Customer', |
||
107 | |||
108 | 'reference' => 'order_4' |
||
109 | ], |
||
110 | ]; |
||
111 | |||
112 | /** |
||
113 | * @var ContainerInterface |
||
114 | */ |
||
115 | protected $container; |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function setContainer(ContainerInterface $container = null) |
||
124 | |||
125 | /** |
||
126 | * @param object $entity |
||
127 | * @param array $data |
||
128 | * @param array $excludeProperties |
||
129 | */ |
||
130 | View Code Duplication | public function setEntityPropertyValues($entity, array $data, array $excludeProperties = []) |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function load(ObjectManager $manager) |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function getDependencies() |
||
177 | } |
||
178 |
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.