| Conditions | 17 |
| Paths | 16900 |
| Total Lines | 145 |
| Code Lines | 79 |
| Lines | 10 |
| Ratio | 6.9 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 88 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 89 | { |
||
| 90 | $config = new Config($input); |
||
| 91 | |||
| 92 | $cloverFilePath = $config->getCloverFilePath(); |
||
| 93 | |||
| 94 | $cloverFile = null; |
||
| 95 | if (file_exists($cloverFilePath)) { |
||
| 96 | $cloverFile = CloverFile::fromFile($cloverFilePath, getcwd()); |
||
| 97 | //$output->writeln(sprintf('Code coverage: %.2f%%', $cloverFile->getCoveragePercentage() * 100)); |
||
| 98 | } |
||
| 99 | |||
| 100 | $crap4JFilePath = $config->getCrap4JFilePath(); |
||
| 101 | |||
| 102 | $crap4jFile = null; |
||
| 103 | if (file_exists($crap4JFilePath)) { |
||
| 104 | $crap4jFile = Crap4JFile::fromFile($crap4JFilePath); |
||
| 105 | } |
||
| 106 | |||
| 107 | $files = $config->getFiles(); |
||
| 108 | |||
| 109 | $methodsProvider = null; |
||
| 110 | $codeCoverageProvider = null; |
||
| 111 | |||
| 112 | if ($cloverFile !== null && $crap4jFile !== null) { |
||
| 113 | $methodsProvider = new CrapMethodMerger($cloverFile, $crap4jFile); |
||
| 114 | $codeCoverageProvider = $cloverFile; |
||
| 115 | } elseif ($cloverFile !== null) { |
||
| 116 | $methodsProvider = $cloverFile; |
||
| 117 | $codeCoverageProvider = $cloverFile; |
||
| 118 | } elseif ($crap4jFile !== null) { |
||
| 119 | $methodsProvider = $crap4jFile; |
||
| 120 | } elseif (empty($files)) { |
||
| 121 | throw new \RuntimeException('Could not find neither clover file, nor crap4j file for analysis nor files to send in comments. Nothing done. Searched paths: '.$cloverFilePath.' and '.$crap4JFilePath); |
||
| 122 | } |
||
| 123 | |||
| 124 | $gitlabApiToken = $config->getGitlabApiToken(); |
||
| 125 | |||
| 126 | $gitlabUrl = $config->getGitlabUrl(); |
||
| 127 | $gitlabApiUrl = $config->getGitlabApiUrl(); |
||
| 128 | |||
| 129 | |||
| 130 | /*$projectId = $input->getOption('gitlab-project-id'); |
||
| 131 | if ($projectId === null) { |
||
| 132 | $projectId = getenv('CI_PROJECT_ID'); |
||
| 133 | if ($projectId === false) { |
||
| 134 | throw new \RuntimeException('Could not find the Gitlab project ID in the "CI_PROJECT_ID" environment variable (usually set by Gitlab CI). Either set this environment variable or pass the ID via the --gitlab-project-id command line option.'); |
||
| 135 | } |
||
| 136 | }*/ |
||
| 137 | |||
| 138 | $projectName = $config->getGitlabProjectName(); |
||
| 139 | |||
| 140 | $commitSha = $config->getCommitSha(); |
||
| 141 | |||
| 142 | $currentBranchName = $config->getCurrentBranchName(); |
||
| 143 | |||
| 144 | $client = Client::create($gitlabApiUrl); |
||
| 145 | $client->authenticate($gitlabApiToken); |
||
| 146 | |||
| 147 | $diffService = new DiffService(1, 20); |
||
| 148 | |||
| 149 | $sendCommentService = new SendCommentService($client, $diffService); |
||
| 150 | |||
| 151 | // From CI_COMMIT_SHA, we can get the commit ( -> project -> build -> commit ) |
||
| 152 | // From the merge_requests API, we can get the list of commits for a single merge request |
||
| 153 | // Hence, we can find the merge_request matching a build! |
||
| 154 | |||
| 155 | $buildService = new BuildService($client); |
||
| 156 | |||
| 157 | $inMergeRequest = false; |
||
| 158 | |||
| 159 | try { |
||
| 160 | $mergeRequest = $buildService->findMergeRequestByCommitSha($projectName, $commitSha); |
||
| 161 | |||
| 162 | $repo = new GitRepository(getcwd()); |
||
| 163 | $targetCommit = $repo->getLatestCommitForBranch('origin/'.$mergeRequest['target_branch']); |
||
| 164 | $lastCommonCommit = $repo->getMergeBase($targetCommit, $commitSha); |
||
| 165 | |||
| 166 | |||
| 167 | list($previousCodeCoverageProvider, $previousMethodsProvider) = $this->getMeasuresFromCommit($buildService, $mergeRequest['target_project_id'], $lastCommonCommit, $cloverFilePath, $crap4JFilePath, $config->getJobStage()); |
||
| 168 | //list($previousCodeCoverageProvider, $previousMethodsProvider) = $this->getMeasuresFromBranch($buildService, $mergeRequest['target_project_id'], $mergeRequest['target_branch'], $cloverFilePath, $crap4JFilePath); |
||
| 169 | |||
| 170 | $message = new Message(); |
||
| 171 | if ($codeCoverageProvider !== null) { |
||
| 172 | $message->addCoverageMessage($codeCoverageProvider, $previousCodeCoverageProvider); |
||
| 173 | } else { |
||
| 174 | $output->writeln('Could not find clover file for code coverage analysis.'); |
||
| 175 | } |
||
| 176 | View Code Duplication | if ($methodsProvider !== null) { |
|
| 177 | $message->addDifferencesHtml($methodsProvider, $previousMethodsProvider, $diffService, $commitSha, $gitlabUrl, $projectName); |
||
| 178 | } else { |
||
| 179 | $output->writeln('Could not find clover file nor crap4j file for CRAP score analysis.'); |
||
| 180 | } |
||
| 181 | |||
| 182 | $this->addFilesToMessage($message, $files, $output, $config); |
||
| 183 | |||
| 184 | $client->merge_requests->addComment($projectName, $mergeRequest['id'], (string) $message); |
||
| 185 | |||
| 186 | $inMergeRequest = true; |
||
| 187 | } catch (MergeRequestNotFoundException $e) { |
||
| 188 | // If there is no merge request attached to this build, let's skip the merge request comment. We can still make some comments on the commit itself! |
||
| 189 | $output->writeln('It seems that this CI build is not part of a merge request.'); |
||
| 190 | } |
||
| 191 | |||
| 192 | try { |
||
| 193 | $targetProjectId = $mergeRequest['target_project_id'] ?? $projectName; |
||
| 194 | list($lastCommitCoverage, $lastCommitMethodsProvider) = $this->getMeasuresFromBranch($buildService, $targetProjectId, $currentBranchName, $cloverFilePath, $crap4JFilePath, $config->getJobStage()); |
||
| 195 | |||
| 196 | $sendCommentService->sendDifferencesCommentsInCommit($methodsProvider, $lastCommitMethodsProvider, $projectName, $commitSha, $gitlabUrl); |
||
| 197 | |||
| 198 | |||
| 199 | if ($config->isOpenIssue() && !$inMergeRequest) { |
||
| 200 | $message = new Message(); |
||
| 201 | |||
| 202 | if ($codeCoverageProvider !== null) { |
||
| 203 | $message->addCoverageMessage($codeCoverageProvider, $lastCommitCoverage); |
||
| 204 | } else { |
||
| 205 | $output->writeln('Could not find clover file for code coverage analysis.'); |
||
| 206 | } |
||
| 207 | |||
| 208 | View Code Duplication | if ($methodsProvider !== null) { |
|
| 209 | $message->addDifferencesHtml($methodsProvider, $lastCommitMethodsProvider, $diffService, $commitSha, $gitlabUrl, $projectName); |
||
| 210 | } else { |
||
| 211 | $output->writeln('Could not find clover file nor crap4j file for CRAP score analysis.'); |
||
| 212 | } |
||
| 213 | |||
| 214 | $this->addFilesToMessage($message, $files, $output, $config); |
||
| 215 | |||
| 216 | $project = new Project($projectName, $client); |
||
| 217 | |||
| 218 | $options = [ |
||
| 219 | 'description' => (string) $message |
||
| 220 | ]; |
||
| 221 | |||
| 222 | $userId = $this->getCommiterId($client, $project, $commitSha); |
||
| 223 | if ($userId !== null) { |
||
| 224 | $options['assignee_id'] = $userId; |
||
| 225 | } |
||
| 226 | |||
| 227 | $project->createIssue('Build failed', $options); |
||
| 228 | } |
||
| 229 | } catch (BuildNotFoundException $e) { |
||
| 230 | $output->writeln('Unable to find a previous build for this branch. Skipping adding comments inside the commit. '.$e->getMessage()); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 439 | */ |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.