Conditions | 5 |
Paths | 27 |
Total Lines | 76 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
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 |
||
61 | protected function execute(InputInterface $input, OutputInterface $output) |
||
62 | { |
||
63 | $config = new Config($input); |
||
64 | |||
65 | $cloverFilePath = $config->getCloverFilePath(); |
||
66 | |||
67 | $cloverFile = CloverFile::fromFile($cloverFilePath, getcwd()); |
||
68 | $output->writeln(sprintf('Code coverage: %.2f%%', $cloverFile->getCoveragePercentage()*100)); |
||
69 | |||
70 | $gitlabApiToken = $config->getGitlabApiToken(); |
||
71 | |||
72 | $gitlabUrl = $config->getGitlabUrl(); |
||
73 | $gitlabApiUrl = $config->getGitlabApiUrl(); |
||
74 | |||
75 | |||
76 | |||
77 | /*$projectId = $input->getOption('gitlab-project-id'); |
||
78 | if ($projectId === null) { |
||
79 | $projectId = getenv('CI_PROJECT_ID'); |
||
80 | if ($projectId === false) { |
||
81 | 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.'); |
||
82 | } |
||
83 | }*/ |
||
84 | |||
85 | $projectName = $config->getGitlabProjectName(); |
||
86 | |||
87 | $buildRef = $config->getGitlabBuildRef(); |
||
88 | |||
89 | $currentBranchName = $config->getCurrentBranchName(); |
||
90 | |||
91 | $client = new Client($gitlabApiUrl); |
||
92 | $client->authenticate($gitlabApiToken); |
||
93 | |||
94 | $diffService = new DiffService(1, 20); |
||
95 | |||
96 | $sendCommentService = new SendCommentService($client, $diffService); |
||
97 | |||
98 | // From CI_BUILD_REF, we can get the commit ( -> project -> build -> commit ) |
||
99 | // From the merge_requests API, we can get the list of commits for a single merge request |
||
100 | // Hence, we can find the merge_request matching a build! |
||
101 | |||
102 | $buildService = new BuildService($client); |
||
103 | |||
104 | try { |
||
105 | $mergeRequest = $buildService->findMergeRequestByBuildRef($projectName, $buildRef); |
||
106 | |||
107 | |||
108 | try { |
||
109 | $previousCloverFile = $this->getCloverFileFromBranch($buildService, $mergeRequest['target_project_id'], $mergeRequest['target_branch']); |
||
110 | } catch (RuntimeException $e) { |
||
111 | if ($e->getCode() === 404) { |
||
112 | // We could not find a previous clover file in the master branch. |
||
113 | // Maybe this branch is the first to contain clover files? |
||
114 | // Let's deal with this by generating a fake "empty" clover file. |
||
115 | $previousCloverFile = EmptyCloverFile::create(); |
||
116 | } else { |
||
117 | throw $e; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | $sendCommentService->sendCodeCoverageCommentToMergeRequest($cloverFile, $previousCloverFile, $projectName, $mergeRequest['id'], $buildRef, $gitlabUrl); |
||
122 | |||
123 | } catch (MergeRequestNotFoundException $e) { |
||
124 | // 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! |
||
125 | |||
126 | $output->writeln('It seems that this CI build is not part of a merge request. Skipping.'); |
||
127 | } |
||
128 | |||
129 | try { |
||
130 | $lastCommitCloverFile = $this->getCloverFileFromBranch($buildService, $mergeRequest['target_project_id'], $currentBranchName); |
||
131 | $sendCommentService->sendDifferencesCommentsInCommit($cloverFile, $lastCommitCloverFile, $projectName, $buildRef, $gitlabUrl); |
||
132 | } catch (BuildNotFoundException $e) { |
||
133 | $output->writeln('Unable to find a previous build for this branch. Skipping adding comments inside the commit. '.$e->getMessage()); |
||
134 | } |
||
135 | |||
136 | } |
||
137 | |||
231 | */ |
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.