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 |
||
20 | View Code Duplication | class HttpsAuth implements SubscriberInterface |
|
|
|||
21 | { |
||
22 | const AUTH_NAME = 'gigya'; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $apiKey; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $secret; |
||
33 | |||
34 | /** |
||
35 | * @var null|string |
||
36 | */ |
||
37 | private $userKey; |
||
38 | |||
39 | /** |
||
40 | * @param string $apiKey |
||
41 | * @param string $secret |
||
42 | * @param string|null $userKey |
||
43 | */ |
||
44 | 55 | public function __construct($apiKey, $secret, $userKey = null) |
|
50 | |||
51 | /** |
||
52 | * Returns an array of event names this subscriber wants to listen to. |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | 8 | public function getEvents() |
|
60 | |||
61 | /** |
||
62 | * Add the authentication params to the request. |
||
63 | * |
||
64 | * @param BeforeEvent $event |
||
65 | */ |
||
66 | 11 | public function sign(BeforeEvent $event) |
|
67 | { |
||
68 | 11 | $request = $event->getRequest(); |
|
69 | 11 | if ($request->getScheme() == 'https' && $request->getConfig()->get('auth') == static::AUTH_NAME) { |
|
70 | 9 | $query = $request->getQuery(); |
|
71 | 9 | $query['apiKey'] = $this->apiKey; |
|
72 | 9 | $query['secret'] = $this->secret; |
|
73 | 9 | if ((bool) $this->userKey) { |
|
74 | 2 | $query['userKey'] = $this->userKey; |
|
75 | } |
||
76 | } |
||
77 | 11 | } |
|
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | 39 | public function getApiKey() |
|
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | 39 | public function getSecret() |
|
94 | |||
95 | /** |
||
96 | * @return string|null |
||
97 | */ |
||
98 | 39 | public function getUserKey() |
|
102 | } |
||
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.