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 |
||
29 | class Congress extends BaseAPI |
||
30 | { |
||
31 | |||
32 | const BASE_URI = 'https://congress.api.sunlightfoundation.com/'; |
||
33 | |||
34 | /** |
||
35 | * Create Congress API instance |
||
36 | * |
||
37 | * @param array $options api options |
||
38 | */ |
||
39 | public function __construct($options = []) |
||
43 | |||
44 | /** |
||
45 | * Create Guzzle Client |
||
46 | * |
||
47 | * @param string $key Sunlight Foundation api key |
||
48 | * |
||
49 | * @return null |
||
50 | */ |
||
51 | View Code Duplication | public function setClient($key) |
|
62 | |||
63 | /** |
||
64 | * Return Congress API Guzzle Client |
||
65 | * |
||
66 | * @return GuzzleHTTP\Client Guzzle Client |
||
67 | */ |
||
68 | public function getClient() |
||
72 | |||
73 | /** |
||
74 | * Search Congress API legislators |
||
75 | * |
||
76 | * @param array $search search fields |
||
77 | * @param array $fields display fields |
||
78 | * |
||
79 | * @return Response API Response |
||
80 | */ |
||
81 | View Code Duplication | public function legislators($search = [], $fields = []) |
|
94 | |||
95 | /** |
||
96 | * Find reps for a given zip |
||
97 | * |
||
98 | * @param int $zip zip code |
||
99 | * @param array $fields display fields |
||
100 | * |
||
101 | * @return Response API Response |
||
102 | */ |
||
103 | public function locateByZip($zip, $fields = []) |
||
108 | |||
109 | /** |
||
110 | * Find reps for a given lat/lng |
||
111 | * |
||
112 | * @param float $lat latitude |
||
113 | * @param float $lng longitude |
||
114 | * @param array $fields display fields |
||
115 | * |
||
116 | * @return Response API Response |
||
117 | */ |
||
118 | public function locateByGeo($lat, $lng, $fields = []) |
||
123 | } |
||
124 |
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.