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 |
||
31 | class Writer implements WriterInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var TreeBuilder Helper used when building trees |
||
35 | */ |
||
36 | private $treeBuilder; |
||
37 | |||
38 | /** |
||
39 | * @var PrintingVisitor Helper used when generating content |
||
40 | */ |
||
41 | private $printer; |
||
42 | |||
43 | /** |
||
44 | * @var VisitorInterface Helper used to validate and process tree |
||
45 | */ |
||
46 | private $visitor; |
||
47 | |||
48 | public function __construct(TreeBuilder $treeBuilder, PrintingVisitor $printer, VisitorInterface $visitor) |
||
54 | |||
55 | public function getContent(): string |
||
65 | |||
66 | public function reset(): void |
||
70 | |||
71 | public function addNewMandate(string $payerNr, AccountNumber $account, IdInterface $id): void |
||
75 | |||
76 | public function deleteMandate(string $payerNr): void |
||
80 | |||
81 | public function acceptDigitalMandate(string $payerNr): void |
||
85 | |||
86 | public function rejectDigitalMandate(string $payerNr): void |
||
90 | |||
91 | public function updateMandate(string $payerNr, string $newPayerNr): void |
||
95 | |||
96 | View Code Duplication | public function addPayment( |
|
106 | |||
107 | public function addMonthlyPayment(string $payerNr, SEK $amount, \DateTimeInterface $date, string $ref = ''): void |
||
111 | |||
112 | public function addImmediatePayment(string $payerNr, SEK $amount, string $ref = ''): void |
||
116 | |||
117 | View Code Duplication | public function addOutgoingPayment( |
|
127 | |||
128 | public function addImmediateOutgoingPayment(string $payerNr, SEK $amount, string $ref = ''): void |
||
132 | |||
133 | public function deletePayments(string $payerNr): void |
||
137 | } |
||
138 |
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.