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 FormHandler 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 FormHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class FormHandler |
||
13 | { |
||
14 | /** |
||
15 | * bPost instance |
||
16 | * |
||
17 | * @var Bpost |
||
18 | */ |
||
19 | private $bpost; |
||
20 | |||
21 | /** |
||
22 | * The parameters |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | private $parameters = array(); |
||
27 | |||
28 | /** |
||
29 | * Create bPostFormHandler instance |
||
30 | * |
||
31 | * @param string $accountId |
||
32 | * @param string $passPhrase |
||
33 | * @param string $apiUrl |
||
34 | */ |
||
35 | public function __construct($accountId, $passPhrase, $apiUrl = Bpost::API_URL) |
||
36 | { |
||
37 | $this->bpost = new Bpost($accountId, $passPhrase, $apiUrl); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Calculate the hash |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | private function getChecksum() |
||
77 | |||
78 | /** |
||
79 | * Get the parameters |
||
80 | * |
||
81 | * @param bool $form |
||
82 | * @param bool $includeChecksum |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getParameters($form = false, $includeChecksum = true) |
||
86 | { |
||
87 | $return = $this->parameters; |
||
88 | |||
89 | if ($form && isset($return['orderLine'])) { |
||
90 | foreach ($return['orderLine'] as $key => $value) { |
||
91 | $return['orderLine[' . $key . ']'] = $value; |
||
92 | } |
||
93 | |||
94 | unset($return['orderLine']); |
||
95 | } |
||
96 | |||
97 | if ($includeChecksum) { |
||
98 | $return['accountId'] = $this->bpost->getAccountId(); |
||
99 | $return['checksum'] = $this->getChecksum(); |
||
100 | } |
||
101 | |||
102 | return $return; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Set a parameter |
||
107 | * |
||
108 | * @param string $key |
||
109 | * @param mixed $value |
||
110 | * @throws BpostInvalidValueException |
||
111 | * @throws BpostInvalidLengthException |
||
112 | */ |
||
113 | public function setParameter($key, $value) |
||
211 | } |
||
212 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.