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