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 |
||
25 | class Pinterest { |
||
26 | |||
27 | /** |
||
28 | * Reference to authentication class instance |
||
29 | * |
||
30 | * @var Auth\PinterestOAuth |
||
31 | */ |
||
32 | public $auth; |
||
33 | |||
34 | /** |
||
35 | * A reference to the request class which travels |
||
36 | * through the application |
||
37 | * |
||
38 | * @var Transport\Request |
||
39 | */ |
||
40 | public $request; |
||
41 | |||
42 | /** |
||
43 | * A array containing the cached endpoints |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $cachedEndpoints = []; |
||
48 | |||
49 | /** |
||
50 | * Constructor |
||
51 | * |
||
52 | * @param string $client_id |
||
53 | * @param string $client_secret |
||
54 | * @param CurlBuilder $curlbuilder |
||
55 | */ |
||
56 | 43 | public function __construct($client_id, $client_secret, $curlbuilder = null) |
|
68 | |||
69 | /** |
||
70 | * Get an Pinterest API endpoint |
||
71 | * |
||
72 | * @access public |
||
73 | * @param string $endpoint |
||
74 | * @return mixed |
||
75 | * @throws Exceptions\InvalidEndpointException |
||
76 | */ |
||
77 | 39 | public function __get($endpoint) |
|
99 | |||
100 | /** |
||
101 | * Get rate limit from the headers |
||
102 | * response header may change from X-Ratelimit-Limit to X-RateLimit-Limit |
||
103 | * @access public |
||
104 | * @return integer |
||
105 | */ |
||
106 | 1 | View Code Duplication | public function getRateLimit() |
114 | |||
115 | /** |
||
116 | * Get rate limit remaining from the headers |
||
117 | * response header may change from X-Ratelimit-Remaining to X-RateLimit-Remaining |
||
118 | * @access public |
||
119 | * @return mixed |
||
120 | */ |
||
121 | 1 | View Code Duplication | public function getRateLimitRemaining() |
129 | } |
||
130 |
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.