Skip to content

Commit 1753f57

Browse files
authored
PHPMD - decrease cyclomatic complexity report level to 9 (#168)
1 parent b1856e3 commit 1753f57

9 files changed

+237
-139
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![Build status](https://img.shields.io/travis/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://travis-ci.org/kubawerlos/php-cs-fixer-custom-fixers)
1010
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
11-
![Tests](https://img.shields.io/badge/tests-1353-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-1358-brightgreen.svg)
1212
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
1313
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)
1414

phpmd.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<rule ref='rulesets/codesize.xml/CyclomaticComplexity'>
1313
<properties>
14-
<property name='reportLevel' value='10' />
14+
<property name='reportLevel' value='9' />
1515
</properties>
1616
</rule>
1717
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />

src/Fixer/PhpdocParamOrderFixer.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ function foo($a, $b, $c) {}
3131
);
3232
}
3333

34+
public function getPriority(): int
35+
{
36+
return 5;
37+
}
38+
3439
public function isCandidate(Tokens $tokens): bool
3540
{
3641
return $tokens->isAllTokenKindsFound([T_DOC_COMMENT, T_FUNCTION]);
@@ -55,21 +60,27 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5560

5661
$paramNames = $this->getParamNames($tokens, $functionIndex);
5762

58-
$newContent = $this->getSortedDocComment($tokens[$index]->getContent(), $paramNames);
63+
$docBlock = new DocBlock($tokens[$index]->getContent());
64+
$sorted = $this->getSortedAnnotations($docBlock->getAnnotations(), $paramNames);
65+
66+
foreach ($sorted as $annotationIndex => $annotationContent) {
67+
/** @var Annotation $annotation */
68+
$annotation = $docBlock->getAnnotation($annotationIndex);
69+
$annotation->remove();
5970

60-
if ($newContent === $tokens[$index]->getContent()) {
71+
/** @var Line $line */
72+
$line = $docBlock->getLine($annotation->getStart());
73+
$line->setContent($annotationContent);
74+
}
75+
76+
if ($docBlock->getContent() === $tokens[$index]->getContent()) {
6177
continue;
6278
}
6379

64-
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
80+
$tokens[$index] = new Token([T_DOC_COMMENT, $docBlock->getContent()]);
6581
}
6682
}
6783

68-
public function getPriority(): int
69-
{
70-
return 5;
71-
}
72-
7384
/**
7485
* @return string[]
7586
*/
@@ -90,25 +101,17 @@ private function getParamNames(Tokens $tokens, int $functionIndex): array
90101
return $paramNames;
91102
}
92103

