Conditions | 19 |
Paths | 47 |
Total Lines | 80 |
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 |
||
114 | public function process() |
||
115 | { |
||
116 | if (!$this->tempFile) { |
||
117 | throw new Exception("Temporary sitemap file has not been set"); |
||
118 | } |
||
119 | |||
120 | if (!file_exists($this->tempFile)) { |
||
121 | throw new Exception("Temporary file $this->tempFile has been deleted!"); |
||
122 | } |
||
123 | |||
124 | $remainingChildren = $this->pagesToProcess; |
||
125 | |||
126 | // if there's no more, we're done! |
||
127 | if (!count($remainingChildren)) { |
||
128 | $this->completeJob(); |
||
129 | $this->isComplete = true; |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | // lets process our first item - note that we take it off the list of things left to do |
||
134 | $ID = array_shift($remainingChildren); |
||
135 | |||
136 | // get the page |
||
137 | $page = Versioned::get_by_stage(Page::class, Versioned::LIVE, '"SiteTree_Live"."ID" = ' . $ID); |
||
138 | |||
139 | if (!$page || !$page->Count()) { |
||
140 | $this->addMessage("Page ID #$ID could not be found, skipping"); |
||
141 | } else { |
||
142 | $page = $page->First(); |
||
143 | } |
||
144 | |||
145 | if ($page && $page instanceof Page && !($page instanceof ErrorPage)) { |
||
146 | if ($page->canView() && (!isset($page->Priority) || $page->Priority > 0)) { |
||
147 | /** @var DBDatetime $created */ |
||
148 | $created = $page->dbObject('Created'); |
||
149 | $now = DBDatetime::create(); |
||
150 | $now->setValue(DBDatetime::now()->Rfc2822()); |
||
151 | $versions = $page->Version; |
||
152 | $timediff = $now->format('U') - $created->format('U'); |
||
153 | |||
154 | // Check how many revisions have been made over the lifetime of the |
||
155 | // Page for a rough estimate of it's changing frequency. |
||
156 | $period = $timediff / ($versions + 1); |
||
157 | |||
158 | if ($period > 60 * 60 * 24 * 365) { // > 1 year |
||
159 | $page->ChangeFreq = 'yearly'; |
||
160 | } elseif ($period > 60 * 60 * 24 * 30) { // > ~1 month |
||
161 | $page->ChangeFreq = 'monthly'; |
||
162 | } elseif ($period > 60 * 60 * 24 * 7) { // > 1 week |
||
163 | $page->ChangeFreq = 'weekly'; |
||
164 | } elseif ($period > 60 * 60 * 24) { // > 1 day |
||
165 | $page->ChangeFreq = 'daily'; |
||
166 | } elseif ($period > 60 * 60) { // > 1 hour |
||
167 | $page->ChangeFreq = 'hourly'; |
||
168 | } else { // < 1 hour |
||
169 | $page->ChangeFreq = 'always'; |
||
170 | } |
||
171 | |||
172 | // do the generation of the file in a temporary location |
||
173 | $content = $page->renderWith('SitemapEntry'); |
||
174 | |||
175 | $fp = fopen($this->tempFile, "a"); |
||
176 | if (!$fp) { |
||
177 | throw new Exception("Could not open $this->tempFile for writing"); |
||
178 | } |
||
179 | fputs($fp, $content, strlen($content)); |
||
180 | fclose($fp); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // and now we store the new list of remaining children |
||
185 | $this->pagesToProcess = $remainingChildren; |
||
186 | $this->currentStep++; |
||
187 | |||
188 | if (!count($remainingChildren)) { |
||
189 | $this->completeJob(); |
||
190 | $this->isComplete = true; |
||
191 | return; |
||
192 | } |
||
193 | } |
||
194 | |||
222 |
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@property
annotation 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.