| Conditions | 4 |
| Paths | 6 |
| Total Lines | 65 |
| 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 |
||
| 83 | function moveAttachment(&$row, $db, $from_prefix, $attachmentUploadDir) |
||
| 84 | { |
||
| 85 | static $smf_folders = null; |
||
| 86 | |||
| 87 | if ($smf_folders === null) |
||
| 88 | { |
||
| 89 | $request = $db->query(" |
||
| 90 | SELECT value |
||
| 91 | FROM {$from_prefix}settings |
||
| 92 | WHERE variable='attachmentUploadDir';"); |
||
| 93 | list ($smf_attachments_dir) = $db->fetch_row($request); |
||
| 94 | |||
| 95 | $smf_folders = @unserialize($smf_attachments_dir); |
||
| 96 | if (!is_array($smf_folders)) |
||
| 97 | $smf_folders = array(1 => $smf_attachments_dir); |
||
| 98 | } |
||
| 99 | |||
| 100 | // If something is broken, better try to account for it as well. |
||
| 101 | if (isset($row['id_folder']) && isset($smf_folders[$row['id_folder']])) |
||
| 102 | $smf_attachments_dir = $smf_folders[$row['id_folder']]; |
||
| 103 | else |
||
| 104 | $smf_attachments_dir = $smf_folders[1]; |
||
| 105 | |||
| 106 | if (empty($row['file_hash'])) |
||
| 107 | { |
||
| 108 | $row['file_hash'] = createAttachmentFileHash($row['filename']); |
||
| 109 | $source_file = $row['filename']; |
||
| 110 | } |
||
| 111 | else |
||
| 112 | $source_file = $row['id_attach'] . '_' . $row['file_hash']; |
||
| 113 | |||
| 114 | if (empty($row['mime_type'])) |
||
| 115 | { |
||
| 116 | $fileext = ''; |
||
| 117 | $mimetype = ''; |
||
| 118 | $is_thumb = false; |
||
| 119 | |||
| 120 | if (preg_match('/\.(jpg|jpeg|gif|png)(_thumb)?$/i',$row['filename'],$m)) |
||
| 121 | { |
||
| 122 | $fileext = strtolower($m[1]); |
||
| 123 | $is_thumb = !empty($m[2]); |
||
| 124 | |||
| 125 | if (empty($row['mime_type'])) |
||
| 126 | { |
||
| 127 | // AFAIK, all thumbnails got created as PNG |
||
| 128 | if ($is_thumb) $mimetype = 'image/png'; |
||
| 129 | elseif ($fileext == 'jpg') $mimetype = 'image/jpeg'; |
||
| 130 | else $mimetype = 'image/'.$fileext; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | else if (preg_match('/\.([a-z][a-z0-9]*)$/i',$row['filename'],$m)) |
||
| 134 | { |
||
| 135 | $fileext = strtolower($m[1]); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (empty($row['fileext'])) $row['fileext'] = $fileext; |
||
| 139 | |||
| 140 | // try using getimagesize to calculate the mime type, otherwise use the $mimetype set from above |
||
| 141 | $size = @getimagesize($filename); |
||
| 142 | |||
| 143 | $row['mime_type'] = empty($size['mime']) ? $mimetype : $size['mime']; |
||
| 144 | } |
||
| 145 | |||
| 146 | copy_file($smf_attachments_dir . '/' . $source_file, $attachmentUploadDir . '/' . $row['id_attach'] . '_' . $row['file_hash'] . '.elk'); |
||
| 147 | } |