| Conditions | 9 |
| Paths | 25 |
| Total Lines | 59 |
| Code Lines | 35 |
| 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 |
||
| 70 | public function before() |
||
| 71 | { |
||
| 72 | $this->id = $this->_content->id; |
||
| 73 | $this->title = $this->_content->getLocaled('title'); |
||
| 74 | $this->text = $this->_content->getLocaled('text'); |
||
| 75 | $this->display = (bool)$this->_content->display; |
||
| 76 | |||
| 77 | // check if title and text are exists |
||
| 78 | if (Str::length($this->title) < 1 || Str::length($this->text) < 1) { |
||
| 79 | throw new ForbiddenException('Content of this page is empty!'); |
||
| 80 | } |
||
| 81 | |||
| 82 | // get meta data |
||
| 83 | $this->metaTitle = $this->_content->getLocaled('meta_title'); |
||
| 84 | if (Any::isEmpty($this->metaTitle)) { |
||
| 85 | $this->metaTitle = $this->title; |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->metaDescription = $this->_content->getLocaled('meta_description'); |
||
| 89 | $tmpKeywords = $this->_content->getLocaled('meta_keywords'); |
||
| 90 | $this->metaKeywords = explode(',', $tmpKeywords); |
||
| 91 | |||
| 92 | // set content date, category data |
||
| 93 | $this->createDate = Date::humanize($this->_content->created_at); |
||
| 94 | $this->catName = $this->_category->getLocaled('title'); |
||
| 95 | $this->catPath = $this->_category->path; |
||
| 96 | |||
| 97 | // set user data |
||
| 98 | if (App::$User->isExist($this->_content->author_id)) { |
||
| 99 | $this->authorId = $this->_content->author_id; |
||
| 100 | $profile = App::$User->identity($this->authorId)->profile; |
||
| 101 | $this->authorName = $profile->getNickname(); |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->source = $this->_content->source; |
||
| 105 | $this->views = $this->_content->views+1; |
||
| 106 | |||
| 107 | // build category nesting sorted array |
||
| 108 | $this->catNesting[] = $this->expandCategoryNesting(); |
||
| 109 | // set gallery thumbnail & image if exist |
||
| 110 | $this->prepareGallery(); |
||
| 111 | |||
| 112 | // set rating data |
||
| 113 | $this->rating = $this->_content->rating; |
||
| 114 | $ignoredRate = App::$Session->get('content.rate.ignore'); |
||
| 115 | $this->canRate = true; |
||
| 116 | if (Any::isArray($ignoredRate) && Arr::in((string)$this->id, $ignoredRate)) { |
||
| 117 | $this->canRate = false; |
||
| 118 | } |
||
| 119 | if (!App::$User->isAuth()) { |
||
| 120 | $this->canRate = false; |
||
| 121 | } elseif ($this->authorId === App::$User->identity()->getId()) { |
||
| 122 | $this->canRate = false; |
||
| 123 | } |
||
| 124 | |||
| 125 | // update views count |
||
| 126 | $this->_content->views += 1; |
||
| 127 | $this->_content->save(); |
||
| 128 | } |
||
| 129 | |||
| 232 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.