| Conditions | 8 |
| Paths | 13 |
| Total Lines | 65 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 4 | public function call() |
||
| 5 | {
|
||
| 6 | global $theme; |
||
| 7 | if(User::isLoggedIn()) |
||
| 8 | {
|
||
| 9 | $this->next->call(); |
||
| 10 | return; |
||
| 11 | } |
||
| 12 | |||
| 13 | $pageURL = $this->app->request()->getResourceUri(); |
||
| 14 | $md5 = md5($pageURL . $theme); |
||
| 15 | $fileCache = new FileCache(); |
||
| 16 | |||
| 17 | // Pages to cache |
||
| 18 | $pages = array( |
||
| 19 | "/kill/" => 60*60*24, // Kill detail pages can easily be cached for many hours since they rarely change.. |
||
| 20 | "/system/" => 60*60, // 1 hour cache on the system page |
||
| 21 | "/region/" => 60*60, // 1 hour cache on the region page |
||
| 22 | "/group/" => 60*60, // 1 hour cache on the groups view |
||
| 23 | "/ship/" => 60*15, // 15 minutes on ship view |
||
| 24 | "/character/" => 60*5, // 5 minutes on character view |
||
| 25 | "/corporation/" => 60*5, // 5 minutes on corporation view |
||
| 26 | "/alliance/" => 60*5, // 5 minutes on alliance view |
||
| 27 | "/faction/" => 60*15, // 15 minutes on faction view |
||
| 28 | "/item/" => 60*60*24, // 1 Day on item view, shit barely changes.. |
||
| 29 | "/information/" => 60*5, // 5 minutes on information, tho it's probably not needed tbh |
||
| 30 | ); |
||
| 31 | |||
| 32 | // Default cachetime is 0seconds |
||
| 33 | $cacheTime = 0; |
||
| 34 | foreach($pages as $page => $cTime) |
||
| 35 | {
|
||
| 36 | if(stristr($pageURL, $page)) // Only do shit on the kill page for now... Shou |
||
| 37 | {
|
||
| 38 | $cacheTime = $cTime; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | // Default headers |
||
| 43 | header("X-PageCache: false");
|
||
| 44 | header("X-PageCache-Time: {$cacheTime}");
|
||
| 45 | |||
| 46 | // Application response |
||
| 47 | $rsp = $this->app->response(); |
||
| 48 | |||
| 49 | // Get the cached result if it exists |
||
| 50 | $data = $fileCache->get($md5); |
||
| 51 | if (isset($data) && !empty($data["body"])) |
||
| 52 | {
|
||
| 53 | // cache hit... return the cached content |
||
| 54 | header("X-PageCache: true");
|
||
| 55 | $rsp["Content-Type"] = $data["content_type"]; |
||
| 56 | $rsp->body($data["body"]); |
||
|
|
|||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | // cache miss... continue on to generate the page |
||
| 61 | $this->next->call(); |
||
| 62 | |||
| 63 | // cache result for future look up |
||
| 64 | if ($rsp->status() == 200) {
|
||
| 65 | if($cacheTime > 0) |
||
| 66 | $fileCache->set($md5, array("key" => $pageURL, "content_type" => $rsp["Content-Type"], "body" => $rsp->body()), $cacheTime);
|
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.