| Conditions | 18 |
| Paths | 593 |
| Total Lines | 151 |
| Code Lines | 91 |
| Lines | 24 |
| Ratio | 15.89 % |
| 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 |
||
| 157 | private function getEditSummaryUsage($project, $user, $namespace) |
||
| 158 | { |
||
| 159 | $dbName = $project->getDatabaseName(); |
||
| 160 | |||
| 161 | $cacheKey = 'editsummaryusage.' . $dbName . '.' |
||
| 162 | . $user->getCacheKey() . '.' . $namespace; |
||
| 163 | |||
| 164 | $cache = $this->container->get('cache.app'); |
||
| 165 | if ($cache->hasItem($cacheKey)) { |
||
| 166 | return $cache->getItem($cacheKey)->get(); |
||
| 167 | } |
||
| 168 | |||
| 169 | // Load the database tables |
||
| 170 | $revisionTable = $project->getRepository()->getTableName($dbName, 'revision'); |
||
| 171 | $pageTable = $project->getRepository()->getTableName($dbName, 'page'); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Connection to the replica database |
||
| 175 | * |
||
| 176 | * @var Connection $conn |
||
| 177 | */ |
||
| 178 | $conn = $this->get('doctrine')->getManager('replicas')->getConnection(); |
||
| 179 | |||
| 180 | $condNamespace = $namespace === 'all' ? '' : 'AND page_namespace = :namespace'; |
||
| 181 | $pageJoin = $namespace === 'all' ? '' : "JOIN $pageTable ON rev_page = page_id"; |
||
| 182 | $username = $user->getUsername(); |
||
| 183 | |||
| 184 | // Prepare the query and execute |
||
| 185 | $sql = "SELECT rev_comment, rev_timestamp, rev_minor_edit |
||
| 186 | FROM $revisionTable |
||
| 187 | $pageJoin |
||
| 188 | WHERE rev_user_text = :username |
||
| 189 | $condNamespace |
||
| 190 | ORDER BY rev_timestamp DESC"; |
||
| 191 | |||
| 192 | $resultQuery = $conn->prepare($sql); |
||
| 193 | $resultQuery->bindParam('username', $username); |
||
| 194 | if ($namespace !== 'all') { |
||
| 195 | $resultQuery->bindParam('namespace', $namespace); |
||
| 196 | } |
||
| 197 | $resultQuery->execute(); |
||
| 198 | |||
| 199 | View Code Duplication | if ($resultQuery->errorCode() > 0) { |
|
| 200 | $this->addFlash('notice', ['no-result', $username]); |
||
| 201 | return $this->redirectToRoute( |
||
| 202 | 'EditSummaryProject', |
||
| 203 | [ |
||
| 204 | 'project' => $project->getDomain() |
||
| 205 | ] |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Set defaults, so we don't get variable undefined errors |
||
| 210 | $totalSummariesMajor = 0; |
||
| 211 | $totalSummariesMinor = 0; |
||
| 212 | $totalEditsMajor = 0; |
||
| 213 | $totalEditsMinor = 0; |
||
| 214 | $recentEditsMajor = 0; |
||
| 215 | $recentEditsMinor = 0; |
||
| 216 | $recentSummariesMajor = 0; |
||
| 217 | $recentSummariesMinor = 0; |
||
| 218 | $monthTotals = []; |
||
| 219 | $monthEditsummaryTotals = []; |
||
| 220 | $totalEdits = 0; |
||
| 221 | $totalSummaries = 0; |
||
| 222 | |||
| 223 | while ($row = $resultQuery->fetch()) { |
||
| 224 | // Extract the date out of the date field |
||
| 225 | $timestamp = DateTime::createFromFormat('YmdHis', $row['rev_timestamp']); |
||
| 226 | |||
| 227 | $monthkey = date_format($timestamp, 'Y-m'); |
||
| 228 | |||
| 229 | // Check and see if the month is set for all major edits edits. |
||
| 230 | // If not, default it to 1. |
||
| 231 | if (!isset($monthTotals[$monthkey])) { |
||
| 232 | $monthTotals[$monthkey] = 1; |
||
| 233 | } else { |
||
| 234 | $monthTotals[$monthkey]++; |
||
| 235 | } |
||
| 236 | |||
| 237 | // Grand total for number of edits |
||
| 238 | $totalEdits++; |
||
| 239 | |||
| 240 | // Total edit summaries |
||
| 241 | if ($row['rev_comment'] !== '') { |
||
| 242 | $totalSummaries++; |
||
| 243 | } |
||
| 244 | |||
| 245 | // Now do the same, if we have an edit summary |
||
| 246 | if ($row['rev_minor_edit'] == 0) { |
||
| 247 | View Code Duplication | if ($row['rev_comment'] !== '') { |
|
| 248 | isset($monthEditsummaryTotals[$monthkey]) ? |
||
| 249 | $monthEditsummaryTotals[$monthkey]++ : |
||
| 250 | $monthEditsummaryTotals[$monthkey] = 1; |
||
| 251 | $totalSummariesMajor++; |
||
| 252 | } |
||
| 253 | |||
| 254 | // Now do the same for recent edits |
||
| 255 | $totalEditsMajor++; |
||
| 256 | if ($recentEditsMajor < 150) { |
||
| 257 | $recentEditsMajor++; |
||
| 258 | if ($row['rev_comment'] != '') { |
||
| 259 | $recentSummariesMajor++; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | // The exact same procedure as documented above for minor edits |
||
| 264 | // If there is a comment, count it |
||
| 265 | View Code Duplication | if ($row['rev_comment'] !== '') { |
|
| 266 | isset($monthEditsummaryTotals[$monthkey]) ? |
||
| 267 | $monthEditsummaryTotals[$monthkey]++ : |
||
| 268 | $monthEditsummaryTotals[$monthkey] = 1; |
||
| 269 | $totalSummariesMinor++; |
||
| 270 | $totalEditsMinor++; |
||
| 271 | } else { |
||
| 272 | $totalEditsMinor++; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Handle recent edits |
||
| 276 | if ($recentEditsMinor < 150) { |
||
| 277 | $recentEditsMinor++; |
||
| 278 | if ($row['rev_comment'] != '') { |
||
| 279 | $recentSummariesMinor++; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | $result = [ |
||
| 286 | 'totalEdits' => $totalEdits, |
||
| 287 | 'totalEditsMajor' => $totalEditsMajor, |
||
| 288 | 'totalEditsMinor' => $totalEditsMinor, |
||
| 289 | 'totalSummaries' => $totalSummaries, |
||
| 290 | 'totalSummariesMajor' => $totalSummariesMajor, |
||
| 291 | 'totalSummariesMinor' => $totalSummariesMinor, |
||
| 292 | 'recentEditsMajor' => $recentEditsMajor, |
||
| 293 | 'recentEditsMinor' => $recentEditsMinor, |
||
| 294 | 'recentSummariesMajor' => $recentSummariesMajor, |
||
| 295 | 'recentSummariesMinor' => $recentSummariesMinor, |
||
| 296 | 'monthTotals' => $monthTotals, |
||
| 297 | 'monthEditSumTotals' => $monthEditsummaryTotals, |
||
| 298 | ]; |
||
| 299 | |||
| 300 | // Cache for 10 minutes, and return. |
||
| 301 | $cacheItem = $cache->getItem($cacheKey) |
||
| 302 | ->set($result) |
||
| 303 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 304 | $cache->save($cacheItem); |
||
| 305 | |||
| 306 | return $result; |
||
| 307 | } |
||
| 308 | } |
||
| 309 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: