| Conditions | 8 |
| Paths | 24 |
| Total Lines | 52 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 41 | public static function main(string $imgFileName): PBXApiResult |
||
| 42 | { |
||
| 43 | clearstatcache(); |
||
| 44 | $res = new PBXApiResult(); |
||
| 45 | $res->processor = __METHOD__; |
||
| 46 | $res->success = true; |
||
| 47 | |||
| 48 | $firmwareDirTmp = dirname($imgFileName); |
||
| 49 | $progress_file = $firmwareDirTmp . '/progress'; |
||
| 50 | |||
| 51 | // Wait until a download process started |
||
| 52 | $d_pid = Processes::getPidOfProcess("{$firmwareDirTmp}/download_settings.json"); |
||
| 53 | if (empty($d_pid)) { |
||
| 54 | usleep(500000); |
||
| 55 | } |
||
| 56 | $error = ''; |
||
| 57 | if (file_exists("{$firmwareDirTmp}/error")) { |
||
| 58 | $error = trim(file_get_contents("{$firmwareDirTmp}/error")); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!file_exists($progress_file)) { |
||
| 62 | $res->data[FilesConstants::D_STATUS_PROGRESS] = '0'; |
||
| 63 | $res->messages[] = FilesConstants::STATUS_NOT_FOUND; |
||
| 64 | $res->success = false; |
||
| 65 | } elseif ('' !== $error) { |
||
| 66 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_ERROR; |
||
| 67 | $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file); |
||
| 68 | $res->messages[] = file_get_contents("{$firmwareDirTmp}/error"); |
||
| 69 | $res->success = false; |
||
| 70 | } elseif ('100' === file_get_contents($progress_file)) { |
||
| 71 | $res->data[FilesConstants::D_STATUS_PROGRESS] = '100'; |
||
| 72 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_COMPLETE; |
||
| 73 | $res->data[FilesConstants::FILE_PATH] = $imgFileName; |
||
| 74 | $res->success = true; |
||
| 75 | } else { |
||
| 76 | $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file); |
||
| 77 | $d_pid = Processes::getPidOfProcess("{$firmwareDirTmp}/download_settings.json"); |
||
| 78 | if (empty($d_pid)) { |
||
| 79 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_ERROR; |
||
| 80 | if (file_exists("{$firmwareDirTmp}/error")) { |
||
| 81 | $res->messages[] = file_get_contents("{$firmwareDirTmp}/error"); |
||
| 82 | } else { |
||
| 83 | $res->messages[] = "Download process interrupted at {$res->data['d_status_progress']}%"; |
||
| 84 | } |
||
| 85 | $res->success = false; |
||
| 86 | } else { |
||
| 87 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_IN_PROGRESS; |
||
| 88 | $res->success = true; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | return $res; |
||
| 93 | } |
||
| 94 | } |