| 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 |
||
| 116 | private function buildScreenshots(Addon $addon, PackageInterface $package, $path) { |
||
| 117 | $extra = $package->getExtra(); |
||
| 118 | $screenshots = array(); |
||
| 119 | $target = self::SCREENSHOTS_DIR . '/' . $addon->Name; |
||
| 120 | |||
| 121 | if (isset($extra['screenshots'])) { |
||
| 122 | $screenshots = (array) $extra['screenshots']; |
||
| 123 | } elseif (isset($extra['screenshot'])) { |
||
| 124 | $screenshots = (array) $extra['screenshot']; |
||
| 125 | } |
||
| 126 | |||
| 127 | // Delete existing screenshots. |
||
| 128 | foreach ($addon->Screenshots() as $screenshot) { |
||
| 129 | $screenshot->delete(); |
||
| 130 | } |
||
| 131 | |||
| 132 | $addon->Screenshots()->removeAll(); |
||
| 133 | |||
| 134 | foreach ($screenshots as $screenshot) { |
||
| 135 | if (!is_string($screenshot)) { |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | |||
| 139 | $scheme = parse_url($screenshot, PHP_URL_SCHEME); |
||
| 140 | |||
| 141 | // Handle absolute image URLs. |
||
| 142 | if ($scheme == 'http' || $scheme == 'https') { |
||
| 143 | $temp = TEMP_FOLDER . '/' . md5($screenshot); |
||
| 144 | |||
| 145 | if (!copy($screenshot, $temp)) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | $data = array( |
||
| 150 | 'name' => basename($screenshot), |
||
| 151 | 'size' => filesize($temp), |
||
| 152 | 'tmp_name' => $temp, |
||
| 153 | 'error' => 0 |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | // Handle images that are included in the repository. |
||
| 157 | else { |
||
| 158 | $source = $path . '/' . ltrim($screenshot, '/'); |
||
| 159 | |||
| 160 | // Prevent directory traversal. |
||
| 161 | if ($source != realpath($source)) { |
||
| 162 | continue; |
||
| 163 | } |
||
| 164 | |||
| 165 | if (!file_exists($source)) { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | |||
| 169 | $data = array( |
||
| 170 | 'name' => basename($source), |
||
| 171 | 'size' => filesize($source), |
||
| 172 | 'tmp_name' => $source, |
||
| 173 | 'error' => 0 |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | $upload = new Upload(); |
||
| 178 | $upload->setValidator(new AddonBuilderScreenshotValidator()); |
||
| 179 | $upload->load($data, $target); |
||
| 180 | |||
| 181 | if($file = $upload->getFile()) { |
||
| 182 | $addon->Screenshots()->add($file); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 188 |
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.