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 |
||
19 | View Code Duplication | class LinkedIn implements ProviderInterface |
|
|
|||
20 | { |
||
21 | const NAME = 'linkedin'; |
||
22 | const SHARE_URL = 'https://www.linkedin.com/shareArticle?%s'; |
||
23 | const API_URL = 'https://www.linkedin.com/countserv/count/share?url=%s&format=json'; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function getName() |
||
32 | |||
33 | /** |
||
34 | * Gets the share link for the URL. |
||
35 | * |
||
36 | * @param array $options This provider supports the following options:<pre> |
||
37 | * title: the title |
||
38 | * summary: the summary |
||
39 | * source: the source |
||
40 | * </pre> |
||
41 | * @param string $url |
||
42 | * @param array $options |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getLink($url, array $options = array()) |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getShares($url) |
||
63 | } |
||
64 |
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.