93-
private function getSortedDocComment(string $comment, array $paramNames): string
104+
private function getSortedAnnotations(array $annotations, array $paramNames): array
94105
{
95-
$docBlock = new DocBlock($comment);
96-
$firstParamIndex = null;
106+
$paramFound = false;
97107
$annotationsBeforeParams = [];
98108
$paramsByName = \array_combine($paramNames, \array_fill(0, \count($paramNames), null));
99109
$superfluousParams = [];
100110
$annotationsAfterParams = [];
101111

102-
foreach ($docBlock->getAnnotations() as $index => $annotation) {
103-
if ($firstParamIndex === null) {
104-
if ($annotation->getTag()->getName() !== 'param') {
105-
$annotationsBeforeParams[] = $annotation->getContent();
106-
continue;
107-
}
108-
$firstParamIndex = $index;
109-
}
110-
112+
foreach ($annotations as $annotation) {
111113
if ($annotation->getTag()->getName() === 'param') {
114+
$paramFound = true;
112115
foreach ($paramNames as $paramName) {
113116
if (Preg::match(\sprintf('/@param\s+(?:[^\$](?:[^<\s]|<[^>]*>)*\s+)?(?:&|\.\.\.)?\s*(\Q%s\E)\b/', $paramName), $annotation->getContent(), $matches) === 1 && !isset($paramsByName[$matches[1]])) {
114117
$paramsByName[$matches[1]] = $annotation->getContent();
@@ -119,21 +122,14 @@ private function getSortedDocComment(string $comment, array $paramNames): string
119122
continue;
120123
}
121124

122-
$annotationsAfterParams[] = $annotation->getContent();
123-
}
124-
125-
$sorted = \array_merge($annotationsBeforeParams, \array_values(\array_filter($paramsByName)), $superfluousParams, $annotationsAfterParams);
126-
127-
foreach ($sorted as $index => $annotationContent) {
128-
/** @var Annotation $annotation */
129-
$annotation = $docBlock->getAnnotation($index);
130-
$annotation->remove();
125+
if ($paramFound) {
126+
$annotationsAfterParams[] = $annotation->getContent();
127+
continue;
128+
}
131129

132-
/** @var Line $line */
133-
$line = $docBlock->getLine($annotation->getStart());
134-
$line->setContent($annotationContent);
130+
$annotationsBeforeParams[] = $annotation->getContent();
135131
}
136132

137-
return $docBlock->getContent();
133+
return \array_merge($annotationsBeforeParams, \array_values(\array_filter($paramsByName)), $superfluousParams, $annotationsAfterParams);
138134
}
139135
}

src/Fixer/SingleSpaceAfterStatementFixer.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public function getConfigurationDefinition(): FixerConfigurationResolver
8686
{
8787
return new FixerConfigurationResolver([
8888
(new FixerOptionBuilder('allow_linebreak', 'whether to allow statement followed by linebreak'))
89-
->setDefault($this->allowLinebreak)
9089
->setAllowedTypes(['bool'])
90+
->setDefault($this->allowLinebreak)
9191
->getOption(),
9292
]);
9393
}
@@ -97,6 +97,11 @@ public function configure(?array $configuration = null): void
9797
$this->allowLinebreak = $configuration['allow_linebreak'] ?? $this->allowLinebreak;
9898
}
9999

100+
public function getPriority(): int
101+
{
102+
return 0;
103+
}
104+
100105
public function isCandidate(Tokens $tokens): bool
101106
{
102107
return $tokens->isAnyTokenKindsFound($this->tokens);
@@ -114,26 +119,29 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
114119
continue;
115120
}
116121

117-
if (!$tokens[$index + 1]->isGivenKind(T_WHITESPACE)) {
118-
if ($token->isGivenKind(T_CLASS) && $tokens[$index + 1]->getContent() === '(') {
119-
continue;
120-
}
121-
if (!\in_array($tokens[$index + 1]->getContent(), [';', ':'], true)) {
122-
$tokens->insertAt($index + 1, new Token([T_WHITESPACE, ' ']));
123-
}
122+
if (!$this->canAddSpaceAfter($tokens, $index)) {
124123
continue;
125124
}
126125

127-
if ($this->allowLinebreak && Preg::match('/\R/', $tokens[$index + 1]->getContent()) === 1) {
126+
if ($tokens[$index + 1]->isGivenKind(T_WHITESPACE)) {
127+
$tokens[$index + 1] = new Token([T_WHITESPACE, ' ']);
128128
continue;
129129
}
130130

131-
$tokens[$index + 1] = new Token([T_WHITESPACE, ' ']);
131+
$tokens->insertAt($index + 1, new Token([T_WHITESPACE, ' ']));
132132
}
133133
}
134134

135-
public function getPriority(): int
135+
private function canAddSpaceAfter(Tokens $tokens, int $index): bool
136136
{
137-
return 0;
137+
if ($tokens[$index + 1]->isGivenKind(T_WHITESPACE)) {
138+
return !$this->allowLinebreak || Preg::match('/\R/', $tokens[$index + 1]->getContent()) !== 1;
139+
}
140+
141+
if ($tokens[$index]->isGivenKind(T_CLASS) && $tokens[$index + 1]->getContent() === '(') {
142+
return false;
143+
}
144+
145+
return !\in_array($tokens[$index + 1]->getContent(), [';', ':'], true);
138146
}
139147
}

src/Fixer/SingleSpaceBeforeStatementFixer.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public function getDefinition(): FixerDefinitionInterface
7676
);
7777
}
7878

79+
public function getPriority(): int
80+
{
81+
return 0;
82+
}
83+
7984
public function isCandidate(Tokens $tokens): bool
8085
{
8186
return $tokens->isAnyTokenKindsFound($this->tokens);
@@ -93,31 +98,38 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
9398
continue;
9499
}
95100

96-
if (!$tokens[$index - 1]->isGivenKind(T_WHITESPACE)) {
97-
if (!\in_array($tokens[$index - 1]->getContent(), ['!', '(', '@', '[', '{'], true) && !$tokens[$index - 1]->isGivenKind(T_OPEN_TAG)) {
98-
$tokens->insertAt($index, new Token([T_WHITESPACE, ' ']));
99-
}
101+
if ($tokens[$index - 1]->isGivenKind(T_OPEN_TAG)) {
100102
continue;
101103
}
102104

103-
if (Preg::match('/\R/', $tokens[$index - 1]->getContent()) === 1) {
105+
if ($tokens[$index - 2]->isGivenKind(T_OPEN_TAG)) {
106+
$this->fixTwoTokensAfterOpenTag($tokens, $index);
104107
continue;
105108
}
106109

107-
if ($tokens[$index - 2]->isGivenKind(T_OPEN_TAG)) {
108-
if (Preg::match('/\R/', $tokens[$index - 2]->getContent()) !== 1) {
109-
$tokens->clearAt($index - 1);
110-
}
111-
112-
return;
113-
}
110+
$this->fixMoreThanTwoTokensAfterOpenTag($tokens, $index);
111+
}
112+
}
114113

115-
$tokens[$index - 1] = new Token([T_WHITESPACE, ' ']);
114+
private function fixTwoTokensAfterOpenTag(Tokens $tokens, int $index): void
115+
{
116+
if ($tokens[$index - 1]->isGivenKind(T_WHITESPACE) && Preg::match('/\R/', $tokens[$index - 2]->getContent()) !== 1) {
117+
$tokens->clearAt($index - 1);
116118
}
117119
}
118120

