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 BrowscapUpdater 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 BrowscapUpdater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class BrowscapUpdater |
||
58 | { |
||
59 | /** |
||
60 | * The cache instance |
||
61 | * |
||
62 | * @var \BrowscapPHP\Cache\BrowscapCacheInterface |
||
63 | */ |
||
64 | private $cache = null; |
||
65 | |||
66 | /** |
||
67 | * @var @var \Psr\Log\LoggerInterface |
||
68 | */ |
||
69 | private $logger = null; |
||
70 | |||
71 | /** |
||
72 | * @var \GuzzleHttp\Client |
||
73 | */ |
||
74 | private $client = null; |
||
75 | |||
76 | /** |
||
77 | * Curl connect timeout in seconds |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | private $connectTimeout = 5; |
||
82 | |||
83 | /** |
||
84 | * Gets a cache instance |
||
85 | * |
||
86 | * @return \BrowscapPHP\Cache\BrowscapCacheInterface |
||
87 | */ |
||
88 | 15 | View Code Duplication | public function getCache() |
102 | |||
103 | /** |
||
104 | * Sets a cache instance |
||
105 | * |
||
106 | * @param \BrowscapPHP\Cache\BrowscapCacheInterface|\WurflCache\Adapter\AdapterInterface $cache |
||
107 | * |
||
108 | * @throws \BrowscapPHP\Exception |
||
109 | */ |
||
110 | 15 | View Code Duplication | public function setCache($cache) |
111 | { |
||
112 | 15 | if ($cache instanceof BrowscapCacheInterface) { |
|
113 | 11 | $this->cache = $cache; |
|
114 | 15 | } elseif ($cache instanceof AdapterInterface) { |
|
115 | 3 | $this->cache = new BrowscapCache($cache); |
|
116 | 3 | } else { |
|
117 | 1 | throw new Exception( |
|
118 | 'the cache has to be an instance of \BrowscapPHP\Cache\BrowscapCacheInterface or ' |
||
119 | 1 | . 'an instanceof of \WurflCache\Adapter\AdapterInterface', |
|
120 | Exception::CACHE_INCOMPATIBLE |
||
121 | 1 | ); |
|
122 | } |
||
123 | 14 | } |
|
124 | |||
125 | /** |
||
126 | * Sets a logger instance |
||
127 | * |
||
128 | * @param \Psr\Log\LoggerInterface $logger |
||
129 | */ |
||
130 | 11 | public function setLogger(LoggerInterface $logger) |
|
131 | { |
||
132 | 11 | $this->logger = $logger; |
|
133 | 11 | } |
|
134 | |||
135 | /** |
||
136 | * returns a logger instance |
||
137 | * |
||
138 | * @return \Psr\Log\LoggerInterface |
||
139 | */ |
||
140 | 12 | public function getLogger() |
|
148 | |||
149 | /** |
||
150 | * Sets the Connection Timeout |
||
151 | * |
||
152 | * @param int $connectTimeout |
||
153 | */ |
||
154 | 1 | public function setConnectTimeout($connectTimeout) |
|
158 | |||
159 | /** |
||
160 | * @return \GuzzleHttp\Client |
||
161 | */ |
||
162 | 10 | public function getClient() |
|
170 | |||
171 | /** |
||
172 | * @param \GuzzleHttp\Client $client |
||
173 | */ |
||
174 | 11 | public function setClient(Client $client) |
|
178 | |||
179 | /** |
||
180 | * reads and parses an ini file and writes the results into the cache |
||
181 | * |
||
182 | * @param string $iniFile |
||
183 | * |
||
184 | * @throws \BrowscapPHP\Exception |
||
185 | */ |
||
186 | 4 | public function convertFile($iniFile) |
|
204 | |||
205 | /** |
||
206 | * reads and parses an ini string and writes the results into the cache |
||
207 | * |
||
208 | * @param string $iniString |
||
209 | */ |
||
210 | 3 | public function convertString($iniString) |
|
217 | |||
218 | /** |
||
219 | * fetches a remote file and stores it into a local folder |
||
220 | * |
||
221 | * @param string $file The name of the file where to store the remote content |
||
222 | * @param string $remoteFile The code for the remote file to load |
||
223 | * |
||
224 | * @throws \BrowscapPHP\Exception\FetcherException |
||
225 | * @throws \BrowscapPHP\Helper\Exception |
||
226 | */ |
||
227 | 3 | public function fetch($file, $remoteFile = IniLoader::PHP_INI) |
|
274 | |||
275 | /** |
||
276 | * fetches a remote file, parses it and writes the result into the cache |
||
277 | * |
||
278 | * if the local stored information are in the same version as the remote data no actions are |
||
279 | * taken |
||
280 | * |
||
281 | * @param string $remoteFile The code for the remote file to load |
||
282 | * |
||
283 | * @throws \BrowscapPHP\Exception\FileNotFoundException |
||
284 | * @throws \BrowscapPHP\Helper\Exception |
||
285 | * @throws \BrowscapPHP\Exception\FetcherException |
||
286 | */ |
||
287 | 2 | public function update($remoteFile = IniLoader::PHP_INI) |
|
326 | |||
327 | /** |
||
328 | * checks if an update on a remote location for the local file or the cache |
||
329 | * |
||
330 | * @throws \BrowscapPHP\Helper\Exception |
||
331 | * @throws \BrowscapPHP\Exception\FetcherException |
||
332 | * @return int|null The actual cached version if a newer version is available, null otherwise |
||
333 | * @return int|null The actual cached version if a newer version is available, null otherwise |
||
334 | */ |
||
335 | 9 | public function checkUpdate() |
|
390 | |||
391 | /** |
||
392 | * @param string $content |
||
393 | * |
||
394 | * @return mixed |
||
395 | */ |
||
396 | 6 | private function sanitizeContent($content) |
|
404 | |||
405 | /** |
||
406 | * reads and parses an ini string and writes the results into the cache |
||
407 | * |
||
408 | * @param \BrowscapPHP\Helper\Converter $converter |
||
409 | * @param string $content |
||
410 | * @param int|null $cachedVersion |
||
411 | */ |
||
412 | 4 | private function storeContent(Converter $converter, $content, $cachedVersion) |
|
423 | } |
||
424 |
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.