Conditions | 13 |
Paths | 645 |
Total Lines | 99 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
158 | private function buildOutput($records) |
||
159 | { |
||
160 | // prepare current category data to output (unserialize locales and strip tags) |
||
161 | $this->category = [ |
||
162 | 'title' => App::$Security->strip_tags(Serialize::getDecodeLocale($this->_currentCategory->title)), |
||
|
|||
163 | 'description' => App::$Security->strip_tags(Serialize::getDecodeLocale($this->_currentCategory->description)), |
||
164 | 'configs' => Serialize::decode($this->_currentCategory->configs), |
||
165 | 'path' => $this->_currentCategory->path |
||
166 | ]; |
||
167 | |||
168 | // check if this category is hidden |
||
169 | if ((int)$this->category['configs']['showCategory'] !== 1) { |
||
170 | throw new ForbiddenException(__('This category is not available to view')); |
||
171 | } |
||
172 | |||
173 | // make sorted tree of categories to display in breadcrumbs |
||
174 | foreach ($this->_allCategories as $cat) { |
||
175 | $this->categories[$cat->id] = $cat; |
||
176 | } |
||
177 | |||
178 | $nullItems = 0; |
||
179 | foreach ($records as $row) { |
||
180 | // get full text |
||
181 | $text = Serialize::getDecodeLocale($row->text); |
||
182 | // try to find page breaker |
||
183 | $breakPosition = mb_strpos($text, self::PAGE_BREAK, null, 'UTF-8'); |
||
184 | // offset is founded, try to split preview from full text |
||
185 | if ($breakPosition !== false) { |
||
186 | $text = Str::sub($text, 0, $breakPosition); |
||
187 | } else { // page breaker is not founded, lets get a fun ;D |
||
188 | // find first paragraph ending |
||
189 | $breakPosition = mb_strpos($text, '</p>', null, 'UTF-8'); |
||
190 | // cut text from position caret before </p> (+4 symbols to save item as valid) |
||
191 | $text = Str::sub($text, 0, $breakPosition+4); |
||
192 | } |
||
193 | |||
194 | $itemPath = $this->categories[$row->category_id]->path; |
||
195 | if (!Str::likeEmpty($itemPath)) { |
||
196 | $itemPath .= '/'; |
||
197 | } |
||
198 | $itemPath .= $row->path; |
||
199 | |||
200 | // try to find poster and thumbnail for this content item |
||
201 | $poster = $row->poster; |
||
202 | $thumb = null; |
||
203 | if (!Str::likeEmpty($poster)) { |
||
204 | $thumbName = Str::cleanExtension($poster) . '.jpg'; |
||
205 | $poster = '/upload/gallery/' . $row->id . '/orig/' . $poster; |
||
206 | $thumb = '/upload/gallery/' . $row->id . '/thumb/' . $thumbName; |
||
207 | if (!File::exist($poster)) { |
||
208 | $poster = null; |
||
209 | } |
||
210 | if (!File::exist($thumb)) { |
||
211 | $thumb = null; |
||
212 | } |
||
213 | } else { |
||
214 | $poster = null; |
||
215 | } |
||
216 | |||
217 | // prepare tags data |
||
218 | $tags = Serialize::getDecodeLocale($row->meta_keywords); |
||
219 | if (!Str::likeEmpty($tags)) { |
||
220 | $tags = explode(',', $tags); |
||
221 | } else { |
||
222 | $tags = null; |
||
223 | } |
||
224 | |||
225 | // check title length on current language locale |
||
226 | $localeTitle = App::$Security->strip_tags(Serialize::getDecodeLocale($row->title)); |
||
227 | if (Str::length($localeTitle) < 1) { |
||
228 | ++$nullItems; |
||
229 | } |
||
230 | |||
231 | $owner = App::$User->identity($row->author_id); |
||
232 | // make a fake if user is not exist over id |
||
233 | if ($owner === null) { |
||
234 | $owner = new User(); |
||
235 | } |
||
236 | |||
237 | // build result array |
||
238 | $this->items[] = [ |
||
239 | 'title' => $localeTitle, |
||
240 | 'text' => $text, |
||
241 | 'date' => Date::convertToDatetime($row->created_at, Date::FORMAT_TO_HOUR), |
||
242 | 'author' => $owner, |
||
243 | 'poster' => $poster, |
||
244 | 'thumb' => $thumb, |
||
245 | 'views' => (int)$row->views, |
||
246 | 'rating' => (int)$row->rating, |
||
247 | 'category' => $this->categories[$row->category_id], |
||
248 | 'uri' => '/content/read/' . $itemPath, |
||
249 | 'tags' => $tags |
||
250 | ]; |
||
251 | } |
||
252 | |||
253 | if ($nullItems === $this->_contentCount) { |
||
254 | throw new NotFoundException(__('Content is not founded')); |
||
255 | } |
||
256 | } |
||
257 | |||
266 | } |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.