119-
public function getPriority(): int
121+
private function fixMoreThanTwoTokensAfterOpenTag(Tokens $tokens, int $index): void
120122
{
121-
return 0;
123+
if ($tokens[$index - 1]->isGivenKind(T_WHITESPACE)) {
124+
if (Preg::match('/\R/', $tokens[$index - 1]->getContent()) !== 1) {
125+
$tokens[$index - 1] = new Token([T_WHITESPACE, ' ']);
126+
}
127+
128+
return;
129+
}
130+
131+
if (!\in_array($tokens[$index - 1]->getContent(), ['!', '(', '@', '[', '{'], true)) {
132+
$tokens->insertAt($index, new Token([T_WHITESPACE, ' ']));
133+
}
122134
}
123135
}

src/TokenRemover.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,43 @@ public static function removeWithLinesIfPossible(Tokens $tokens, int $index): vo
3131
}
3232

3333
private static function isTokenOnlyMeaningfulInLine(Tokens $tokens, int $index): bool
34+
{
35+
return !self::hasMeaningTokenInLineBefore($tokens, $index) && !self::hasMeaningTokenInLineAfter($tokens, $index);
36+
}
37+
38+
private static function hasMeaningTokenInLineBefore(Tokens $tokens, int $index): bool
3439
{
3540
/** @var int $prevIndex */
3641
$prevIndex = $tokens->getNonEmptySibling($index, -1);
3742
if (!$tokens[$prevIndex]->isGivenKind([T_OPEN_TAG, T_WHITESPACE])) {
38-
return false;
43+
return true;
3944
}
4045

4146
if ($tokens[$prevIndex]->isGivenKind(T_OPEN_TAG) && Preg::match('/\R$/', $tokens[$prevIndex]->getContent()) !== 1) {
42-
return false;
47+
return true;
4348
}
4449

4550
if (Preg::match('/\R/', $tokens[$prevIndex]->getContent()) !== 1) {
4651
$prevPrevIndex = $tokens->getNonEmptySibling($prevIndex, -1);
4752
if (!$tokens[$prevPrevIndex]->isGivenKind(T_OPEN_TAG) || Preg::match('/\R$/', $tokens[$prevPrevIndex]->getContent()) !== 1) {
48-
return false;
53+
return true;
4954
}
5055
}
5156

57+
return false;
58+
}
59+
60+
private static function hasMeaningTokenInLineAfter(Tokens $tokens, int $index): bool
61+
{
5262
$nextIndex = $tokens->getNonEmptySibling($index, 1);
5363
if ($nextIndex === null) {
54-
return true;
64+
return false;
5565
}
5666
if (!$tokens[$nextIndex]->isGivenKind(T_WHITESPACE)) {
57-
return false;
67+
return true;
5868
}
5969

60-
return Preg::match('/\R/', $tokens[$nextIndex]->getContent()) === 1;
70+
return Preg::match('/\R/', $tokens[$nextIndex]->getContent()) !== 1;
6171
}
6272

6373
private static function handleWhitespaceBefore(Tokens $tokens, int $index): bool

tests/Fixer/PhpdocParamOrderFixerTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,95 @@ function foo($a) {}
199199
function bar($a, $b) {}
200200
',
201201
];
202+
203+
yield [
204+
'<?php
205+
/**
206+
* @author John Doe
207+
*
208+
* @param bool $a
209+
* @param bool $b
210+
*/
211+
function foo($a, $b) {}
212+
',
213+
'<?php
214+
/**
215+
* @author John Doe
216+
*
217+
* @param bool $b
218+
* @param bool $a
219+
*/
220+
function foo($a, $b) {}
221+
',
222+
];
223+
224+
yield [
225+
'<?php
226+
/**
227+
* @param bool $a
228+
* @param bool $b
229+
*
230+
* @see example.com
231+
*/
232+
function foo($a, $b) {}
233+
',
234+
'<?php
235+
/**
236+
* @param bool $b
237+
* @param bool $a
238+
*
239+
* @see example.com
240+
*/
241+
function foo($a, $b) {}
242+
',
243+
];
244+
245+
yield [
246+
'<?php
247+
/**
248+
* @author John Doe
249+
*
250+
* @param bool $a
251+
* @param bool $b
252+
*
253+
* @see example.com
254+
*/
255+
function foo($a, $b) {}
256+
',
257+
'<?php
258+
/**
259+
* @author John Doe
260+
*
261+
* @param bool $b
262+
* @param bool $a
263+
*
264+
* @see example.com
265+
*/
266+
function foo($a, $b) {}
267+
',
268+
];
269+
270+
yield [
271+
'<?php
272+
/**
273+
* @author John Doe
274+
* @param bool $a
275+
* @param bool $b
276+
* @version 2.0
277+
* @see example.com
278+
*/
279+
function foo($a, $b) {}
280+
',
281+
'<?php
282+
/**
283+
* @author John Doe
284+
* @param bool $b
285+
* @version 2.0
286+
* @param bool $a
287+
* @see example.com
288+
*/
289+
function foo($a, $b) {}
290+
',
291+
];
202292
}
203293
}

0 commit comments

Comments
 (0)