Conditions | 62 |
Paths | > 20000 |
Total Lines | 275 |
Code Lines | 133 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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->getVersionMajor(), $supported_majors)) { |
||
232 | if (!$project->hasAlsoAvailable($release->getVersionMajor())) { |
||
233 | $project->addAlsoAvailable($release->getVersionMajor(), $version); |
||
234 | $project->setRelease($version, $release); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | // Otherwise, this release can't matter to us, since it's neither |
||
239 | // from the release series we're currently using nor the recommended |
||
240 | // release. We don't even care about security updates for this |
||
241 | // branch, since if a project maintainer puts out a security release |
||
242 | // at a higher major version and not at the lower major version, |
||
243 | // they must remove the lower version from the supported major |
||
244 | // versions at the same time, in which case we won't hit this code. |
||
245 | continue; |
||
246 | } |
||
247 | |||
248 | // Look for the 'latest version' if we haven't found it yet. Latest is |
||
249 | // defined as the most recent version for the target major version. |
||
250 | if (!$project->getLatestVersion() && $release->getVersionMajor() == $target_major) { |
||
251 | $project->setLatestVersion($version); |
||
252 | $project->setRelease($version, $release); |
||
253 | } |
||
254 | |||
255 | // Look for the development snapshot release for this branch. |
||
256 | if (!$project->getDevVersion() |
||
257 | && $release->getVersionMajor() == $target_major |
||
258 | && $release->getVersionExtra() == Project::INSTALL_TYPE_DEV |
||
259 | ) { |
||
260 | $project->setDevVersion($version); |
||
261 | $project->setRelease($version, $release); |
||
262 | } |
||
263 | |||
264 | // Look for the 'recommended' version if we haven't found it yet (see |
||
265 | // phpdoc at the top of this function for the definition). |
||
266 | if (!$project->getRecommended() |
||
267 | && $release->getVersionMajor() == $target_major |
||
268 | && $release->getVersionPatch() |
||
269 | ) { |
||
270 | if ($patch != $release->getVersionPatch()) { |
||
271 | $patch = $release->getVersionPatch(); |
||
272 | $release_patch_changed = $release; |
||
273 | } |
||
274 | if (!$release->getVersionExtra() && $patch == $release->getVersionPatch()) { |
||
275 | $project->setRecommended($release_patch_changed->getVersion()); |
||
276 | if ($release_patch_changed instanceof Release) { |
||
277 | $project->setRelease($release_patch_changed->getVersion(), $release_patch_changed); |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | // Stop searching once we hit the currently installed version. |
||
283 | if ($project->getExistingVersion() == $version) { |
||
284 | break; |
||
285 | } |
||
286 | |||
287 | // If we're running a dev snapshot and have a timestamp, stop |
||
288 | // searching for security updates once we hit an official release |
||
289 | // older than what we've got. Allow 100 seconds of leeway to handle |
||
290 | // differences between the datestamp in the .info file and the |
||
291 | // timestamp of the tarball itself (which are usually off by 1 or 2 |
||
292 | // seconds) so that we don't flag that as a new release. |
||
293 | if ($project->getInstallType() == Project::INSTALL_TYPE_DEV) { |
||
294 | if (!$project->getDatestamp()) { |
||
295 | // We don't have current timestamp info, so we can't know. |
||
296 | continue; |
||
297 | } elseif ($release->getDate() && ($project->getDatestamp() + 100 > $release->getDate()->getTimestamp())) { |
||
298 | // We're newer than this, so we can skip it. |
||
299 | continue; |
||
300 | } |
||
301 | } |
||
302 | |||
303 | // See if this release is a security update. |
||
304 | if ($release->hasTerm('Release type') && in_array('Security update', $release->getTerm('Release type'))) { |
||
305 | $project->addSecurityUpdate($release->getVersion(), $release); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | // If we were unable to find a recommended version, then make the latest |
||
310 | // version the recommended version if possible. |
||
311 | if (!$project->getRecommended() && $project->getLatestVersion()) { |
||
312 | $project->setRecommended($project->getLatestVersion()); |
||
313 | } |
||
314 | |||
315 | // Check to see if we need an update or not. |
||
316 | if ($project->hasSecurityUpdates()) { |
||
317 | // If we found security updates, that always trumps any other status. |
||
318 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
319 | } |
||
320 | |||
321 | if ($project->getStatus()) { |
||
322 | // If we already know the status, we're done. |
||
323 | return; |
||
324 | } |
||
325 | |||
326 | // If we don't know what to recommend, there's nothing we can report. |
||
327 | // Bail out early. |
||
328 | if (!$project->getRecommended()) { |
||
329 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
330 | $project->setReason('No available releases found'); |
||
331 | |||
332 | return; |
||
333 | } |
||
334 | |||
335 | // If we're running a dev snapshot, compare the date of the dev snapshot |
||
336 | // with the latest official version, and record the absolute latest in |
||
337 | // 'latest_dev' so we can correctly decide if there's a newer release |
||
338 | // than our current snapshot. |
||
339 | if ($project->getInstallType() == Project::INSTALL_TYPE_DEV) { |
||
340 | if ($project->getDevVersion() && $this->getRelease($project->getDevVersion())->getDate( |
||
341 | ) > $this->getRelease($project->getLatestVersion())->getDate() |
||
342 | ) { |
||
343 | $project->setLatestDev($project->getDevVersion()); |
||
344 | } else { |
||
345 | $project->setLatestDev($project->getLatestVersion()); |
||
346 | } |
||
347 | } |
||
348 | |||
349 | // Figure out the status, based on what we've seen and the install type. |
||
350 | switch ($project->getInstallType()) { |
||
351 | case Project::INSTALL_TYPE_OFFICIAL: |
||
352 | if ($project->getExistingVersion() == $project->getRecommended() || |
||
353 | $project->getExistingVersion() == $project->getLatestVersion() |
||
354 | ) { |
||
355 | $project->setStatus(self::UPDATE_CURRENT); |
||
356 | } else { |
||
357 | $project->setStatus(self::UPDATE_NOT_CURRENT); |
||
358 | } |
||
359 | break; |
||
360 | |||
361 | case Project::INSTALL_TYPE_DEV: |
||
362 | $latest = $this->getRelease($project->getLatestDev()); |
||
363 | |||
364 | if (!$project->getDatestamp()) { |
||
365 | $project->setStatus(self::UPDATE_NOT_CHECKED); |
||
366 | $project->setReason('Unknown release date'); |
||
367 | } elseif (($project->getDatestamp() + 100 > $latest->getDate()->getTimestamp())) { |
||
368 | $project->setStatus(self::UPDATE_CURRENT); |
||
369 | } else { |
||
370 | $project->setStatus(self::UPDATE_NOT_CURRENT); |
||
371 | } |
||
372 | break; |
||
373 | |||
374 | default: |
||
375 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
376 | $project->setReason('Invalid info'); |
||
377 | } |
||
378 | } |
||
379 | |||
668 |