Conditions | 33 |
Paths | 22 |
Total Lines | 163 |
Code Lines | 124 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | 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 declare(strict_types=1); |
||
70 | public function getItemRating($itemId = null, $source = null): array |
||
71 | { |
||
72 | $itemId = $itemId ?? 0; |
||
73 | $source = $source ?? 0; |
||
74 | $xoopsUser = $GLOBALS['xoopsUser']; |
||
75 | |||
76 | $itemRating = []; |
||
77 | $itemRating['nb_vote'] = 0; |
||
78 | $uid = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
||
79 | $voted = false; |
||
80 | $ip = \getenv('REMOTE_ADDR'); |
||
81 | $currentRating = 0; |
||
82 | $count = 0; |
||
83 | |||
84 | $max_units = 10; |
||
85 | $ratingbarsValue = (int)$this->helper->getConfig('ratingbars'); |
||
86 | $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM]; |
||
87 | |||
88 | if (\in_array($ratingbarsValue, $ratingArray, true)) { |
||
89 | $rating_unitwidth = 25; |
||
90 | if (Constants::RATING_5STARS === (int)$this->helper->getConfig('ratingbars')) { |
||
91 | $max_units = 5; |
||
92 | } |
||
93 | |||
94 | $criteria = new \CriteriaCompo(); |
||
95 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
96 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
97 | |||
98 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
99 | ->getObjects($criteria); |
||
|
|||
100 | $count = \count($voteObjs); |
||
101 | $itemRating['nb_vote'] = $count; |
||
102 | |||
103 | foreach ($voteObjs as $voteObj) { |
||
104 | $currentRating += $voteObj->getVar('rate'); |
||
105 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
106 | $voted = true; |
||
107 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
108 | } |
||
109 | } |
||
110 | unset($criteria); |
||
111 | |||
112 | $itemRating['avg_rate_value'] = 0; |
||
113 | if ($count > 0) { |
||
114 | $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2); |
||
115 | } |
||
116 | if (1 == $count) { |
||
117 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_1); |
||
118 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_1); |
||
119 | } else { |
||
120 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_X); |
||
121 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_X); |
||
122 | } |
||
123 | $text = \str_replace('%m', (string)$max_units, $text); |
||
124 | $text = \str_replace('%t', (string)$itemRating['nb_vote'], $text); |
||
125 | $shorttext = \str_replace('%t', (string)$itemRating['nb_vote'], $shorttext); |
||
126 | $itemRating['text'] = $text; |
||
127 | $itemRating['shorttext'] = $shorttext; |
||
128 | $itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; |
||
129 | $itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; |
||
130 | |||
131 | $itemRating['ip'] = $ip; |
||
132 | $itemRating['uid'] = $uid; |
||
133 | $itemRating['voted'] = $voted; |
||
134 | // YouTube Liking ========================================== |
||
135 | } elseif (Constants::RATING_LIKES === (int)$this->helper->getConfig('ratingbars')) { |
||
136 | // get count of "dislikes" |
||
137 | $criteria = new \CriteriaCompo(); |
||
138 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
139 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
140 | $criteria->add(new \Criteria('rate', 0, '<')); |
||
141 | |||
142 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
143 | ->getObjects($criteria); |
||
144 | $count = \count($voteObjs); |
||
145 | |||
146 | foreach ($voteObjs as $voteObj) { |
||
147 | $currentRating += $voteObj->getVar('rate'); |
||
148 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
149 | $voted = true; |
||
150 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
151 | } |
||
152 | } |
||
153 | unset($criteria); |
||
154 | $itemRating['dislikes'] = $count; |
||
155 | |||
156 | // get count of "likes" |
||
157 | $criteria = new \CriteriaCompo(); |
||
158 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
159 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
160 | $criteria->add(new \Criteria('rate', 0, '>')); |
||
161 | |||
162 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
163 | ->getObjects($criteria); |
||
164 | $count = \count($voteObjs); |
||
165 | $currentRating = 0; |
||
166 | foreach ($voteObjs as $voteObj) { |
||
167 | $currentRating += $voteObj->getVar('rate'); |
||
168 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
169 | $voted = true; |
||
170 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
171 | } |
||
172 | } |
||
173 | unset($criteria); |
||
174 | $itemRating['likes'] = $count; |
||
175 | |||
176 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; |
||
177 | $itemRating['ip'] = $ip; |
||
178 | $itemRating['uid'] = $uid; |
||
179 | $itemRating['voted'] = $voted; |
||
180 | // Facebook Reactions ========================================== |
||
181 | } elseif (Constants::RATING_REACTION === (int)$this->helper->getConfig('ratingbars')) { |
||
182 | $criteria = new \CriteriaCompo(); |
||
183 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
184 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
185 | $criteria->add(new \Criteria('rate', 0, '<')); |
||
186 | |||
187 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
188 | ->getObjects($criteria); |
||
189 | $count = \count($voteObjs); |
||
190 | $itemRating['nb_vote'] = $count; |
||
191 | |||
192 | foreach ($voteObjs as $voteObj) { |
||
193 | $currentRating += $voteObj->getVar('rate'); |
||
194 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
195 | $voted = true; |
||
196 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
197 | } |
||
198 | } |
||
199 | unset($criteria); |
||
200 | $itemRating['dislikes'] = $count; |
||
201 | |||
202 | $criteria = new \CriteriaCompo(); |
||
203 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
204 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
205 | $criteria->add(new \Criteria('rate', 0, '>')); |
||
206 | |||
207 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
208 | ->getObjects($criteria); |
||
209 | $count = \count($voteObjs); |
||
210 | $currentRating = 0; |
||
211 | foreach ($voteObjs as $voteObj) { |
||
212 | $currentRating += $voteObj->getVar('rate'); |
||
213 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
214 | $voted = true; |
||
215 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
216 | } |
||
217 | } |
||
218 | unset($criteria); |
||
219 | $itemRating['likes'] = $count; |
||
220 | |||
221 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; |
||
222 | $itemRating['ip'] = $ip; |
||
223 | $itemRating['uid'] = $uid; |
||
224 | $itemRating['voted'] = $voted; |
||
225 | } else { |
||
226 | $itemRating['uid'] = $uid; |
||
227 | $itemRating['nb_vote'] = $count; |
||
228 | $itemRating['voted'] = $voted; |
||
229 | $itemRating['ip'] = $ip; |
||
230 | } |
||
231 | |||
232 | return $itemRating; |
||
233 | } |
||
534 |