| 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 |
||
| 115 | private function getEditSummaryUsage(Project $project, User $user, $namespace) |
||
| 116 | { |
||
| 117 | $dbName = $project->getDatabaseName(); |
||
| 118 | |||
| 119 | $cacheKey = 'editsummaryusage.' . $dbName . '.' |
||
| 120 | . $user->getCacheKey() . '.' . $namespace; |
||
| 121 | |||
| 122 | $cache = $this->container->get('cache.app'); |
||
| 123 | if ($cache->hasItem($cacheKey)) { |
||
| 124 | return $cache->getItem($cacheKey)->get(); |
||
| 125 | } |
||
| 126 | |||
| 127 | // Load the database tables |
||
| 128 | $revisionTable = $project->getRepository()->getTableName($dbName, 'revision'); |
||
| 129 | $pageTable = $project->getRepository()->getTableName($dbName, 'page'); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Connection to the replica database |
||
| 133 | * |
||
| 134 | * @var Connection $conn |
||
| 135 | */ |
||
| 136 | $conn = $this->get('doctrine')->getManager('replicas')->getConnection(); |
||
| 137 | |||
| 138 | $condNamespace = $namespace === 'all' ? '' : 'AND page_namespace = :namespace'; |
||
| 139 | $pageJoin = $namespace === 'all' ? '' : "JOIN $pageTable ON rev_page = page_id"; |
||
| 140 | $username = $user->getUsername(); |
||
| 141 | |||
| 142 | // Prepare the query and execute |
||
| 143 | $sql = "SELECT rev_comment, rev_timestamp, rev_minor_edit |
||
| 144 | FROM $revisionTable |
||
| 145 | $pageJoin |
||
| 146 | WHERE rev_user_text = :username |
||
| 147 | $condNamespace |
||
| 148 | ORDER BY rev_timestamp DESC"; |
||
| 149 | |||
| 150 | $resultQuery = $conn->prepare($sql); |
||
| 151 | $resultQuery->bindParam('username', $username); |
||
| 152 | if ($namespace !== 'all') { |
||
| 153 | $resultQuery->bindParam('namespace', $namespace); |
||
| 154 | } |
||
| 155 | $resultQuery->execute(); |
||
| 156 | |||
| 157 | View Code Duplication | if ($resultQuery->errorCode() > 0) { |
|
| 158 | $this->addFlash('notice', ['no-result', $username]); |
||
| 159 | return $this->redirectToRoute( |
||
| 160 | 'EditSummaryProject', |
||
| 161 | [ |
||
| 162 | 'project' => $project->getDomain() |
||
| 163 | ] |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Set defaults, so we don't get variable undefined errors |
||
| 168 | $totalSummariesMajor = 0; |
||
| 169 | $totalSummariesMinor = 0; |
||
| 170 | $totalEditsMajor = 0; |
||
| 171 | $totalEditsMinor = 0; |
||
| 172 | $recentEditsMajor = 0; |
||
| 173 | $recentEditsMinor = 0; |
||
| 174 | $recentSummariesMajor = 0; |
||
| 175 | $recentSummariesMinor = 0; |
||
| 176 | $monthTotals = []; |
||
| 177 | $monthEditsummaryTotals = []; |
||
| 178 | $totalEdits = 0; |
||
| 179 | $totalSummaries = 0; |
||
| 180 | |||
| 181 | while ($row = $resultQuery->fetch()) { |
||
| 182 | // Extract the date out of the date field |
||
| 183 | $timestamp = DateTime::createFromFormat('YmdHis', $row['rev_timestamp']); |
||
| 184 | |||
| 185 | $monthkey = date_format($timestamp, 'Y-m'); |
||
| 186 | |||
| 187 | // Check and see if the month is set for all major edits edits. |
||
| 188 | // If not, default it to 1. |
||
| 189 | if (!isset($monthTotals[$monthkey])) { |
||
| 190 | $monthTotals[$monthkey] = 1; |
||
| 191 | } else { |
||
| 192 | $monthTotals[$monthkey]++; |
||
| 193 | } |
||
| 194 | |||
| 195 | // Grand total for number of edits |
||
| 196 | $totalEdits++; |
||
| 197 | |||
| 198 | // Total edit summaries |
||
| 199 | if ($row['rev_comment'] !== '') { |
||
| 200 | $totalSummaries++; |
||
| 201 | } |
||
| 202 | |||
| 203 | // Now do the same, if we have an edit summary |
||
| 204 | if ($row['rev_minor_edit'] == 0) { |
||
| 205 | View Code Duplication | if ($row['rev_comment'] !== '') { |
|
| 206 | isset($monthEditsummaryTotals[$monthkey]) ? |
||
| 207 | $monthEditsummaryTotals[$monthkey]++ : |
||
| 208 | $monthEditsummaryTotals[$monthkey] = 1; |
||
| 209 | $totalSummariesMajor++; |
||
| 210 | } |
||
| 211 | |||
| 212 | // Now do the same for recent edits |
||
| 213 | $totalEditsMajor++; |
||
| 214 | if ($recentEditsMajor < 150) { |
||
| 215 | $recentEditsMajor++; |
||
| 216 | if ($row['rev_comment'] != '') { |
||
| 217 | $recentSummariesMajor++; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } else { |
||
| 221 | // The exact same procedure as documented above for minor edits |
||
| 222 | // If there is a comment, count it |
||
| 223 | View Code Duplication | if ($row['rev_comment'] !== '') { |
|
| 224 | isset($monthEditsummaryTotals[$monthkey]) ? |
||
| 225 | $monthEditsummaryTotals[$monthkey]++ : |
||
| 226 | $monthEditsummaryTotals[$monthkey] = 1; |
||
| 227 | $totalSummariesMinor++; |
||
| 228 | $totalEditsMinor++; |
||
| 229 | } else { |
||
| 230 | $totalEditsMinor++; |
||
| 231 | } |
||
| 232 | |||
| 233 | // Handle recent edits |
||
| 234 | if ($recentEditsMinor < 150) { |
||
| 235 | $recentEditsMinor++; |
||
| 236 | if ($row['rev_comment'] != '') { |
||
| 237 | $recentSummariesMinor++; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | $result = [ |
||
| 244 | 'totalEdits' => $totalEdits, |
||
| 245 | 'totalEditsMajor' => $totalEditsMajor, |
||
| 246 | 'totalEditsMinor' => $totalEditsMinor, |
||
| 247 | 'totalSummaries' => $totalSummaries, |
||
| 248 | 'totalSummariesMajor' => $totalSummariesMajor, |
||
| 249 | 'totalSummariesMinor' => $totalSummariesMinor, |
||
| 250 | 'recentEditsMajor' => $recentEditsMajor, |
||
| 251 | 'recentEditsMinor' => $recentEditsMinor, |
||
| 252 | 'recentSummariesMajor' => $recentSummariesMajor, |
||
| 253 | 'recentSummariesMinor' => $recentSummariesMinor, |
||
| 254 | 'monthTotals' => $monthTotals, |
||
| 255 | 'monthEditSumTotals' => $monthEditsummaryTotals, |
||
| 256 | ]; |
||
| 257 | |||
| 258 | // Cache for 10 minutes, and return. |
||
| 259 | $cacheItem = $cache->getItem($cacheKey) |
||
| 260 | ->set($result) |
||
| 261 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 262 | $cache->save($cacheItem); |
||
| 263 | |||
| 264 | return $result; |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.