| Conditions | 12 |
| Paths | 54 |
| Total Lines | 70 |
| 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 |
||
| 136 | private function buildScreenshots(Addon $addon, PackageInterface $package, $path) { |
||
| 137 | $extra = $package->getExtra(); |
||
| 138 | $screenshots = array(); |
||
| 139 | $target = self::SCREENSHOTS_DIR . '/' . $addon->Name; |
||
| 140 | |||
| 141 | if (isset($extra['screenshots'])) { |
||
| 142 | $screenshots = (array) $extra['screenshots']; |
||
| 143 | } elseif (isset($extra['screenshot'])) { |
||
| 144 | $screenshots = (array) $extra['screenshot']; |
||
| 145 | } |
||
| 146 | |||
| 147 | // Delete existing screenshots. |
||
| 148 | foreach ($addon->Screenshots() as $screenshot) { |
||
| 149 | $screenshot->delete(); |
||
| 150 | } |
||
| 151 | |||
| 152 | $addon->Screenshots()->removeAll(); |
||
| 153 | |||
| 154 | foreach ($screenshots as $screenshot) { |
||
| 155 | if (!is_string($screenshot)) { |
||
| 156 | continue; |
||
| 157 | } |
||
| 158 | |||
| 159 | $scheme = parse_url($screenshot, PHP_URL_SCHEME); |
||
| 160 | |||
| 161 | // Handle absolute image URLs. |
||
| 162 | if ($scheme == 'http' || $scheme == 'https') { |
||
| 163 | $temp = TEMP_FOLDER . '/' . md5($screenshot); |
||
| 164 | |||
| 165 | if (!copy($screenshot, $temp)) { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | |||
| 169 | $data = array( |
||
| 170 | 'name' => basename($screenshot), |
||
| 171 | 'size' => filesize($temp), |
||
| 172 | 'tmp_name' => $temp, |
||
| 173 | 'error' => 0 |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | // Handle images that are included in the repository. |
||
| 177 | else { |
||
| 178 | $source = $path . '/' . ltrim($screenshot, '/'); |
||
| 179 | |||
| 180 | // Prevent directory traversal. |
||
| 181 | if ($source != realpath($source)) { |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | if (!file_exists($source)) { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | $data = array( |
||
| 190 | 'name' => basename($source), |
||
| 191 | 'size' => filesize($source), |
||
| 192 | 'tmp_name' => $source, |
||
| 193 | 'error' => 0 |
||
| 194 | ); |
||
| 195 | } |
||
| 196 | |||
| 197 | $upload = new Upload(); |
||
| 198 | $upload->setValidator(new AddonBuilderScreenshotValidator()); |
||
| 199 | $upload->load($data, $target); |
||
| 200 | |||
| 201 | if($file = $upload->getFile()) { |
||
| 202 | $addon->Screenshots()->add($file); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 208 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.