Conditions | 60 |
Paths | > 20000 |
Total Lines | 276 |
Code Lines | 129 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 2 |
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 |
||
104 | public function compare(Project $project) |
||
105 | { |
||
106 | // If the project status is marked as something bad, there's nothing else |
||
107 | // to consider. |
||
108 | if ($this->getProjectStatus()) { |
||
109 | switch ($this->getProjectStatus()) { |
||
110 | case 'insecure': |
||
111 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
112 | break; |
||
113 | case 'unpublished': |
||
114 | case 'revoked': |
||
115 | $project->setStatus(self::UPDATE_REVOKED); |
||
116 | break; |
||
117 | case 'unsupported': |
||
118 | $project->setStatus(self::UPDATE_NOT_SUPPORTED); |
||
119 | break; |
||
120 | case 'not-fetched': |
||
121 | $project->setStatus(self::UPDATE_NOT_FETCHED); |
||
122 | break; |
||
123 | |||
124 | default: |
||
125 | // Assume anything else (e.g. 'published') is valid and we should |
||
126 | // perform the rest of the logic in this function. |
||
127 | break; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | if ($project->getStatus()) { |
||
132 | // We already know the status for this project, so there's nothing else to |
||
133 | // compute. Record the project status into $project_data and we're done. |
||
134 | $project->setProjectStatus($this->getProjectStatus()); |
||
135 | |||
136 | return; |
||
137 | } |
||
138 | |||
139 | // Figure out the target major version. |
||
140 | $existing_major = $project->getExistingMajor(); |
||
141 | $supported_majors = array(); |
||
142 | if ($this->getSupportedMajors()) { |
||
143 | $supported_majors = explode(',', $this->getSupportedMajors()); |
||
144 | } elseif ($this->getDefaultMajor()) { |
||
145 | // Older release history XML file without supported or recommended. |
||
146 | $supported_majors[] = $this->getDefaultMajor(); |
||
147 | } |
||
148 | |||
149 | if (in_array($existing_major, $supported_majors)) { |
||
150 | // Still supported, stay at the current major version. |
||
151 | $target_major = $existing_major; |
||
152 | } elseif ($this->getRecommendedMajor()) { |
||
153 | // Since 'recommended_major' is defined, we know this is the new XML |
||
154 | // format. Therefore, we know the current release is unsupported since |
||
155 | // its major version was not in the 'supported_majors' list. We should |
||
156 | // find the best release from the recommended major version. |
||
157 | $target_major = $this->getRecommendedMajor(); |
||
158 | $project->setStatus(self::UPDATE_NOT_SUPPORTED); |
||
159 | } elseif ($this->getDefaultMajor()) { |
||
160 | // Older release history XML file without recommended, so recommend |
||
161 | // the currently defined "default_major" version. |
||
162 | $target_major = $this->getDefaultMajor(); |
||
163 | } else { |
||
164 | // Malformed XML file? Stick with the current version. |
||
165 | $target_major = $existing_major; |
||
166 | } |
||
167 | |||
168 | // Make sure we never tell the admin to downgrade. If we recommended an |
||
169 | // earlier version than the one they're running, they'd face an |
||
170 | // impossible data migration problem, since Drupal never supports a DB |
||
171 | // downgrade path. In the unfortunate case that what they're running is |
||
172 | // unsupported, and there's nothing newer for them to upgrade to, we |
||
173 | // can't print out a "Recommended version", but just have to tell them |
||
174 | // what they have is unsupported and let them figure it out. |
||
175 | $target_major = max($existing_major, $target_major); |
||
176 | |||
177 | $release_patch_changed = null; |
||
178 | $patch = ''; |
||
179 | |||
180 | // If the project is marked as UPDATE_FETCH_PENDING, it means that the |
||
181 | // data we currently have (if any) is stale, and we've got a task queued |
||
182 | // up to (re)fetch the data. In that case, we mark it as such, merge in |
||
183 | // whatever data we have (e.g. project title and link), and move on. |
||
184 | if ($this->getFetchStatus() == self::UPDATE_FETCH_PENDING) { |
||
185 | $project->setStatus(self::UPDATE_FETCH_PENDING); |
||
186 | $project->setReason('No available update data'); |
||
187 | $project->setFetchStatus($this->getFetchStatus()); |
||
188 | |||
189 | return; |
||
190 | } |
||
191 | |||
192 | // Defend ourselves from XML history files that contain no releases. |
||
193 | if (!$this->getReleases()) { |
||
194 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
195 | $project->setReason('No available releases found'); |
||
196 | |||
197 | return; |
||
198 | } |
||
199 | |||
200 | foreach ($this->getReleases() as $version => $release) { |
||
201 | // First, if this is the existing release, check a few conditions. |
||
202 | if ($project->getExistingVersion() == $version) { |
||
203 | if ($release->hasTerm('Release type') && |
||
204 | in_array('Insecure', $release->getTerm('Release type')) |
||
205 | ) { |
||
206 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
207 | } elseif ($release->getStatus() == 'unpublished') { |
||
208 | $project->setStatus(self::UPDATE_REVOKED); |
||
209 | } elseif ($release->hasTerm('Release type') && |
||
210 | in_array('Unsupported', $release->getTerm('Release type')) |
||
211 | ) { |
||
212 | $project->setStatus(self::UPDATE_NOT_SUPPORTED); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | // Otherwise, ignore unpublished, insecure, or unsupported releases. |
||
217 | if ($release->getStatus() == 'unpublished' || |
||
218 | ($release->hasTerm('Release type') && |
||
219 | (in_array('Insecure', $release->getTerm('Release type')) || |
||
220 | in_array('Unsupported', $release->getTerm('Release type')))) |
||
221 | ) { |
||
222 | continue; |
||
223 | } |
||
224 | |||
225 | // See if this is a higher major version than our target and yet still |
||
226 | // supported. If so, record it as an "Also available" release. |
||
227 | // Note: some projects have a HEAD release from CVS days, which could |
||
228 | // be one of those being compared. They would not have version_major |
||
229 | // set, so we must call isset first. |
||
230 | if ($release->getVersionMajor() > $target_major) { |
||
231 | // if (in_array($release['version_major'], $supported_majors)) { |
||
|
|||
232 | // if (!isset($project_data['also'][$release['version_major']])) { |
||
233 | // $project_data['also'][$release['version_major']] = $version; |
||
234 | // $project_data['releases'][$version] = $release; |
||
235 | // |
||
236 | // $project->addAlsoAvailable($version, $release); |
||
237 | // } |
||
238 | // } |
||
239 | // Otherwise, this release can't matter to us, since it's neither |
||
240 | // from the release series we're currently using nor the recommended |
||
241 | // release. We don't even care about security updates for this |
||
242 | // branch, since if a project maintainer puts out a security release |
||
243 | // at a higher major version and not at the lower major version, |
||
244 | // they must remove the lower version from the supported major |
||
245 | // versions at the same time, in which case we won't hit this code. |
||
246 | continue; |
||
247 | } |
||
248 | |||
249 | // Look for the 'latest version' if we haven't found it yet. Latest is |
||
250 | // defined as the most recent version for the target major version. |
||
251 | if (!$project->getLatestVersion() && $release->getVersionMajor() == $target_major) { |
||
252 | $project->setLatestVersion($version); |
||
253 | $project->setRelease($version, $release); |
||
254 | } |
||
255 | |||
256 | // Look for the development snapshot release for this branch. |
||
257 | if (!$project->getDevVersion() |
||
258 | && $release->getVersionMajor() == $target_major |
||
259 | && $release->getVersionExtra() == Project::INSTALL_TYPE_DEV |
||
260 | ) { |
||
261 | $project->setDevVersion($version); |
||
262 | $project->setRelease($version, $release); |
||
263 | } |
||
264 | |||
265 | // Look for the 'recommended' version if we haven't found it yet (see |
||
266 | // phpdoc at the top of this function for the definition). |
||
267 | if (!$project->getRecommended() |
||
268 | && $release->getVersionMajor() == $target_major |
||
269 | && $release->getVersionPatch() |
||
270 | ) { |
||
271 | if ($patch != $release->getVersionPatch()) { |
||
272 | $patch = $release->getVersionPatch(); |
||
273 | $release_patch_changed = $release; |
||
274 | } |
||
275 | if (!$release->getVersionExtra() && $patch == $release->getVersionPatch()) { |
||
276 | $project->setRecommended($release_patch_changed->getVersion()); |
||
277 | if ($release_patch_changed instanceof Release) { |
||
278 | $project->setRelease($release_patch_changed->getVersion(), $release_patch_changed); |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | // Stop searching once we hit the currently installed version. |
||
284 | if ($project->getExistingVersion() == $version) { |
||
285 | break; |
||
286 | } |
||
287 | |||
288 | // If we're running a dev snapshot and have a timestamp, stop |
||
289 | // searching for security updates once we hit an official release |
||
290 | // older than what we've got. Allow 100 seconds of leeway to handle |
||
291 | // differences between the datestamp in the .info file and the |
||
292 | // timestamp of the tarball itself (which are usually off by 1 or 2 |
||
293 | // seconds) so that we don't flag that as a new release. |
||
294 | if ($project->getInstallType() == Project::INSTALL_TYPE_DEV) { |
||
295 | if (!$project->getDatestamp()) { |
||
296 | // We don't have current timestamp info, so we can't know. |
||
297 | continue; |
||
298 | } elseif ($release->getDate() && ($project->getDatestamp() + 100 > $release->getDate())) { |
||
299 | // We're newer than this, so we can skip it. |
||
300 | continue; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | // See if this release is a security update. |
||
305 | if ($release->hasTerm('Release type') && in_array('Security update', $release->getTerm('Release type'))) { |
||
306 | $project->addSecurityUpdate($release->getVersion(), $release); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | // If we were unable to find a recommended version, then make the latest |
||
311 | // version the recommended version if possible. |
||
312 | if (!$project->getRecommended() && $project->getLatestVersion()) { |
||
313 | $project->setRecommended($project->getLatestVersion()); |
||
314 | } |
||
315 | |||
316 | // Check to see if we need an update or not. |
||
317 | if ($project->hasSecurityUpdates()) { |
||
318 | // If we found security updates, that always trumps any other status. |
||
319 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
320 | } |
||
321 | |||
322 | if ($project->getStatus()) { |
||
323 | // If we already know the status, we're done. |
||
324 | return; |
||
325 | } |
||
326 | |||
327 | // If we don't know what to recommend, there's nothing we can report. |
||
328 | // Bail out early. |
||
329 | if (!$project->getRecommended()) { |
||
330 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
331 | $project->setReason('No available releases found'); |
||
332 | |||
333 | return; |
||
334 | } |
||
335 | |||
336 | // If we're running a dev snapshot, compare the date of the dev snapshot |
||
337 | // with the latest official version, and record the absolute latest in |
||
338 | // 'latest_dev' so we can correctly decide if there's a newer release |
||
339 | // than our current snapshot. |
||
340 | if ($project->getInstallType() == Project::INSTALL_TYPE_DEV) { |
||
341 | if ($project->getDevVersion() && $this->getRelease($project->getDevVersion())->getDate( |
||
342 | ) > $this->getRelease($project->getLatestVersion())->getDate() |
||
343 | ) { |
||
344 | $project->setLatestDev($project->getDevVersion()); |
||
345 | } else { |
||
346 | $project->setLatestDev($project->getLatestVersion()); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | // Figure out the status, based on what we've seen and the install type. |
||
351 | switch ($project->getInstallType()) { |
||
352 | case Project::INSTALL_TYPE_OFFICIAL: |
||
353 | if ($project->getExistingVersion() == $project->getRecommended() || |
||
354 | $project->getExistingVersion() == $project->getLatestVersion() |
||
355 | ) { |
||
356 | $project->setStatus(self::UPDATE_CURRENT); |
||
357 | } else { |
||
358 | $project->setStatus(self::UPDATE_NOT_CURRENT); |
||
359 | } |
||
360 | break; |
||
361 | |||
362 | case Project::INSTALL_TYPE_DEV: |
||
363 | $latest = $this->getRelease($project->getLatestDev()); |
||
364 | |||
365 | if (!$project->getDatestamp()) { |
||
366 | $project->setStatus(self::UPDATE_NOT_CHECKED); |
||
367 | $project->setReason('Unknown release date'); |
||
368 | } elseif (($project->getDatestamp() + 100 > $latest->getDate())) { |
||
369 | $project->setStatus(self::UPDATE_CURRENT); |
||
370 | } else { |
||
371 | $project->setStatus(self::UPDATE_NOT_CURRENT); |
||
372 | } |
||
373 | break; |
||
374 | |||
375 | default: |
||
376 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
377 | $project->setReason('Invalid info'); |
||
378 | } |
||
379 | } |
||
380 | |||
669 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.