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 |
||
11 | View Code Duplication | class YahooController extends Controller |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * Instance of Guzzle Client |
||
15 | * @var object |
||
16 | */ |
||
17 | protected $client; |
||
18 | |||
19 | /** |
||
20 | * BaseUrl |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $baseUrl; |
||
24 | |||
25 | /** |
||
26 | * Initialize the Controller with necessary arguments |
||
27 | */ |
||
28 | public function __construct() |
||
38 | |||
39 | /** |
||
40 | * Get the response from Yahoo API |
||
41 | * @param string $relativeUrl |
||
42 | */ |
||
43 | private function setGetResponse($relativeUrl) |
||
47 | |||
48 | /** |
||
49 | * Get the whole response from a get operation |
||
50 | * @return array |
||
51 | */ |
||
52 | private function getResponse() |
||
56 | |||
57 | /** |
||
58 | * Get the data response from a get operation |
||
59 | * @return array |
||
60 | */ |
||
61 | private function getData() |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Return all data to the Yahoo API dashboard |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function getPage() |
||
77 | |||
78 | } |
||
79 |
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.