Conditions | 21 |
Paths | 129 |
Total Lines | 117 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
96 | function track(): array |
||
97 | { |
||
98 | $result = []; |
||
99 | |||
100 | global $plugin; |
||
101 | global $courseCode; |
||
102 | global $userId; |
||
103 | global $docId; |
||
104 | global $groupId; |
||
105 | global $sessionId; |
||
106 | global $courseInfo; |
||
107 | |||
108 | if (($body_stream = file_get_contents("php://input")) === false) { |
||
109 | $result["error"] = "Bad Request"; |
||
110 | return $result; |
||
111 | } |
||
112 | |||
113 | $data = json_decode($body_stream, true); |
||
114 | |||
115 | if ($data === null) { |
||
116 | $result["error"] = "Bad Response"; |
||
117 | return $result; |
||
118 | } |
||
119 | |||
120 | if (!empty($plugin->get("jwt_secret"))) { |
||
121 | |||
122 | if (!empty($data["token"])) { |
||
123 | try { |
||
124 | $payload = \Firebase\JWT\JWT::decode($data["token"], $plugin->get("jwt_secret"), array("HS256")); |
||
125 | } catch (\UnexpectedValueException $e) { |
||
126 | $result["status"] = "error"; |
||
127 | $result["error"] = "403 Access denied"; |
||
128 | return $result; |
||
129 | } |
||
130 | } else { |
||
131 | $token = substr($_SERVER[AppConfig::JwtHeader()], strlen("Bearer ")); |
||
132 | try { |
||
133 | $decodeToken = \Firebase\JWT\JWT::decode($token, $plugin->get("jwt_secret"), array("HS256")); |
||
134 | $payload = $decodeToken->payload; |
||
135 | } catch (\UnexpectedValueException $e) { |
||
136 | $result["status"] = "error"; |
||
137 | $result["error"] = "403 Access denied"; |
||
138 | return $result; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | $data["url"] = isset($payload->url) ? $payload->url : null; |
||
143 | $data["status"] = $payload->status; |
||
144 | } |
||
145 | |||
146 | $status = $data["status"]; |
||
147 | |||
148 | $track_result = 1; |
||
149 | switch ($status) { |
||
150 | case TrackerStatus_MustSave: |
||
151 | case TrackerStatus_Corrupted: |
||
152 | |||
153 | $downloadUri = $data["url"]; |
||
154 | |||
155 | if (!empty($docId) && !empty($courseCode)) { |
||
156 | $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode, false, $sessionId); |
||
157 | |||
158 | if ($docInfo === false) { |
||
159 | $result["error"] = "File not found"; |
||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | $filePath = $docInfo["absolute_path"]; |
||
164 | } else { |
||
165 | $result["error"] = "Bad Request"; |
||
166 | return $result; |
||
167 | } |
||
168 | |||
169 | list ($isAllowToEdit, $isMyDir, $isGroupAccess, $isReadonly) = getPermissions($docInfo, $userId, $courseCode, $groupId, $sessionId); |
||
170 | |||
171 | if ($isReadonly) { |
||
172 | break; |
||
173 | } |
||
174 | |||
175 | if (($new_data = file_get_contents($downloadUri)) === false) { |
||
176 | break; |
||
177 | } |
||
178 | |||
179 | if ($isAllowToEdit || $isMyDir || $isGroupAccess) { |
||
180 | $groupInfo = GroupManager::get_group_properties($groupId); |
||
181 | |||
182 | if ($fp = @fopen($filePath, "w")) { |
||
183 | fputs($fp, $new_data); |
||
184 | fclose($fp); |
||
185 | api_item_property_update($courseInfo, |
||
186 | TOOL_DOCUMENT, |
||
187 | $docId, |
||
188 | "DocumentUpdated", |
||
189 | $userId, |
||
190 | $groupInfo, |
||
191 | null, |
||
192 | null, |
||
193 | null, |
||
194 | $sessionId); |
||
195 | update_existing_document($courseInfo, |
||
196 | $docId, |
||
197 | filesize($filePath), |
||
198 | false); |
||
199 | $track_result = 0; |
||
200 | break; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | case TrackerStatus_Editing: |
||
205 | case TrackerStatus_Closed: |
||
206 | |||
207 | $track_result = 0; |
||
208 | break; |
||
209 | } |
||
210 | |||
211 | $result["error"] = $track_result; |
||
212 | return $result; |
||
213 | } |
||
281 |
If you suppress an error, we recommend checking for the error condition explicitly: