Skip to content

Commit 7d1dcd8

Browse files
authored
PHPMD - decrease cyclomatic complexity report level to 10 (#166)
1 parent 14cd569 commit 7d1dcd8

File tree

5 files changed

+100
-55
lines changed

5 files changed

+100
-55
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-1352-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-1353-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='11' />
14+
<property name='reportLevel' value='10' />
1515
</properties>
1616
</rule>
1717
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />

src/Fixer/MultilineCommentOpeningClosingAloneFixer.php

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public function getDefinition(): FixerDefinitionInterface
2121
);
2222
}
2323

24+
public function getPriority(): int
25+
{
26+
// Must be run after MultilineCommentOpeningClosingFixer and NoTrailingWhitespaceInCommentFixer
27+
// Must be run before PhpdocTrimFixer
28+
return -1;
29+
}
30+
2431
public function isCandidate(Tokens $tokens): bool
2532
{
2633
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
@@ -38,45 +45,57 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
3845
continue;
3946
}
4047

41-
$content = $token->getContent();
42-
43-
if (Preg::match('/\R/', $content) !== 1) {
48+
if (Preg::match('/\R/', $token->getContent()) !== 1) {
4449
continue;
4550
}
4651

47-
$toFixOpening = Preg::match('#^/\*+\R#', $content) !== 1;
48-
$toFixClosing = Preg::match('#\R\h*\*+/$#', $content) !== 1;
52+
$this->fixOpening($tokens, $index);
53+
$this->fixClosing($tokens, $index);
54+
}
55+
}
4956

50-
if (!$toFixOpening && !$toFixClosing) {
51-
continue;
52-
}
57+
private function fixOpening(Tokens $tokens, int $index): void
58+
{
59+
$token = $tokens[$index];
5360

54-
Preg::match('#\R(\h*)#', $content, $matches);
55-
$indent = $matches[1];
56-
57-
if ($toFixOpening) {
58-
Preg::match('#^(/\*+)(.*?)(\R)(.*)$#s', $content, $matches);
59-
if ($matches === []) {
60-
continue;
61-
}
62-
if ($matches[2][0] === '/') {
63-
$matches[2] = ' ' . $matches[2];
64-
}
65-
$content = $matches[1] . $matches[3] . $indent . '*' . $matches[2] . $matches[3] . $matches[4];
66-
}
61+
if (Preg::match('#^/\*+\R#', $token->getContent()) === 1) {
62+
return;
63+
}
6764

68-
if ($toFixClosing) {
69-
$content = Preg::replace('#(\R)(.+?)\h*(\*+/)$#', \sprintf('$1$2$1%s$3', $indent), $content);
70-
}
65+
Preg::match('#\R(\h*)#', $token->getContent(), $matches);
66+
$indent = $matches[1];
7167

72-
$tokens[$index] = new Token([$token->getId(), $content]);
68+
Preg::match('#^(/\*+)(.*?)(\R)(.*)$#s', $token->getContent(), $matches);
69+
if ($matches === []) {
70+
return;
71+
}
72+
73+
if ($matches[2][0] === '/') {
74+
$matches[2] = ' ' . $matches[2];
75+
}
76+
77+
$newContent = $matches[1] . $matches[3] . $indent . '*' . $matches[2] . $matches[3] . $matches[4];
78+
79+
if ($newContent !== $token->getContent()) {
80+
$tokens[$index] = new Token([$token->getId(), $newContent]);
7381
}
7482
}
7583

76-
public function getPriority(): int
84+
private function fixClosing(Tokens $tokens, int $index): void
7785
{
78-
// Must be run after MultilineCommentOpeningClosingFixer and NoTrailingWhitespaceInCommentFixer
79-
// Must be run before PhpdocTrimFixer
80-
return -1;
86+
$token = $tokens[$index];
87+
88+
if (Preg::match('#\R\h*\*+/$#', $token->getContent()) === 1) {
89+
return;
90+
}
91+
92+
Preg::match('#\R(\h*)#', $token->getContent(), $matches);
93+
$indent = $matches[1];
94+
95+
$newContent = Preg::replace('#(\R)(.+?)\h*(\*+/)$#', \sprintf('$1$2$1%s$3', $indent), $token->getContent());
96+
97+
if ($newContent !== $token->getContent()) {
98+
$tokens[$index] = new Token([$token->getId(), $newContent]);
99+
}
81100
}
82101
}

src/Fixer/NullableParamStyleFixer.php

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public function configure(?array $configuration = null): void
4545
$this->style = $configuration['style'] ?? $this->style;
4646
}
4747

48+
public function getPriority(): int
49+
{
50+
// must be run before NoUnreachableDefaultArgumentValueFixer
51+
return 1;
52+
}
53+
4854
public function isCandidate(Tokens $tokens): bool
4955
{
5056
return $tokens->isAllTokenKindsFound([T_FUNCTION]);
@@ -67,39 +73,45 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6773

6874
$paramBlockEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $paramBlockStartIndex);
6975

70-
for ($i = $paramBlockEndIndex; $i > $paramBlockStartIndex; $i--) {
71-
if (!$tokens[$i]->equals([T_STRING, 'null'], false)) {
72-
continue;
73-
}
76+
$this->fixFunction($tokens, $paramBlockStartIndex, $paramBlockEndIndex);
77+
}
78+
}
7479

75-
/** @var int $variableIndex */
76-
$variableIndex = $tokens->getPrevTokenOfKind($i, [[T_VARIABLE]]);
80+
private function fixFunction(Tokens $tokens, int $paramBlockStartIndex, int $paramBlockEndIndex): void
81+
{
82+
for ($i = $paramBlockEndIndex; $i > $paramBlockStartIndex; $i--) {
83+
if (!$tokens[$i]->equals([T_STRING, 'null'], false)) {
84+
continue;
85+
}
7786

78-
/** @var int $typeIndex */
79-
$typeIndex = $tokens->getPrevMeaningfulToken($variableIndex);
87+
/** @var int $variableIndex */
88+
$variableIndex = $tokens->getPrevTokenOfKind($i, [[T_VARIABLE]]);
8089

81-
if (!$tokens[$typeIndex]->isGivenKind([CT::T_ARRAY_TYPEHINT, T_CALLABLE, T_STRING])) {
82-
continue;
83-
}
90+
/** @var int $typeIndex */
91+
$typeIndex = $tokens->getPrevMeaningfulToken($variableIndex);
8492

85-
/** @var int $separatorIndex */
86-
$separatorIndex = $tokens->getPrevTokenOfKind($typeIndex, ['(', ',']);
93+
if (!$tokens[$typeIndex]->isGivenKind([CT::T_ARRAY_TYPEHINT, T_CALLABLE, T_STRING])) {
94+
continue;
95+
}
96+
97+
/** @var int $separatorIndex */
98+
$separatorIndex = $tokens->getPrevTokenOfKind($typeIndex, ['(', ',']);
8799

88-
/** @var int $nullableIndex */
89-
$nullableIndex = $tokens->getNextMeaningfulToken($separatorIndex);
100+
/** @var int $nullableIndex */
101+
$nullableIndex = $tokens->getNextMeaningfulToken($separatorIndex);
90102

91-
if ($this->style === 'with_question_mark' && !$tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
103+
if ($this->style === 'with_question_mark') {
104+
if (!$tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
92105
$tokens->insertAt($nullableIndex, new Token([CT::T_NULLABLE_TYPE, '?']));
93-
} elseif ($this->style === 'without_question_mark' && $tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
94-
$tokens->clearAt($nullableIndex);
95106
}
107+
continue;
96108
}
97-
}
98-
}
99109

100-
public function getPriority(): int
101-
{
102-
// must be run before NoUnreachableDefaultArgumentValueFixer
103-
return 1;
110+
if (!$tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
111+
continue;
112+
}
113+
114+
$tokens->clearAt($nullableIndex);
115+
}
104116
}
105117
}

tests/Fixer/NullableParamStyleFixerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ public function provideFixCases(): iterable
8282
['style' => 'without_question_mark'],
8383
];
8484

85+
yield [
86+
'<?php function foo(
87+
int $x = null,
88+
int $y = null,
89+
string $z = NULL
90+
) {}',
91+
'<?php function foo(
92+
?int $x = null,
93+
int $y = null,
94+
?string $z = NULL
95+
) {}',
96+
['style' => 'without_question_mark'],
97+
];
98+
8599
yield [
86100
'<?php function foo(int $x = null) {}',
87101
null,

0 commit comments

Comments
 (0)