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 |
||
21 | abstract class AbstractGithubObject |
||
22 | { |
||
23 | /** |
||
24 | * @var Registry Options for the GitHub object. |
||
25 | * @since 1.0 |
||
26 | */ |
||
27 | protected $options; |
||
28 | |||
29 | /** |
||
30 | * @var Http The HTTP client object to use in sending HTTP requests. |
||
31 | * @since 1.0 |
||
32 | */ |
||
33 | protected $client; |
||
34 | |||
35 | /** |
||
36 | * @var string The package the object resides in |
||
37 | * @since 1.0 |
||
38 | */ |
||
39 | protected $package = ''; |
||
40 | |||
41 | /** |
||
42 | * Array containing the allowed hook events |
||
43 | * |
||
44 | * @var array |
||
45 | * @since 1.5.2 |
||
46 | * @see https://developer.github.com/webhooks/#events |
||
47 | * @note From 1.4.0 to 1.5.1 this was named $events, it was renamed due to naming conflicts with package subclasses |
||
48 | */ |
||
49 | protected $hookEvents = array( |
||
50 | '*', |
||
51 | 'commit_comment', |
||
52 | 'create', |
||
53 | 'delete', |
||
54 | 'deployment', |
||
55 | 'deployment_status', |
||
56 | 'fork', |
||
57 | 'gollum', |
||
58 | 'issue_comment', |
||
59 | 'issues', |
||
60 | 'member', |
||
61 | 'membership', |
||
62 | 'page_build', |
||
63 | 'public', |
||
64 | 'pull_request_review_comment', |
||
65 | 'pull_request', |
||
66 | 'push', |
||
67 | 'repository', |
||
68 | 'release', |
||
69 | 'status', |
||
70 | 'team_add', |
||
71 | 'watch', |
||
72 | ); |
||
73 | |||
74 | /** |
||
75 | * Constructor. |
||
76 | * |
||
77 | * @param Registry $options GitHub options object. |
||
78 | * @param Http $client The HTTP client object. |
||
79 | * |
||
80 | * @since 1.0 |
||
81 | */ |
||
82 | public function __construct(Registry $options = null, Http $client = null) |
||
90 | |||
91 | /** |
||
92 | * Method to build and return a full request URL for the request. This method will |
||
93 | * add appropriate pagination details if necessary and also prepend the API url |
||
94 | * to have a complete URL for the request. |
||
95 | * |
||
96 | * @param string $path URL to inflect |
||
97 | * @param integer $page Page to request |
||
98 | * @param integer $limit Number of results to return per page |
||
99 | * |
||
100 | * @return string The request URL. |
||
101 | * |
||
102 | * @since 1.0 |
||
103 | */ |
||
104 | protected function fetchUrl($path, $page = 0, $limit = 0) |
||
142 | |||
143 | /** |
||
144 | * Process the response and decode it. |
||
145 | * |
||
146 | * @param Response $response The response. |
||
147 | * @param integer $expectedCode The expected "good" code. |
||
148 | * |
||
149 | * @return mixed |
||
150 | * |
||
151 | * @since 1.0 |
||
152 | * @throws UnexpectedResponseException |
||
153 | */ |
||
154 | protected function processResponse(Response $response, $expectedCode = 200) |
||
167 | } |
||
168 |
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.