| Conditions | 19 |
| Paths | 47 |
| Total Lines | 79 |
| Code Lines | 48 |
| 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 |
||
| 106 | public function process() |
||
| 107 | { |
||
| 108 | if (!$this->tempFile) { |
||
| 109 | throw new Exception("Temporary sitemap file has not been set"); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!file_exists($this->tempFile)) { |
||
| 113 | throw new Exception("Temporary file $this->tempFile has been deleted!"); |
||
| 114 | } |
||
| 115 | |||
| 116 | $remainingChildren = $this->pagesToProcess; |
||
| 117 | |||
| 118 | // if there's no more, we're done! |
||
| 119 | if (!count($remainingChildren)) { |
||
| 120 | $this->completeJob(); |
||
| 121 | $this->isComplete = true; |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | // lets process our first item - note that we take it off the list of things left to do |
||
| 126 | $ID = array_shift($remainingChildren); |
||
| 127 | |||
| 128 | // get the page |
||
| 129 | $page = Versioned::get_by_stage('Page', 'Live', '"SiteTree_Live"."ID" = '.$ID); |
||
| 130 | |||
| 131 | if (!$page || !$page->Count()) { |
||
| 132 | $this->addMessage("Page ID #$ID could not be found, skipping"); |
||
| 133 | } else { |
||
| 134 | $page = $page->First(); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($page && $page instanceof Page && !($page instanceof ErrorPage)) { |
||
| 138 | if ($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) { |
||
| 139 | $created = $page->dbObject('Created'); |
||
| 140 | $now = new DBDatetime(); |
||
| 141 | $now->value = date('Y-m-d H:i:s'); |
||
| 142 | $versions = $page->Version; |
||
| 143 | $timediff = $now->format('U') - $created->format('U'); |
||
| 144 | |||
| 145 | // Check how many revisions have been made over the lifetime of the |
||
| 146 | // Page for a rough estimate of it's changing frequency. |
||
| 147 | $period = $timediff / ($versions + 1); |
||
| 148 | |||
| 149 | if ($period > 60*60*24*365) { // > 1 year |
||
| 150 | $page->ChangeFreq = 'yearly'; |
||
| 151 | } elseif ($period > 60*60*24*30) { // > ~1 month |
||
| 152 | $page->ChangeFreq = 'monthly'; |
||
| 153 | } elseif ($period > 60*60*24*7) { // > 1 week |
||
| 154 | $page->ChangeFreq = 'weekly'; |
||
| 155 | } elseif ($period > 60*60*24) { // > 1 day |
||
| 156 | $page->ChangeFreq = 'daily'; |
||
| 157 | } elseif ($period > 60*60) { // > 1 hour |
||
| 158 | $page->ChangeFreq = 'hourly'; |
||
| 159 | } else { // < 1 hour |
||
| 160 | $page->ChangeFreq = 'always'; |
||
| 161 | } |
||
| 162 | |||
| 163 | // do the generation of the file in a temporary location |
||
| 164 | $content = $page->renderWith('SitemapEntry'); |
||
| 165 | |||
| 166 | $fp = fopen($this->tempFile, "a"); |
||
| 167 | if (!$fp) { |
||
| 168 | throw new Exception("Could not open $this->tempFile for writing"); |
||
| 169 | } |
||
| 170 | fputs($fp, $content, strlen($content)); |
||
| 171 | fclose($fp); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | // and now we store the new list of remaining children |
||
| 176 | $this->pagesToProcess = $remainingChildren; |
||
| 177 | $this->currentStep++; |
||
| 178 | |||
| 179 | if (!count($remainingChildren)) { |
||
| 180 | $this->completeJob(); |
||
| 181 | $this->isComplete = true; |
||
| 182 | return; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 208 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.