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:
Complex classes like Rabo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Rabo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Rabo extends Engine |
||
12 | { |
||
13 | /** |
||
14 | * returns the name of the bank. |
||
15 | * |
||
16 | * @return string |
||
17 | */ |
||
18 | protected function parseStatementBank() |
||
22 | |||
23 | /** |
||
24 | * Overloaded: Rabo has different way of storing account info. |
||
25 | * |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | View Code Duplication | protected function parseTransactionAccount() |
|
46 | |||
47 | /** |
||
48 | * Overloaded: Rabo has different way of storing account name. |
||
49 | * |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | protected function parseTransactionAccountName() |
||
78 | |||
79 | /** |
||
80 | * Overloaded: Rabo has different way of storing transaction value timestamps (ymd). |
||
81 | * |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | protected function parseTransactionEntryTimestamp() |
||
93 | |||
94 | /** |
||
95 | * Overloaded: Rabo has different way of storing transaction value timestamps (ymd). |
||
96 | * |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | View Code Duplication | protected function parseTransactionValueTimestamp() |
|
108 | |||
109 | /** |
||
110 | * Overloaded: Rabo uses longer strings for accountnumbers. |
||
111 | * |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | protected function sanitizeAccount($string) |
||
123 | |||
124 | /** |
||
125 | * Overloaded: Rabo encapsulates the description with /REMI/ for SEPA. |
||
126 | * |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | View Code Duplication | protected function sanitizeDescription($string) |
|
145 | |||
146 | /** |
||
147 | * Overloaded: Is applicable if first line has :940:. |
||
148 | * |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public static function isApplicable($string) |
||
155 | |||
156 | protected function parseTransactionType() |
||
209 | |||
210 | } |
||
211 |
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.