Conditions | 59 |
Paths | > 20000 |
Total Lines | 252 |
Code Lines | 127 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
86 | public function compare(Project $project) |
||
87 | { |
||
88 | // If the project status is marked as something bad, there's nothing else |
||
89 | // to consider. |
||
90 | if ($this->getProjectStatus()) { |
||
91 | switch ($this->getProjectStatus()) { |
||
92 | case 'insecure': |
||
93 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
94 | break; |
||
95 | case 'unpublished': |
||
96 | case 'revoked': |
||
97 | $project->setStatus(self::UPDATE_REVOKED); |
||
98 | break; |
||
99 | case 'unsupported': |
||
100 | $project->setStatus(self::UPDATE_NOT_SUPPORTED); |
||
101 | break; |
||
102 | case 'not-fetched': |
||
103 | $project->setStatus(self::UPDATE_NOT_FETCHED); |
||
104 | break; |
||
105 | |||
106 | default: |
||
107 | // Assume anything else (e.g. 'published') is valid and we should |
||
108 | // perform the rest of the logic in this function. |
||
109 | break; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | if ($project->getStatus()) { |
||
114 | // We already know the status for this project, so there's nothing else to |
||
115 | // compute. Record the project status into $project_data and we're done. |
||
116 | $project->setProjectStatus($this->getProjectStatus()); |
||
117 | |||
118 | return; |
||
119 | } |
||
120 | |||
121 | // Figure out the target major version. |
||
122 | $existing_major = $project->getExistingMajor(); |
||
123 | $supported_majors = array(); |
||
124 | if ($this->getSupportedMajors()) { |
||
125 | $supported_majors = explode(',', $this->getSupportedMajors()); |
||
126 | } elseif ($this->getDefaultMajor()) { |
||
127 | // Older release history XML file without supported or recommended. |
||
128 | $supported_majors[] = $this->getDefaultMajor(); |
||
129 | } |
||
130 | |||
131 | if (in_array($existing_major, $supported_majors)) { |
||
132 | // Still supported, stay at the current major version. |
||
133 | $target_major = $existing_major; |
||
134 | } elseif ($this->getRecommendedMajor()) { |
||
135 | // Since 'recommended_major' is defined, we know this is the new XML |
||
136 | // format. Therefore, we know the current release is unsupported since |
||
137 | // its major version was not in the 'supported_majors' list. We should |
||
138 | // find the best release from the recommended major version. |
||
139 | $target_major = $this->getRecommendedMajor(); |
||
140 | $project->setStatus(self::UPDATE_NOT_SUPPORTED); |
||
141 | } elseif ($this->getDefaultMajor()) { |
||
142 | // Older release history XML file without recommended, so recommend |
||
143 | // the currently defined "default_major" version. |
||
144 | $target_major = $this->getDefaultMajor(); |
||
145 | } else { |
||
146 | // Malformed XML file? Stick with the current version. |
||
147 | $target_major = $existing_major; |
||
148 | } |
||
149 | |||
150 | // Make sure we never tell the admin to downgrade. If we recommended an |
||
151 | // earlier version than the one they're running, they'd face an |
||
152 | // impossible data migration problem, since Drupal never supports a DB |
||
153 | // downgrade path. In the unfortunate case that what they're running is |
||
154 | // unsupported, and there's nothing newer for them to upgrade to, we |
||
155 | // can't print out a "Recommended version", but just have to tell them |
||
156 | // what they have is unsupported and let them figure it out. |
||
157 | $target_major = max($existing_major, $target_major); |
||
158 | |||
159 | $release_patch_changed = null; |
||
160 | $patch = ''; |
||
161 | |||
162 | // If the project is marked as UPDATE_FETCH_PENDING, it means that the |
||
163 | // data we currently have (if any) is stale, and we've got a task queued |
||
164 | // up to (re)fetch the data. In that case, we mark it as such, merge in |
||
165 | // whatever data we have (e.g. project title and link), and move on. |
||
166 | if ($this->getFetchStatus() == self::UPDATE_FETCH_PENDING) { |
||
167 | $project->setStatus(self::UPDATE_FETCH_PENDING); |
||
168 | $project->setReason('No available update data'); |
||
169 | $project->setFetchStatus($this->getFetchStatus()); |
||
170 | |||
171 | return; |
||
172 | } |
||
173 | |||
174 | // Defend ourselves from XML history files that contain no releases. |
||
175 | if (!$this->getReleases()) { |
||
176 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
177 | $project->setReason('No available releases found'); |
||
178 | |||
179 | return; |
||
180 | } |
||
181 | |||
182 | foreach ($this->getReleases() as $release => $release) { |
||
183 | // First, if this is the existing release, check a few conditions. |
||
184 | if ($project->getExistingVersion() == $release) { |
||
185 | if ($release->hasTerm('Release type') && |
||
186 | in_array('Insecure', $release->getTerm('Release type')) |
||
187 | ) { |
||
188 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
189 | } elseif ($release->getStatus() == 'unpublished') { |
||
190 | $project->setStatus(self::UPDATE_REVOKED); |
||
191 | } elseif ($release->hasTerm('Release type') && |
||
192 | in_array('Unsupported', $release->getTerm('Release type')) |
||
193 | ) { |
||
194 | $project->setStatus(self::UPDATE_NOT_SUPPORTED); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | // Otherwise, ignore unpublished, insecure, or unsupported releases. |
||
199 | if ($release->getStatus() == 'unpublished' || |
||
200 | ($release->hasTerm('Release type') && |
||
201 | (in_array('Insecure', $release->getTerm('Release type')) || |
||
202 | in_array('Unsupported', $release->getTerm('Release type')))) |
||
203 | ) { |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | // Look for the 'latest version' if we haven't found it yet. Latest is |
||
208 | // defined as the most recent version for the target major version. |
||
209 | if (!$project->getLatestVersion() && $release->getVersionMajor() == $target_major) { |
||
210 | $project->setLatestVersion($release); |
||
211 | $project->setRelease($release, $release); |
||
|
|||
212 | } |
||
213 | |||
214 | // Look for the development snapshot release for this branch. |
||
215 | if (!$project->getDevVersion() |
||
216 | && $release->getVersionMajor() == $target_major |
||
217 | && $release->getVersionExtra() == Project::INSTALL_TYPE_DEV |
||
218 | ) { |
||
219 | $project->setDevVersion($release); |
||
220 | $project->setRelease($release, $release); |
||
221 | } |
||
222 | |||
223 | // Look for the 'recommended' version if we haven't found it yet (see |
||
224 | // phpdoc at the top of this function for the definition). |
||
225 | if (!$project->getRecommended() |
||
226 | && $release->getVersionMajor() == $target_major |
||
227 | && $release->getVersionPatch() |
||
228 | ) { |
||
229 | if ($patch != $release->getVersionPatch()) { |
||
230 | $patch = $release->getVersionPatch(); |
||
231 | $release_patch_changed = $release; |
||
232 | } |
||
233 | if (!$release->getVersionExtra() && $patch == $release->getVersionPatch()) { |
||
234 | $project->setRecommended($release_patch_changed->getVersion()); |
||
235 | if ($release_patch_changed instanceof Release) { |
||
236 | $project->setRelease($release_patch_changed->getVersion(), $release_patch_changed); |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | // Stop searching once we hit the currently installed version. |
||
242 | if ($project->getExistingVersion() == $release) { |
||
243 | break; |
||
244 | } |
||
245 | |||
246 | // If we're running a dev snapshot and have a timestamp, stop |
||
247 | // searching for security updates once we hit an official release |
||
248 | // older than what we've got. Allow 100 seconds of leeway to handle |
||
249 | // differences between the datestamp in the .info file and the |
||
250 | // timestamp of the tarball itself (which are usually off by 1 or 2 |
||
251 | // seconds) so that we don't flag that as a new release. |
||
252 | if ($project->getInstallType() == Project::INSTALL_TYPE_DEV) { |
||
253 | if (!$project->getDatestamp()) { |
||
254 | // We don't have current timestamp info, so we can't know. |
||
255 | continue; |
||
256 | } elseif ($release->getDate() && ($project->getDatestamp() + 100 > $release->getDate())) { |
||
257 | // We're newer than this, so we can skip it. |
||
258 | continue; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | // See if this release is a security update. |
||
263 | if ($release->hasTerm('Release type') && in_array('Security update', $release->getTerm('Release type'))) { |
||
264 | $project->addSecurityUpdate($release->getVersion(), $release); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | // If we were unable to find a recommended version, then make the latest |
||
269 | // version the recommended version if possible. |
||
270 | if (!$project->getRecommended() && $project->getLatestVersion()) { |
||
271 | $project->setRecommended($project->getLatestVersion()); |
||
272 | } |
||
273 | |||
274 | // Check to see if we need an update or not. |
||
275 | if ($project->hasSecurityUpdates()) { |
||
276 | // If we found security updates, that always trumps any other status. |
||
277 | $project->setStatus(self::UPDATE_NOT_SECURE); |
||
278 | } |
||
279 | |||
280 | if ($project->getStatus()) { |
||
281 | // If we already know the status, we're done. |
||
282 | return; |
||
283 | } |
||
284 | |||
285 | // If we don't know what to recommend, there's nothing we can report. |
||
286 | // Bail out early. |
||
287 | if (!$project->getRecommended()) { |
||
288 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
289 | $project->setReason('No available releases found'); |
||
290 | |||
291 | return; |
||
292 | } |
||
293 | |||
294 | // If we're running a dev snapshot, compare the date of the dev snapshot |
||
295 | // with the latest official version, and record the absolute latest in |
||
296 | // 'latest_dev' so we can correctly decide if there's a newer release |
||
297 | // than our current snapshot. |
||
298 | if ($project->getInstallType() == Project::INSTALL_TYPE_DEV) { |
||
299 | if ($project->getDevVersion() && $this->getRelease($project->getDevVersion())->getDate( |
||
300 | ) > $this->getRelease($project->getLatestVersion())->getDate() |
||
301 | ) { |
||
302 | $project->setLatestDev($project->getDevVersion()); |
||
303 | } else { |
||
304 | $project->setLatestDev($project->getLatestVersion()); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | // Figure out the status, based on what we've seen and the install type. |
||
309 | switch ($project->getInstallType()) { |
||
310 | case Project::INSTALL_TYPE_OFFICIAL: |
||
311 | if ($project->getExistingVersion() == $project->getRecommended() || |
||
312 | $project->getExistingVersion() == $project->getLatestVersion() |
||
313 | ) { |
||
314 | $project->setStatus(self::UPDATE_CURRENT); |
||
315 | } else { |
||
316 | $project->setStatus(self::UPDATE_NOT_CURRENT); |
||
317 | } |
||
318 | break; |
||
319 | |||
320 | case Project::INSTALL_TYPE_DEV: |
||
321 | $latest = $this->getRelease($project->getLatestDev()); |
||
322 | |||
323 | if (!$project->getDatestamp()) { |
||
324 | $project->setStatus(self::UPDATE_NOT_CHECKED); |
||
325 | $project->setReason('Unknown release date'); |
||
326 | } elseif (($project->getDatestamp() + 100 > $latest->getDate())) { |
||
327 | $project->setStatus(self::UPDATE_CURRENT); |
||
328 | } else { |
||
329 | $project->setStatus(self::UPDATE_NOT_CURRENT); |
||
330 | } |
||
331 | break; |
||
332 | |||
333 | default: |
||
334 | $project->setStatus(self::UPDATE_UNKNOWN); |
||
335 | $project->setReason('Invalid info'); |
||
336 | } |
||
337 | } |
||
338 | |||
621 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: