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:
Complex classes like ApiController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApiController, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Myth\Api\Server; |
||
44 | class ApiController extends BaseController { |
||
45 | |||
46 | use AuthTrait; |
||
47 | |||
48 | protected $language_file = 'api'; |
||
49 | |||
50 | protected $ajax_notices = false; |
||
51 | |||
52 | /** |
||
53 | * Holds all request parameters. |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $vars = []; |
||
57 | |||
58 | |||
59 | protected $request; |
||
60 | |||
61 | protected $allowed_http_methods = [ |
||
62 | 'get', |
||
63 | 'put', |
||
64 | 'post', |
||
65 | 'delete', |
||
66 | 'options', |
||
67 | 'patch', |
||
68 | 'head' |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * Turns off authorization checks. |
||
73 | * Only intended for temp use in |
||
74 | * development environments. |
||
75 | * @var bool |
||
76 | */ |
||
77 | protected $do_auth_check = true; |
||
78 | |||
79 | /** |
||
80 | * The current page of results being requested. |
||
81 | * @var int |
||
82 | */ |
||
83 | protected $page = 0; |
||
84 | |||
85 | /** |
||
86 | * The number of results to return per page |
||
87 | * of results, by default. |
||
88 | * @var int |
||
89 | */ |
||
90 | protected $per_page = 20; |
||
91 | |||
92 | /** |
||
93 | * Based on the current page, |
||
94 | * used for LIMITing data requests |
||
95 | * from database. |
||
96 | * |
||
97 | * @var int |
||
98 | */ |
||
99 | protected $offset = 0; |
||
100 | |||
101 | /** |
||
102 | * Stores any select values passed to any methods |
||
103 | * via the $_GET var ?fields=x,y,z. |
||
104 | * |
||
105 | * @var null |
||
106 | */ |
||
107 | protected $selects = null; |
||
108 | |||
109 | /** |
||
110 | * The time in microseconds that the request started. |
||
111 | * |
||
112 | * @var null |
||
113 | */ |
||
114 | protected $start_time = null; |
||
115 | |||
116 | /** |
||
117 | * Specifies whether this request should be logged. |
||
118 | * |
||
119 | * @var bool |
||
120 | */ |
||
121 | protected $enable_logging; |
||
122 | |||
123 | /** |
||
124 | * Whether rate limiting is enabled. |
||
125 | * |
||
126 | * @var bool |
||
127 | */ |
||
128 | protected $enable_rate_limits; |
||
129 | |||
130 | /** |
||
131 | * The number of requests allowed per user/hour |
||
132 | * |
||
133 | * @var int |
||
134 | */ |
||
135 | protected $rate_limits = 0; |
||
136 | |||
137 | /** |
||
138 | * Status strings/codes allowed when using |
||
139 | * the generic 'fail' method. |
||
140 | * |
||
141 | * @var array |
||
142 | */ |
||
143 | protected $codes = array( |
||
144 | 'created' => 201, |
||
145 | 'deleted' => 200, |
||
146 | 'invalid_request' => 400, |
||
147 | 'unsupported_response_type' => 400, |
||
148 | 'invalid_scope' => 400, |
||
149 | 'temporarily_unavailable' => 400, |
||
150 | 'invalid_grant' => 400, |
||
151 | 'invalid_credentials' => 400, |
||
152 | 'invalid_refresh' => 400, |
||
153 | 'no_data' => 400, |
||
154 | 'invalid_data' => 400, |
||
155 | 'access_denied' => 401, |
||
156 | 'unauthorized' => 401, |
||
157 | 'invalid_client' => 401, |
||
158 | 'forbidden' => 403, |
||
159 | 'resource_not_found' => 404, |
||
160 | 'not_acceptable' => 406, |
||
161 | 'resource_exists' => 409, |
||
162 | 'resource_gone' => 410, |
||
163 | 'too_many_requests' => 429, |
||
164 | 'server_error' => 500, |
||
165 | 'unsupported_grant_type' => 501, |
||
166 | 'not_implemented' => 501 |
||
167 | ); |
||
168 | |||
169 | /** |
||
170 | * Convert common browser-sent langauge |
||
171 | * strings to a folder name in the languages folder |
||
172 | * that we want to use. |
||
173 | * |
||
174 | * Primarily used for converting to english when |
||
175 | * viewing the API in a browser. |
||
176 | * |
||
177 | * @var array |
||
178 | */ |
||
179 | protected $lang_map = [ |
||
180 | 'en-us' => 'english', |
||
181 | 'en' => 'english', |
||
182 | 'eng' => 'english', |
||
183 | 'en-au' => 'english', |
||
184 | 'en-nz' => 'english', |
||
185 | 'en-za' => 'english', |
||
186 | 'en-tt' => 'english', |
||
187 | 'en-gb' => 'english', |
||
188 | 'en-ca' => 'english', |
||
189 | 'en-ie' => 'english', |
||
190 | 'en-jm' => 'english', |
||
191 | 'en-bz' => 'english', |
||
192 | ]; |
||
193 | |||
194 | /** |
||
195 | * If you wish to override the default authentication |
||
196 | * library used for authentication, set this to the |
||
197 | * fully namespaced class name. |
||
198 | * |
||
199 | * @var string |
||
200 | */ |
||
201 | protected $authenticate_class = '\Myth\Api\Auth\APIAuthentication'; |
||
202 | |||
203 | /** |
||
204 | * The idiom that should be used for the language if |
||
205 | * no specific language is requested in Accept-Language header. |
||
206 | * |
||
207 | * @var string |
||
208 | */ |
||
209 | protected $default_language = 'english'; |
||
210 | |||
211 | //-------------------------------------------------------------------- |
||
212 | |||
213 | public function __construct() |
||
288 | |||
289 | //-------------------------------------------------------------------- |
||
290 | |||
291 | /** |
||
292 | * Responsible for enforcing SSL restrictions. |
||
293 | * |
||
294 | * @param $method |
||
295 | * @param array $arguments |
||
296 | * |
||
297 | * @return mixed |
||
298 | */ |
||
299 | public function _remap($method, $arguments = []) |
||
316 | |||
317 | //-------------------------------------------------------------------- |
||
318 | |||
319 | //-------------------------------------------------------------------- |
||
320 | // Response Methods |
||
321 | //-------------------------------------------------------------------- |
||
322 | |||
323 | /** |
||
324 | * Provides a single, simple method to return an API response, formatted |
||
325 | * as json, with the proper content type and status code. |
||
326 | * |
||
327 | * // todo Allow responses in other formats, like jsonp, html and csv |
||
328 | * |
||
329 | * @param $data |
||
330 | * @param int $status_code |
||
331 | * |
||
332 | * @return mixed |
||
333 | */ |
||
334 | public function respond ($data = null, $status_code = 200) |
||
364 | |||
365 | //-------------------------------------------------------------------- |
||
366 | |||
367 | /** |
||
368 | * Returns a failure code to the end user. Mainly so that we have a simple |
||
369 | * way to return a consistent response format. |
||
370 | * |
||
371 | * @param $description |
||
372 | * @param $status_code |
||
373 | * @param string $error_code |
||
374 | * |
||
375 | * @return mixed |
||
376 | */ |
||
377 | protected function fail ($description, $status_code, $error_code = 'invalid_request') |
||
392 | |||
393 | //-------------------------------------------------------------------- |
||
394 | |||
395 | //-------------------------------------------------------------------- |
||
396 | // Response Helpers |
||
397 | //-------------------------------------------------------------------- |
||
398 | |||
399 | /** |
||
400 | * Used after successfully creating a new resource. |
||
401 | * |
||
402 | * @param $data |
||
403 | * |
||
404 | * @return mixed |
||
405 | */ |
||
406 | protected function respondCreated($data) |
||
410 | |||
411 | //-------------------------------------------------------------------- |
||
412 | |||
413 | /** |
||
414 | * Used when a resource has been successfully deleted. |
||
415 | * |
||
416 | * @param $data |
||
417 | * |
||
418 | * @return mixed |
||
419 | */ |
||
420 | protected function respondDeleted($data) |
||
424 | |||
425 | //-------------------------------------------------------------------- |
||
426 | |||
427 | /** |
||
428 | * Used |
||
429 | * |
||
430 | * @param $description |
||
431 | * |
||
432 | * @return mixed |
||
433 | */ |
||
434 | protected function failUnauthorized($description) |
||
438 | |||
439 | //-------------------------------------------------------------------- |
||
440 | |||
441 | /** |
||
442 | * Used when access to this resource is not allowed. Authorization |
||
443 | * will not help. |
||
444 | * |
||
445 | * @param $description |
||
446 | * |
||
447 | * @return mixed |
||
448 | */ |
||
449 | public function failForbidden($description) |
||
453 | |||
454 | //-------------------------------------------------------------------- |
||
455 | |||
456 | /** |
||
457 | * Used when the resource the request is for cannot be found. |
||
458 | * |
||
459 | * @param $description |
||
460 | * |
||
461 | * @return mixed |
||
462 | */ |
||
463 | protected function failNotFound($description) |
||
467 | |||
468 | //-------------------------------------------------------------------- |
||
469 | |||
470 | /** |
||
471 | * Used for when invalid data is presented to the API. |
||
472 | * |
||
473 | * @param $description |
||
474 | * |
||
475 | * @return mixed |
||
476 | */ |
||
477 | protected function failBadRequest($description) |
||
481 | |||
482 | //-------------------------------------------------------------------- |
||
483 | |||
484 | /** |
||
485 | * Used when the data does not validate. Separate for better |
||
486 | * readability and in case we ever change the response code |
||
487 | * in the future. |
||
488 | * |
||
489 | * @param $description |
||
490 | * |
||
491 | * @return mixed |
||
492 | */ |
||
493 | protected function failValidationError($description) |
||
497 | |||
498 | //-------------------------------------------------------------------- |
||
499 | |||
500 | /** |
||
501 | * Used when trying to create a new resource and it already exists. |
||
502 | * |
||
503 | * @param $description |
||
504 | * |
||
505 | * @return mixed |
||
506 | */ |
||
507 | protected function failResourceExists($description) |
||
511 | |||
512 | //-------------------------------------------------------------------- |
||
513 | |||
514 | /** |
||
515 | * Used when the resource has intentionally been removed already and will not |
||
516 | * be available again. Like when its already been deleted. |
||
517 | * |
||
518 | * @param $description |
||
519 | * |
||
520 | * @return mixed |
||
521 | */ |
||
522 | protected function failResourceGone($description) |
||
526 | |||
527 | //-------------------------------------------------------------------- |
||
528 | |||
529 | /** |
||
530 | * Used when the user has made too many requests against the within |
||
531 | * the last hour. |
||
532 | * |
||
533 | * @param $description |
||
534 | * |
||
535 | * @return mixed |
||
536 | */ |
||
537 | protected function failTooManyRequests($description) |
||
541 | |||
542 | //-------------------------------------------------------------------- |
||
543 | |||
544 | //-------------------------------------------------------------------- |
||
545 | // Utility Methods |
||
546 | //-------------------------------------------------------------------- |
||
547 | |||
548 | /** |
||
549 | * @param $name |
||
550 | * |
||
551 | * @return bool |
||
552 | */ |
||
553 | public function grabVar($name) |
||
557 | |||
558 | //-------------------------------------------------------------------- |
||
559 | |||
560 | /** |
||
561 | * Creates the URL for the next set of results based on the |
||
562 | * 'page' value set in the calling URL. |
||
563 | * |
||
564 | * If $clean_get is TRUE will only include the ?page value on |
||
565 | * the URL, otherwise will include all $_GET values that were |
||
566 | * sent to the URL. |
||
567 | * |
||
568 | * Returns null if this request has had paging turned off, |
||
569 | * via ?page=0. |
||
570 | * |
||
571 | * @param $path |
||
572 | * @param $clean_get |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | View Code Duplication | public function nextURL($path, $clean_get = false) |
|
609 | |||
610 | //-------------------------------------------------------------------- |
||
611 | |||
612 | /** |
||
613 | * Creates the URL for the prev set of results based on the |
||
614 | * 'page' value set in the calling URL. |
||
615 | * |
||
616 | * If $clean_get is TRUE will only include the ?page value on |
||
617 | * the URL, otherwise will include all $_GET values that were |
||
618 | * sent to the URL. |
||
619 | * |
||
620 | * Returns null if this request has had paging turned off, |
||
621 | * via ?page=0. |
||
622 | * |
||
623 | * @param $path |
||
624 | * @param bool $clean_get |
||
625 | * |
||
626 | * @return string |
||
627 | */ |
||
628 | View Code Duplication | public function prevURL ($path, $clean_get = false) |
|
661 | |||
662 | //-------------------------------------------------------------------- |
||
663 | |||
664 | //-------------------------------------------------------------------- |
||
665 | // Internal Methods |
||
666 | //-------------------------------------------------------------------- |
||
667 | |||
668 | /** |
||
669 | * Determines the current page and offset based upon a ?page $_GET var. |
||
670 | * |
||
671 | * The offset value is based on the current $this->per_page value. |
||
672 | * |
||
673 | * A request can set ?page=0 to turn off paging altogether. |
||
674 | */ |
||
675 | protected function detectPage( ) |
||
701 | |||
702 | //-------------------------------------------------------------------- |
||
703 | |||
704 | /** |
||
705 | * Detects the request method and populates the $vars array based on |
||
706 | * the method found. |
||
707 | * |
||
708 | * NOTE that any $_GET vars will have to be accessed by the standard |
||
709 | * methods when the method isn't a GET request. |
||
710 | * |
||
711 | * @return string |
||
712 | */ |
||
713 | protected function detectMethod() |
||
739 | |||
740 | //-------------------------------------------------------------------- |
||
741 | |||
742 | /** |
||
743 | * Detects one or more languages that should the request should be |
||
744 | * returned as. If more than 1 exists, just load the first language |
||
745 | * file. |
||
746 | * |
||
747 | * @return array|mixed|null |
||
748 | */ |
||
749 | protected function detectLanguage() |
||
795 | |||
796 | //-------------------------------------------------------------------- |
||
797 | |||
798 | /** |
||
799 | * Checks for the $_GET key of 'fields' and will store that |
||
800 | * value automatically in $this->selects for use in your own queries. |
||
801 | */ |
||
802 | public function detectFields() |
||
822 | |||
823 | //-------------------------------------------------------------------- |
||
824 | |||
825 | |||
826 | /** |
||
827 | * Takes care of logging the request information to the database. |
||
828 | */ |
||
829 | public function logTime() |
||
848 | |||
849 | //-------------------------------------------------------------------- |
||
850 | |||
851 | /** |
||
852 | * Checks the user's number of requests within the current hour. |
||
853 | * Returns true if they are within their limits and can make additional |
||
854 | * requests. Returns false if they have exceeded the number of requests |
||
855 | * for this hour. |
||
856 | * |
||
857 | * @return bool |
||
858 | */ |
||
859 | private function isWithinLimits() |
||
870 | |||
871 | //-------------------------------------------------------------------- |
||
872 | |||
873 | } |
||
874 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: