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 KoalamonReporter 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 KoalamonReporter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class KoalamonReporter implements Reporter |
||
17 | { |
||
18 | /** |
||
19 | * @var Result[] |
||
20 | */ |
||
21 | private $results = []; |
||
22 | |||
23 | private $config; |
||
24 | private $system; |
||
25 | private $collect; |
||
26 | private $identifier; |
||
27 | private $systemUseRetriever; |
||
28 | private $tool = 'smoke'; |
||
29 | private $groupBy; |
||
30 | private $server; |
||
31 | |||
32 | /** |
||
33 | * @var KoalaReporter |
||
34 | */ |
||
35 | private $reporter; |
||
36 | |||
37 | /* |
||
38 | * @var Retriever |
||
39 | */ |
||
40 | private $retriever; |
||
41 | |||
42 | private $output; |
||
43 | |||
44 | const STATUS_SUCCESS = 'success'; |
||
45 | const STATUS_FAILURE = 'failure'; |
||
46 | |||
47 | public function init($apiKey, Configuration $_configuration, OutputInterface $_output, $server = 'http://www.koalamon.com', $system = '', $identifier = '', $tool = '', $collect = true, $systemUseRetriever = false, $groupBy = false) |
||
67 | |||
68 | public function setResponseRetriever(Retriever $retriever) |
||
72 | |||
73 | /** |
||
74 | * @param Rule []; |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | private function getRuleKeys() |
||
87 | |||
88 | public function processResult(Result $result) |
||
92 | |||
93 | public function finish() |
||
94 | { |
||
95 | $this->output->writeln('Sending results to ' . $this->server . " ... \n"); |
||
96 | |||
97 | if ($this->groupBy === 'prefix') { |
||
98 | $this->sendGroupedByPrefix(); |
||
99 | } else { |
||
100 | if ($this->collect) { |
||
101 | $this->sendCollected(); |
||
102 | } else { |
||
103 | $this->sendSingle(); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | private function getPrefix($string) |
||
112 | |||
113 | private function sendGroupedByPrefix() |
||
162 | |||
163 | private function sendSingle() |
||
164 | { |
||
165 | $rules = $this->getRuleKeys(); |
||
166 | foreach ($this->results as $result) { |
||
167 | $failedTests = array(); |
||
168 | if ($result->isFailure()) { |
||
169 | foreach ($result->getMessages() as $ruleLKey => $message) { |
||
170 | $identifier = 'smoke_' . $ruleLKey . '_' . $result->getUrl(); |
||
171 | |||
172 | if ($this->system === '') { |
||
173 | $system = str_replace('http://', '', $result->getUrl()); |
||
174 | } else { |
||
175 | $system = $this->system; |
||
176 | } |
||
177 | $this->send($identifier, $system, 'smoke', $message, self::STATUS_FAILURE, (string) $result->getUrl()); |
||
178 | $failedTests[] = $ruleLKey; |
||
179 | } |
||
180 | } |
||
181 | foreach ($rules as $rule) { |
||
182 | if (!in_array($rule, $failedTests, true)) { |
||
183 | $identifier = 'smoke_' . $rule . '_' . $result->getUrl(); |
||
184 | |||
185 | if ($this->systemUseRetriever) { |
||
186 | $system = $this->retriever->getSystem($result->getUrl()); |
||
187 | } elseif ($this->system === '') { |
||
188 | $system = str_replace('http://', '', $result->getUrl()); |
||
189 | } else { |
||
190 | $system = $this->system; |
||
191 | } |
||
192 | $this->send($identifier, $system, 'smoke_' . $rule . '_' . $result->getUrl(), self::STATUS_SUCCESS, (string) $result->getUrl()); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | private function sendCollected() |
||
239 | |||
240 | private function send($identifier, $system, $message, $status, $url = '', $value = 0, $tool = null) |
||
248 | } |
||
249 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: