| Conditions | 12 |
| Paths | 30 |
| Total Lines | 50 |
| 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 |
||
| 69 | protected function processFile(File $file) { |
||
| 70 | $placeholder = Config::inst()->get('CloudAssets', 'file_placeholder'); |
||
| 71 | |||
| 72 | // 6. Local needs to be wrapped or uploaded -> uploads as normal (via updateCloudStatus) |
||
| 73 | $file->updateCloudStatus(); |
||
| 74 | |||
| 75 | $bucket = $file->getCloudBucket(); |
||
| 76 | if ($bucket && $bucket->hasMethod('getFileSize')) { |
||
| 77 | $size = $bucket->getFileSize($file); |
||
| 78 | |||
| 79 | // if the size matches the placeholder, go ahead and check the actual contents |
||
| 80 | if ($size === strlen($placeholder)) { |
||
| 81 | echo " Remote placeholder!"; |
||
| 82 | $content = $bucket->getContents($file); |
||
| 83 | if ($content === $placeholder) $size = -1; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($size < 0) { |
||
| 87 | if ($file->isLocalMissing()) { |
||
| 88 | // 5. Both remote and local are missing or corrupt -> deletes if thumbnail, flags if not (writes to missing_files.csv) |
||
| 89 | if ($file instanceof CloudImageCached) { |
||
| 90 | echo " Corrupted thumbnail deleted"; |
||
| 91 | $file->delete(); |
||
| 92 | return; |
||
| 93 | } else { |
||
| 94 | if (!isset($this->missing)) $this->missing = fopen(BASE_PATH . '/missing_files.csv', 'a'); |
||
| 95 | fputcsv($this->missing, array(date('Y-m-d H:i:s'), $file->ID, $file->Filename)); |
||
| 96 | echo " Corrupted in both locations!!!"; |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | // 3. Remote file is a placeholder but local in tact -> clears status and re-uploads |
||
| 101 | $file->CloudStatus = 'Local'; |
||
| 102 | $file->updateCloudStatus(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // 1. Local file doesn't exist -> downloads from cloud |
||
| 108 | // 2. Local file is a placeholder but KeepLocal is true -> downloads from cloud |
||
| 109 | $file->createLocalIfNeeded(); |
||
| 110 | |||
| 111 | // 4. Meta data is missing -> restores meta |
||
| 112 | if ($file instanceof CloudImage && !$file->getCloudMeta('Dimensions')) { |
||
| 113 | echo " Missing metadata!"; |
||
| 114 | if ($file->isLocalMissing()) $file->downloadFromCloud(); |
||
| 115 | $file->setCloudMeta('Dimensions', $file->getDimensions('live')); |
||
| 116 | $file->write(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | } |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.