| Conditions | 21 |
| Paths | 289 |
| Total Lines | 120 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 3 | 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 |
||
| 65 | public function before() |
||
| 66 | { |
||
| 67 | $this->id = $this->_content->id; |
||
| 68 | $this->title = Serialize::getDecodeLocale($this->_content->title); |
||
| 69 | $this->text = Serialize::getDecodeLocale($this->_content->text); |
||
| 70 | |||
| 71 | // check if title and text are exists |
||
| 72 | if (Str::length($this->title) < 1 || Str::length($this->text) < 1) { |
||
| 73 | throw new ForbiddenException(); |
||
| 74 | } |
||
| 75 | |||
| 76 | // get meta data |
||
| 77 | $this->metaTitle = Serialize::getDecodeLocale($this->_content->meta_title); |
||
| 78 | if (Str::likeEmpty($this->metaTitle)) { |
||
| 79 | $this->metaTitle = $this->title; |
||
| 80 | } |
||
| 81 | $this->metaDescription = Serialize::getDecodeLocale($this->_content->meta_description); |
||
| 82 | $tmpKeywords = Serialize::getDecodeLocale($this->_content->meta_keywords); |
||
| 83 | $this->metaKeywords = explode(',', $tmpKeywords); |
||
| 84 | |||
| 85 | // set content date, category data |
||
| 86 | $this->createDate = Date::humanize($this->_content->created_at); |
||
| 87 | $this->catName = Serialize::getDecodeLocale($this->_category->title); |
||
| 88 | $this->catPath = $this->_category->path; |
||
| 89 | |||
| 90 | // set user data |
||
| 91 | if (App::$User->isExist($this->_content->author_id)) { |
||
| 92 | $this->authorId = $this->_content->author_id; |
||
| 93 | $profile = App::$User->identity($this->authorId)->getProfile(); |
||
| 94 | $this->authorName = $profile->getNickname(); |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->source = $this->_content->source; |
||
| 98 | $this->views = $this->_content->views+1; |
||
| 99 | // check for dependence, add '' for general cat, ex: general/depend1/depend2/.../depend-n |
||
| 100 | $catNestingArray = Arr::merge([0 => ''], explode('/', $this->catPath)); |
||
| 101 | if ($catNestingArray > 1) { |
||
| 102 | // latest element its a current nesting level, lets cleanup it |
||
| 103 | array_pop($catNestingArray); |
||
| 104 | $catNestingPath = null; |
||
| 105 | foreach ($catNestingArray as $cPath) { |
||
| 106 | $catNestingPath .= $cPath; |
||
| 107 | |||
| 108 | // try to find category by path in db |
||
| 109 | $record = ContentCategory::getByPath($catNestingPath); |
||
| 110 | if ($record !== null && $record->count() > 0) { |
||
| 111 | // if founded - add to nesting data |
||
| 112 | $this->catNesting[] = [ |
||
| 113 | 'name' => Serialize::getDecodeLocale($record->title), |
||
| 114 | 'path' => $record->path |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | if (!Str::likeEmpty($catNestingPath)) { |
||
| 118 | $catNestingPath .= '/'; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | // build array of category nesting level |
||
| 124 | $this->catNesting[] = [ |
||
| 125 | 'name' => $this->catName, |
||
| 126 | 'path' => $this->catPath |
||
| 127 | ]; |
||
| 128 | |||
| 129 | // get gallery images and poster data |
||
| 130 | $galleryPath = '/upload/gallery/' . $this->_content->id; |
||
| 131 | // check if gallery folder is exist |
||
| 132 | if (Directory::exist($galleryPath)) { |
||
| 133 | $originImages = File::listFiles($galleryPath . '/orig/', ['.jpg', '.png', '.gif', '.jpeg', '.bmp', '.webp'], true); |
||
| 134 | // generate poster data |
||
| 135 | if (Arr::in($this->_content->poster, $originImages)) { |
||
| 136 | // original poster |
||
| 137 | $posterName = $this->_content->poster; |
||
| 138 | $this->posterFull = $galleryPath . '/orig/' . $posterName; |
||
| 139 | if (!File::exist($this->posterFull)) { |
||
| 140 | $this->posterFull = null; |
||
| 141 | } |
||
| 142 | // thumb poster |
||
| 143 | $posterSplit = explode('.', $posterName); |
||
| 144 | array_pop($posterSplit); |
||
| 145 | $posterCleanName = implode('.', $posterSplit); |
||
| 146 | $this->posterThumb = $galleryPath . '/thumb/' . $posterCleanName . '.jpg'; |
||
| 147 | if (!File::exist($this->posterThumb)) { |
||
| 148 | $this->posterThumb = null; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // generate full gallery |
||
| 153 | foreach ($originImages as $image) { |
||
| 154 | $imageSplit = explode('.', $image); |
||
| 155 | array_pop($imageSplit); |
||
| 156 | $imageClearName = implode('.', $imageSplit); |
||
| 157 | // skip image used in poster |
||
| 158 | if (Str::startsWith($imageClearName, $this->_content->poster)) { |
||
| 159 | continue; |
||
| 160 | } |
||
| 161 | $thumbPath = $galleryPath . '/thumb/' . $imageClearName . '.jpg'; |
||
| 162 | if (File::exist($thumbPath)) { |
||
| 163 | $this->galleryItems[$thumbPath] = $galleryPath . '/orig/' . $image; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // set rating data |
||
| 169 | $this->rating = $this->_content->rating; |
||
| 170 | $ignoredRate = App::$Session->get('content.rate.ignore'); |
||
| 171 | $this->canRate = true; |
||
| 172 | if (Obj::isArray($ignoredRate) && Arr::in((string)$this->id, $ignoredRate)) { |
||
| 173 | $this->canRate = false; |
||
| 174 | } |
||
| 175 | if (!App::$User->isAuth()) { |
||
| 176 | $this->canRate = false; |
||
| 177 | } elseif ($this->authorId === App::$User->identity()->getId()) { |
||
| 178 | $this->canRate = false; |
||
| 179 | } |
||
| 180 | |||
| 181 | // update views count |
||
| 182 | $this->_content->views += 1; |
||
| 183 | $this->_content->save(); |
||
| 184 | } |
||
| 185 | |||
| 194 | } |
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.