| Conditions | 18 |
| Paths | 640 |
| Total Lines | 83 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 30 | function show($api = null) { |
||
| 31 | $path = func_get_args(); |
||
| 32 | |||
| 33 | if ($api == "api") { |
||
| 34 | $path = array_slice($path, 1); |
||
| 35 | } |
||
| 36 | |||
| 37 | $file_id = $this->jugaad_model->get_path_id($path); |
||
| 38 | $file_type = $this->jugaad_model->get_file_type($file_id); |
||
| 39 | |||
| 40 | if (end($path) == 'index') { |
||
| 41 | $this->http->redirect( |
||
| 42 | locale_base_url() . implode("/", array_slice($path, 0, -1)) . "/" |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | |||
| 46 | // Check if index exists |
||
| 47 | if ($file_type == 'directory') { |
||
| 48 | $file_id = $this->jugaad_model->get_slug_id($file_id, 'index'); |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($file_id === false) { |
||
| 52 | $this->http->response_code(404); |
||
| 53 | } |
||
| 54 | |||
| 55 | $file = $this->jugaad_model->get_file($file_id); |
||
| 56 | |||
| 57 | $file["user_can"] = $this->perms_model->get_permissions($file_id, $this->user); |
||
| 58 | |||
| 59 | if (!$file["user_can"]["read_file"]) { |
||
| 60 | $this->http->response_code(404); |
||
| 61 | } |
||
| 62 | |||
| 63 | $template_meta = $this->template_model->get_meta($file["template"]); |
||
| 64 | if ($template_meta === false) { |
||
| 65 | $this->http->response_code(404); |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($api == "api") { |
||
| 69 | foreach ($template_meta as $name => $meta) { |
||
| 70 | if (isset($meta["inApi"]) && $meta["inApi"] === false) { |
||
| 71 | unset($template_meta[$name]); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $latest_version = $this->jugaad_model->get_latest_version_id_with_external_data($file_id, $template_meta, $this->user); |
||
| 76 | |||
| 77 | if (!empty($_GET["prev_id"]) && $_GET["prev_id"] == $latest_version) { |
||
| 78 | $this->http->response_code(304, false); |
||
| 79 | exit(); |
||
| 80 | } |
||
| 81 | |||
| 82 | $data = $this->jugaad_model->get_file_data($file_id, $template_meta, $this->user, true); |
||
| 83 | |||
| 84 | header('Content-Type: application/json; charset=UTF-8'); |
||
| 85 | echo json_encode([ |
||
| 86 | "version_id" => $latest_version, |
||
| 87 | "page_data" => $data |
||
| 88 | ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
| 89 | } else { |
||
| 90 | $header = getallheaders(); |
||
| 91 | $is_ajax = isset($header["X-Ajax-Request"]) && $header["X-Ajax-Request"]; |
||
| 92 | |||
| 93 | if ($is_ajax) { |
||
| 94 | foreach ($template_meta as $name => $meta) { |
||
| 95 | if (isset($meta["inAjax"]) && $meta["inAjax"] === false) { |
||
| 96 | unset($template_meta[$name]); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $data = $this->jugaad_model->get_file_data($file_id, $template_meta, $this->user, true); |
||
| 102 | |||
| 103 | $data["is_ajax"] = $is_ajax; |
||
| 104 | |||
| 105 | $data["is_authenticated"] = $this->auth->is_authenticated(); |
||
| 106 | $data["user_nick"] = $this->auth->get_user(); |
||
| 107 | |||
| 108 | $view_name = $this->template_model->get_view_name($file["template"]); |
||
| 109 | |||
| 110 | $data["page_slug"] = implode('__', $path); |
||
| 111 | |||
| 112 | $this->load_view($view_name, $data); |
||
| 113 | } |
||
| 176 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.