| Conditions | 21 |
| Paths | 289 |
| Total Lines | 124 |
| Code Lines | 72 |
| 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 |
||
| 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 | // check for dependence, add '' for general cat, ex: general/depend1/depend2/.../depend-n |
||
| 107 | $catNestingArray = Arr::merge([0 => ''], explode('/', $this->catPath)); |
||
| 108 | if ($catNestingArray > 1) { |
||
| 109 | // latest element its a current nesting level, lets cleanup it |
||
| 110 | array_pop($catNestingArray); |
||
| 111 | $catNestingPath = null; |
||
| 112 | foreach ($catNestingArray as $cPath) { |
||
| 113 | $catNestingPath .= $cPath; |
||
| 114 | |||
| 115 | // try to find category by path in db |
||
| 116 | $record = ContentCategory::getByPath($catNestingPath); |
||
| 117 | if ($record !== null && $record->count() > 0) { |
||
| 118 | // if founded - add to nesting data |
||
| 119 | $this->catNesting[] = [ |
||
| 120 | 'name' => $record->getLocaled('title'), |
||
| 121 | 'path' => $record->path |
||
| 122 | ]; |
||
| 123 | } |
||
| 124 | if (!Str::likeEmpty($catNestingPath)) { |
||
| 125 | $catNestingPath .= '/'; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | // build array of category nesting level |
||
| 131 | $this->catNesting[] = [ |
||
| 132 | 'name' => $this->catName, |
||
| 133 | 'path' => $this->catPath |
||
| 134 | ]; |
||
| 135 | |||
| 136 | // get gallery images and poster data |
||
| 137 | $galleryPath = '/upload/gallery/' . $this->_content->id; |
||
| 138 | // check if gallery folder is exist |
||
| 139 | if (Directory::exist($galleryPath)) { |
||
| 140 | $originImages = File::listFiles($galleryPath . '/orig/', ['.jpg', '.png', '.gif', '.jpeg', '.bmp', '.webp'], true); |
||
| 141 | // generate poster data |
||
| 142 | if (Arr::in($this->_content->poster, $originImages)) { |
||
| 143 | // original poster |
||
| 144 | $posterName = $this->_content->poster; |
||
| 145 | $this->posterFull = $galleryPath . '/orig/' . $posterName; |
||
| 146 | if (!File::exist($this->posterFull)) { |
||
| 147 | $this->posterFull = null; |
||
| 148 | } |
||
| 149 | |||
| 150 | // thumb poster |
||
| 151 | $posterSplit = explode('.', $posterName); |
||
| 152 | array_pop($posterSplit); |
||
| 153 | $posterCleanName = implode('.', $posterSplit); |
||
| 154 | $this->posterThumb = $galleryPath . '/thumb/' . $posterCleanName . '.jpg'; |
||
| 155 | if (!File::exist($this->posterThumb)) { |
||
| 156 | $this->posterThumb = null; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // generate full gallery |
||
| 161 | foreach ($originImages as $image) { |
||
| 162 | $imageSplit = explode('.', $image); |
||
| 163 | array_pop($imageSplit); |
||
| 164 | $imageClearName = implode('.', $imageSplit); |
||
| 165 | // skip image used in poster |
||
| 166 | if (Str::startsWith($imageClearName, $this->_content->poster)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | $thumbPath = $galleryPath . '/thumb/' . $imageClearName . '.jpg'; |
||
| 171 | if (File::exist($thumbPath)) { |
||
| 172 | $this->galleryItems[$thumbPath] = $galleryPath . '/orig/' . $image; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | // set rating data |
||
| 178 | $this->rating = $this->_content->rating; |
||
| 179 | $ignoredRate = App::$Session->get('content.rate.ignore'); |
||
| 180 | $this->canRate = true; |
||
| 181 | if (Any::isArray($ignoredRate) && Arr::in((string)$this->id, $ignoredRate)) { |
||
| 182 | $this->canRate = false; |
||
| 183 | } |
||
| 184 | if (!App::$User->isAuth()) { |
||
| 185 | $this->canRate = false; |
||
| 186 | } elseif ($this->authorId === App::$User->identity()->getId()) { |
||
| 187 | $this->canRate = false; |
||
| 188 | } |
||
| 189 | |||
| 190 | // update views count |
||
| 191 | $this->_content->views += 1; |
||
| 192 | $this->_content->save(); |
||
| 193 | } |
||
| 194 | |||
| 213 |
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.