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 |
||
14 | class BlacklistHelper implements IBlacklistHelper |
||
15 | { |
||
16 | /** @var HttpHelper */ |
||
17 | private $httpHelper; |
||
18 | /** |
||
19 | * Cache of previously requested usernames |
||
20 | * @var array |
||
21 | */ |
||
22 | private $cache = array(); |
||
23 | /** @var string */ |
||
24 | private $mediawikiWebServiceEndpoint; |
||
25 | |||
26 | /** |
||
27 | * BlacklistHelper constructor. |
||
28 | * |
||
29 | * @param HttpHelper $httpHelper |
||
30 | * @param string $mediawikiWebServiceEndpoint |
||
31 | */ |
||
32 | 4 | public function __construct(HttpHelper $httpHelper, $mediawikiWebServiceEndpoint) |
|
37 | |||
38 | /** |
||
39 | * Returns a value indicating whether the provided username is blacklisted by the on-wiki title blacklist |
||
40 | * |
||
41 | * @param string $username |
||
42 | * |
||
43 | * @return false|string False if the username is not blacklisted, else the blacklist entry. |
||
44 | */ |
||
45 | 4 | public function isBlacklisted($username) |
|
76 | |||
77 | /** |
||
78 | * Performs a fetch to MediaWiki for the relevant title blacklist entry |
||
79 | * |
||
80 | * @param string $username The username to look up |
||
81 | * |
||
82 | * @return array |
||
83 | * @throws CurlException |
||
84 | */ |
||
85 | 4 | private function performWikiLookup($username) |
|
103 | } |
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.