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 |
||
| 48 | class TreeBuilder |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var RequestOpeningRecordNode Opening record used for each layout |
||
| 52 | */ |
||
| 53 | private $opening; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var RecordNode[] List of created mandate records |
||
| 57 | */ |
||
| 58 | private $mandateRecords; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var RecordNode[] List of created transaction records |
||
| 62 | */ |
||
| 63 | private $transactionRecords; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var RecordNode[] List of created amendment records |
||
| 67 | */ |
||
| 68 | private $amendmentRecords; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string Payee BGC customer number |
||
| 72 | */ |
||
| 73 | private $bgcNr; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var PayeeBankgiroNode Wrapper around payee bankgiro account number |
||
| 77 | */ |
||
| 78 | private $payeeBgNode; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var \DateTimeInterface Date of file creation |
||
| 82 | */ |
||
| 83 | private $date; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $bgcNr The BGC customer number of payee |
||
| 87 | * @param Bankgiro $bankgiro Payee bankgiro account number |
||
| 88 | * @param \DateTimeInterface $date Optional creation date |
||
| 89 | */ |
||
| 90 | public function __construct(string $bgcNr, Bankgiro $bankgiro, \DateTimeInterface $date = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Reset builder to initial state |
||
| 100 | */ |
||
| 101 | public function reset() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Add a new mandate record to tree |
||
| 119 | */ |
||
| 120 | public function addCreateMandateRecord(string $payerNr, AccountNumber $account, Id $id) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Add a delete mandate record to tree |
||
| 134 | */ |
||
| 135 | View Code Duplication | public function addDeleteMandateRecord(string $payerNr) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Add an accept digital mandate record to tree |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function addAcceptMandateRecord(string $payerNr) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Add a reject digital mandate record to tree |
||
| 160 | */ |
||
| 161 | public function addRejectMandateRecord(string $payerNr) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Add an update mandate record to tree |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function addUpdateMandateRecord(string $payerNr, string $newPayerNr) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get the created request tree |
||
| 190 | */ |
||
| 191 | public function buildTree(): FileNode |
||
| 203 | } |
||
| 204 |
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.