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 |
||
39 | class PrintingVisitor extends Visitor |
||
40 | { |
||
41 | /** |
||
42 | * End-of-line chars used when generating files |
||
43 | */ |
||
44 | const EOL = "\r\n"; |
||
45 | |||
46 | /** |
||
47 | * @var Output |
||
48 | */ |
||
49 | private $output; |
||
50 | |||
51 | public function setOutput(Output $output): void |
||
52 | { |
||
53 | $this->output = $output; |
||
54 | } |
||
55 | |||
56 | public function beforeDate(Node $node): void |
||
57 | { |
||
58 | if ($node->getValue() instanceof \DateTimeInterface) { |
||
59 | $this->output->write($node->getValue()->format('Ymd')); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public function beforeImmediateDate(): void |
||
64 | { |
||
65 | $this->output->write('GENAST '); |
||
66 | } |
||
67 | |||
68 | public function beforeText(Text $node): void |
||
69 | { |
||
70 | $this->output->write($node->getValue()); |
||
71 | } |
||
72 | |||
73 | public function beforePayerNumber(Node $node): void |
||
74 | { |
||
75 | $this->output->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT)); |
||
76 | } |
||
77 | |||
78 | public function beforePayeeBgcNumber(Node $node): void |
||
79 | { |
||
80 | $this->output->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT)); |
||
81 | } |
||
82 | |||
83 | View Code Duplication | public function beforePayeeBankgiro(Node $node): void |
|
|
|||
84 | { |
||
85 | if ($node->getValue() instanceof AccountNumber) { |
||
86 | $account = $node->getValue(); |
||
87 | $this->output->write( |
||
88 | str_pad($account->getSerialNumber() . $account->getCheckDigit(), 10, '0', STR_PAD_LEFT) |
||
89 | ); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | View Code Duplication | public function beforeAccount(Node $node): void |
|
94 | { |
||
95 | if ($node->getValue() instanceof AccountNumber) { |
||
96 | $account = $node->getValue(); |
||
97 | $this->output->write( |
||
98 | $account->getClearingNumber() |
||
99 | . str_pad($account->getSerialNumber() . $account->getCheckDigit(), 12, '0', STR_PAD_LEFT) |
||
100 | ); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | public function beforeInterval(Node $node): void |
||
105 | { |
||
106 | if (!in_array((int)$node->getValue(), range('0', '8'))) { |
||
107 | throw new RuntimeException('Interval must be between 0 and 8'); |
||
108 | } |
||
109 | $this->output->write($node->getValue()); |
||
110 | } |
||
111 | |||
112 | public function beforeAmount(Node $node): void |
||
126 | |||
127 | public function beforeStateId(Node $node): void |
||
136 | |||
137 | public function beforeOpening(): void |
||
138 | { |
||
139 | $this->output->write('01'); |
||
140 | } |
||
141 | |||
142 | public function afterOpening(): void |
||
143 | { |
||
144 | $this->output->write(self::EOL); |
||
145 | } |
||
146 | |||
147 | public function beforeCreateMandateRequest(): void |
||
151 | |||
152 | public function afterCreateMandateRequest(): void |
||
156 | |||
157 | public function beforeDeleteMandateRequest(): void |
||
161 | |||
162 | public function afterDeleteMandateRequest(): void |
||
166 | |||
167 | public function beforeAcceptDigitalMandateRequest(): void |
||
171 | |||
172 | public function afterAcceptDigitalMandateRequest(): void |
||
176 | |||
177 | public function beforeRejectDigitalMandateRequest(): void |
||
181 | |||
182 | public function afterRejectDigitalMandateRequest(): void |
||
186 | |||
187 | public function beforeUpdateMandateRequest(): void |
||
191 | |||
192 | public function afterUpdateMandateRequest(): void |
||
196 | |||
197 | public function beforeIncomingPaymentRequest(): void |
||
201 | |||
202 | public function afterIncomingPaymentRequest(): void |
||
206 | |||
207 | public function beforeOutgoingPaymentRequest(): void |
||
211 | |||
212 | public function afterOutgoingPaymentRequest(): void |
||
216 | } |
||
217 |
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.