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 |
||
| 34 | class Writer |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var TreeBuilder Helper used when building trees |
||
| 38 | */ |
||
| 39 | private $treeBuilder; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var PrintingVisitor Helper used when generating content |
||
| 43 | */ |
||
| 44 | private $printer; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Visitor Helper used to validate and process tree |
||
| 48 | */ |
||
| 49 | private $visitor; |
||
| 50 | |||
| 51 | public function __construct(TreeBuilder $treeBuilder, PrintingVisitor $printer, Visitor $visitor) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Build and return request content |
||
| 60 | */ |
||
| 61 | public function getContent(): string |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Reset internal build queue |
||
| 74 | */ |
||
| 75 | public function reset(): void |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Add a new mandate request to the build queue |
||
| 82 | * |
||
| 83 | * @param string $payerNr Number identifying the payer |
||
| 84 | * @param AccountNumber $account Payer account number |
||
| 85 | * @param IdInterface $id Payer id number |
||
| 86 | */ |
||
| 87 | public function addNewMandate(string $payerNr, AccountNumber $account, IdInterface $id): void |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Add a delete mandate request to the build queue |
||
| 94 | * |
||
| 95 | * @param string $payerNr Number identifying the payer |
||
| 96 | */ |
||
| 97 | public function deleteMandate(string $payerNr): void |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add an accept digital mandate request to the build queue |
||
| 104 | * |
||
| 105 | * @param string $payerNr Number identifying the payer |
||
| 106 | */ |
||
| 107 | public function acceptDigitalMandate(string $payerNr): void |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Add a reject digital mandate request to the build queue |
||
| 114 | * |
||
| 115 | * @param string $payerNr Number identifying the payer |
||
| 116 | */ |
||
| 117 | public function rejectDigitalMandate(string $payerNr): void |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Add an update mandate request to the build queue |
||
| 124 | * |
||
| 125 | * @param string $payerNr Old number identifying the payer |
||
| 126 | * @param string $newPayerNr New number identifying the payer |
||
| 127 | */ |
||
| 128 | public function updateMandate(string $payerNr, string $newPayerNr): void |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Add an incoming payment request to the build queue |
||
| 135 | * |
||
| 136 | * @param string $payerNr Number identifying the payer |
||
| 137 | * @param SEK $amount The requested payment amount |
||
| 138 | * @param \DateTimeInterface $date Requested date of payment (or first date for repeated payments) |
||
| 139 | * @param string $ref Custom payment reference number |
||
| 140 | * @param string $interval Interval for repeted payment, use one of the Intervals constants |
||
| 141 | * @param integer $repetitions Number of repititions (0 repeates payments indefinitely) |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function addPayment( |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Add an incoming payment request to the build queue |
||
| 156 | * |
||
| 157 | * @param string $payerNr Number identifying the payer |
||
| 158 | * @param SEK $amount The requested payment amount |
||
| 159 | * @param \DateTimeInterface $date Requested first date of payment |
||
| 160 | * @param string $ref Custom payment reference number |
||
| 161 | */ |
||
| 162 | public function addMonthlyPayment( |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Add an incoming payment at next possible bank date request to the build queue |
||
| 173 | * |
||
| 174 | * @param string $payerNr Number identifying the payer |
||
| 175 | * @param SEK $amount The requested payment amount |
||
| 176 | * @param string $ref Custom payment reference number |
||
| 177 | */ |
||
| 178 | public function addImmediatePayment(string $payerNr, SEK $amount, string $ref = ''): void |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Add an outgoing payment request to the build queue |
||
| 185 | * |
||
| 186 | * @param string $payerNr Number identifying the payer |
||
| 187 | * @param SEK $amount The requested payment amount |
||
| 188 | * @param \DateTimeInterface $date Requested date of payment (or first date for repeated payments) |
||
| 189 | * @param string $ref Custom payment reference number |
||
| 190 | * @param string $interval Interval for repeted payment, use one of the Intervals constants |
||
| 191 | * @param integer $repetitions Number of repititions (0 repeateds payments indefinitely) |
||
| 192 | */ |
||
| 193 | View Code Duplication | public function addOutgoingPayment( |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Add an outgoing payment on next possible bank date request to the build queue |
||
| 206 | * |
||
| 207 | * @param string $payerNr Number identifying the payer |
||
| 208 | * @param SEK $amount The requested payment amount |
||
| 209 | * @param string $ref Custom payment reference number |
||
| 210 | */ |
||
| 211 | public function addImmediateOutgoingPayment(string $payerNr, SEK $amount, string $ref = ''): void |
||
| 215 | } |
||
| 216 |
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.