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