| Conditions | 7 |
| Paths | 16 |
| Total Lines | 58 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 164 | public function runLinksCheck($limit = null) |
||
| 165 | { |
||
| 166 | // Check the current status |
||
| 167 | $status = BrokenExternalPageTrackStatus::get_or_create(); |
||
| 168 | |||
| 169 | // Calculate pages to run |
||
| 170 | $pageTracks = $status->getIncompleteTracks(); |
||
| 171 | if ($limit) { |
||
| 172 | $pageTracks = $pageTracks->limit($limit); |
||
| 173 | } |
||
| 174 | |||
| 175 | // Check each page |
||
| 176 | foreach ($pageTracks as $pageTrack) { |
||
| 177 | // Flag as complete |
||
| 178 | $pageTrack->Processed = 1; |
||
| 179 | $pageTrack->write(); |
||
| 180 | |||
| 181 | // Check value of html area |
||
| 182 | $page = $pageTrack->Page(); |
||
| 183 | $this->log("Checking {$page->Title}"); |
||
| 184 | $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); |
||
| 185 | if (!$htmlValue->isValid()) { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Check each link |
||
| 190 | $links = $htmlValue->getElementsByTagName('a'); |
||
| 191 | foreach ($links as $link) { |
||
| 192 | $this->checkPageLink($pageTrack, $link); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Update content of page based on link fixes / breakages |
||
| 196 | $htmlValue->saveHTML(); |
||
| 197 | $page->Content = $htmlValue->getContent(); |
||
| 198 | try { |
||
| 199 | $page->write(); |
||
| 200 | } catch (ValidationException $ex) { |
||
| 201 | $this->log("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage()); |
||
| 202 | continue; |
||
| 203 | } |
||
| 204 | |||
| 205 | // Once all links have been created for this page update HasBrokenLinks |
||
| 206 | $count = $pageTrack->BrokenLinks()->count(); |
||
| 207 | $this->log("Found {$count} broken links"); |
||
| 208 | if ($count) { |
||
| 209 | $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class); |
||
| 210 | // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true |
||
| 211 | DB::query(sprintf( |
||
| 212 | 'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'', |
||
| 213 | $siteTreeTable, |
||
| 214 | intval($pageTrack->ID) |
||
| 215 | )); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | $status->updateJobInfo('Updating completed pages'); |
||
| 220 | $status->updateStatus(); |
||
| 221 | return $status; |
||
| 222 | } |
||
| 247 |