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 |
||
38 | class PrintingVisitor extends Visitor |
||
39 | { |
||
40 | /** |
||
41 | * End-of-line chars used when generating files |
||
42 | */ |
||
43 | const EOL = "\r\n"; |
||
44 | |||
45 | /** |
||
46 | * @var Output |
||
47 | */ |
||
48 | private $output; |
||
49 | |||
50 | public function setOutput(Output $output): void |
||
51 | { |
||
52 | $this->output = $output; |
||
53 | } |
||
54 | |||
55 | public function beforeDate(Node $node): void |
||
56 | { |
||
57 | if ($node->getValue() instanceof \DateTimeInterface) { |
||
58 | $this->output->write($node->getValue()->format('Ymd')); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public function beforeImmediateDate(): void |
||
63 | { |
||
64 | $this->output->write('GENAST '); |
||
65 | } |
||
66 | |||
67 | public function beforeText(Node $node): void |
||
68 | { |
||
69 | $this->output->write($node->getValue()); |
||
70 | } |
||
71 | |||
72 | public function beforePayerNumber(Node $node): void |
||
73 | { |
||
74 | $this->output->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT)); |
||
75 | } |
||
76 | |||
77 | public function beforePayeeBgcNumber(Node $node): void |
||
78 | { |
||
79 | $this->output->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT)); |
||
80 | } |
||
81 | |||
82 | View Code Duplication | public function beforePayeeBankgiro(Node $node): void |
|
|
|||
83 | { |
||
84 | if ($node->getValue() instanceof AccountNumber) { |
||
85 | $account = $node->getValue(); |
||
86 | $this->output->write( |
||
87 | str_pad($account->getSerialNumber() . $account->getCheckDigit(), 10, '0', STR_PAD_LEFT) |
||
88 | ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | View Code Duplication | public function beforeAccount(Node $node): void |
|
93 | { |
||
94 | if ($node->getValue() instanceof AccountNumber) { |
||
95 | $account = $node->getValue(); |
||
96 | $this->output->write( |
||
97 | $account->getClearingNumber() |
||
98 | . str_pad($account->getSerialNumber() . $account->getCheckDigit(), 12, '0', STR_PAD_LEFT) |
||
99 | ); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public function beforeInterval(Node $node): void |
||
104 | { |
||
105 | if (!in_array((int)$node->getValue(), range('0', '8'))) { |
||
106 | throw new RuntimeException('Interval must be between 0 and 8'); |
||
107 | } |
||
108 | $this->output->write($node->getValue()); |
||
109 | } |
||
110 | |||
111 | public function beforeAmount(Node $node): void |
||
112 | { |
||
113 | if ($node->getValue() instanceof SEK) { |
||
114 | $amount = $node->getValue(); |
||
115 | |||
116 | if ($amount->isGreaterThan(new SEK('9999999999.99')) || $amount->isLessThan(new SEK('-9999999999.99'))) { |
||
117 | throw new RuntimeException('Amount must be between 9999999999.99 and -9999999999.99'); |
||
118 | } |
||
119 | |||
120 | $this->output->write( |
||
121 | str_pad($amount->getSignalString(), 12, '0', STR_PAD_LEFT) |
||
122 | ); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | public function beforeStateId(Node $node): void |
||
135 | |||
136 | public function beforeOpening(): void |
||
137 | { |
||
138 | $this->output->write('01'); |
||
139 | } |
||
140 | |||
141 | public function beforeCreateMandateRequest(): void |
||
142 | { |
||
143 | $this->output->write('04'); |
||
144 | } |
||
145 | |||
146 | public function beforeDeleteMandateRequest(): void |
||
147 | { |
||
150 | |||
151 | public function beforeAcceptDigitalMandateRequest(): void |
||
155 | |||
156 | public function beforeRejectDigitalMandateRequest(): void |
||
160 | |||
161 | public function beforeUpdateMandateRequest(): void |
||
165 | |||
166 | public function beforeIncomingPaymentRequest(): void |
||
170 | |||
171 | public function beforeOutgoingPaymentRequest(): void |
||
175 | |||
176 | public function beforeAmendmentRequest(): void |
||
180 | |||
181 | public function afterRecord(): void |
||
185 | } |
||
186 |
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.