| Conditions | 6 |
| Paths | 12 |
| Total Lines | 53 |
| Code Lines | 29 |
| 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 |
||
| 153 | public function runLinksCheck($limit = null) |
||
| 154 | { |
||
| 155 | // Check the current status |
||
| 156 | $status = BrokenExternalPageTrackStatus::get_or_create(); |
||
| 157 | |||
| 158 | // Calculate pages to run |
||
| 159 | $pageTracks = $status->getIncompleteTracks(); |
||
| 160 | if ($limit) { |
||
| 161 | $pageTracks = $pageTracks->limit($limit); |
||
| 162 | } |
||
| 163 | |||
| 164 | // Check each page |
||
| 165 | foreach ($pageTracks as $pageTrack) { |
||
| 166 | // Flag as complete |
||
| 167 | $pageTrack->Processed = 1; |
||
| 168 | $pageTrack->write(); |
||
| 169 | |||
| 170 | // Check value of html area |
||
| 171 | $page = $pageTrack->Page(); |
||
| 172 | $this->log("Checking {$page->Title}"); |
||
| 173 | $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); |
||
| 174 | if (!$htmlValue->isValid()) { |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | // Check each link |
||
| 179 | $links = $htmlValue->getElementsByTagName('a'); |
||
| 180 | foreach ($links as $link) { |
||
| 181 | $this->checkPageLink($pageTrack, $link); |
||
| 182 | } |
||
| 183 | |||
| 184 | // Update content of page based on link fixes / breakages |
||
| 185 | $htmlValue->saveHTML(); |
||
| 186 | $page->Content = $htmlValue->getContent(); |
||
| 187 | $page->write(); |
||
| 188 | |||
| 189 | // Once all links have been created for this page update HasBrokenLinks |
||
| 190 | $count = $pageTrack->BrokenLinks()->count(); |
||
| 191 | $this->log("Found {$count} broken links"); |
||
| 192 | if ($count) { |
||
| 193 | $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class); |
||
| 194 | // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true |
||
| 195 | DB::query(sprintf( |
||
| 196 | 'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'', |
||
| 197 | $siteTreeTable, |
||
| 198 | intval($pageTrack->ID) |
||
| 199 | )); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $status->updateJobInfo('Updating completed pages'); |
||
| 204 | $status->updateStatus(); |
||
| 205 | return $status; |
||
| 206 | } |
||
| 231 |