Skip to content

Commit aff40bd

Browse files
authored
PHPMD - decrease cyclomatic complexity report level to 11 (#164)
1 parent ff7bc5f commit aff40bd

File tree

5 files changed

+88
-41
lines changed

5 files changed

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

src/Fixer/InternalClassCasingFixer.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,7 @@ private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool
5353
continue;
5454
}
5555

56-
/** @var int $prevIndex */
57-
$prevIndex = $tokens->getPrevMeaningfulToken($index);
58-
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
59-
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
60-
if ($tokens[$prevIndex]->isGivenKind(T_STRING)) {
61-
continue;
62-
}
63-
} elseif (!$isInGlobalNamespace) {
64-
continue;
65-
}
66-
67-
if ($tokens[$prevIndex]->isGivenKind([T_AS, T_CLASS, T_CONST, T_DOUBLE_COLON, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
68-
continue;
69-
}
70-
71-
$nextIndex = $tokens->getNextMeaningfulToken($index);
72-
if ($tokens[$nextIndex]->isGivenKind(T_NS_SEPARATOR)) {
73-
continue;
74-
}
75-
76-
if (!$tokens[$prevIndex]->isGivenKind([T_NEW]) && $tokens[$nextIndex]->equals('(')) {
56+
if (!$this->isGlobalClassUsage($tokens, $index, $isInGlobalNamespace)) {
7757
continue;
7858
}
7959

@@ -87,6 +67,31 @@ private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool
8767
}
8868
}
8969

70+
private function isGlobalClassUsage(Tokens $tokens, int $index, bool $isInGlobalNamespace): bool
71+
{
72+
/** @var int $prevIndex */
73+
$prevIndex = $tokens->getPrevMeaningfulToken($index);
74+
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
75+
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
76+
if ($tokens[$prevIndex]->isGivenKind(T_STRING)) {
77+
return false;
78+
}
79+
} elseif (!$isInGlobalNamespace) {
80+
return false;
81+
}
82+
83+
if ($tokens[$prevIndex]->isGivenKind([T_AS, T_CLASS, T_CONST, T_DOUBLE_COLON, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
84+
return false;
85+
}
86+
87+
$nextIndex = $tokens->getNextMeaningfulToken($index);
88+
if ($tokens[$nextIndex]->isGivenKind(T_NS_SEPARATOR)) {
89+
return false;
90+
}
91+
92+
return $tokens[$prevIndex]->isGivenKind([T_NEW]) || !$tokens[$nextIndex]->equals('(');
93+
}
94+
9095
private function getCorrectCase(string $className): string
9196
{
9297
static $classes;

src/Fixer/NoUnneededConcatenationFixer.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ public function isRisky(): bool
3131
return false;
3232
}
3333

34-
public function fix(\SplFileInfo $file, Tokens $tokens): void
34+
public function getPriority(): int
3535
{
36-
for ($index = 0; $index < $tokens->count(); $index++) {
37-
if ($tokens[$index]->getContent() !== '.') {
38-
continue;
39-
}
40-
41-
if ($tokens[$index - 1]->isGivenKind(T_WHITESPACE) && Preg::match('/\R/', $tokens[$index - 1]->getContent()) === 1) {
42-
continue;
43-
}
36+
return -1;
37+
}
4438

45-
if ($tokens[$index + 1]->isGivenKind(T_WHITESPACE) && Preg::match('/\R/', $tokens[$index + 1]->getContent()) === 1) {
39+
public function fix(\SplFileInfo $file, Tokens $tokens): void
40+
{
41+
for ($index = $tokens->count() - 1; $index > 0; $index--) {
42+
if (!$tokens[$index]->equals('.')) {
4643
continue;
4744
}
4845

@@ -51,26 +48,47 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5148
if (!$tokens[$prevIndex]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
5249
continue;
5350
}
54-
$prevStringBorder = $tokens[$prevIndex]->getContent()[0];
51+
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $prevIndex, $index)) {
52+
continue;
53+
}
5554

55+
/** @var int $nextIndex */
5656
$nextIndex = $tokens->getNextMeaningfulToken($index);
5757
if (!$tokens[$nextIndex]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
5858
continue;
5959
}
60-
61-
if ($prevStringBorder !== $tokens[$nextIndex]->getContent()[0]) {
60+
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $index, $nextIndex)) {
6261
continue;
6362
}
6463

65-
$tokens[$prevIndex] = new Token([T_CONSTANT_ENCAPSED_STRING, \substr($tokens[$prevIndex]->getContent(), 0, -1) . \substr($tokens[$nextIndex]->getContent(), 1)]);
66-
for ($i = $prevIndex + 1; $i <= $nextIndex; $i++) {
67-
$tokens->clearAt($i);
64+
$this->fixConcat($tokens, $prevIndex, $nextIndex);
65+
}
66+
}
67+
68+
private function areOnlyHorizontalWhitespacesBetween(Tokens $tokens, int $indexStart, int $indexEnd): bool
69+
{
70+
for ($index = $indexStart + 1; $index < $indexEnd; $index++) {
71+
if (!$tokens[$index]->isGivenKind(T_WHITESPACE)) {
72+
return false;
73+
}
74+
if (Preg::match('/\R/', $tokens[$index]->getContent()) === 1) {
75+
return false;
6876
}
6977
}
78+
79+
return true;
7080
}
7181

72-
public function getPriority(): int
82+
private function fixConcat(Tokens $tokens, int $prevIndex, int $nextIndex): void
7383
{
74-
return -1;
84+
if ($tokens[$prevIndex]->getContent()[0] !== $tokens[$nextIndex]->getContent()[0]) {
85+
return;
86+
}
87+
88+
$tokens->overrideRange(
89+
$prevIndex,
90+
$nextIndex,
91+
[new Token([T_CONSTANT_ENCAPSED_STRING, \substr($tokens[$prevIndex]->getContent(), 0, -1) . \substr($tokens[$nextIndex]->getContent(), 1)])]
92+
);
7593
}
7694
}

tests/Fixer/NoUnneededConcatenationFixerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public function provideFixCases(): iterable
5151
yield ['<?php "foo" // comment
5252
. "bar";'];
5353

54+
yield ['<?php "foo"/* comment
55+
*/. "bar";'];
56+
57+
yield [
58+
'<?php "foo"/* comment */. "bar";',
59+
];
60+
5461
yield [
5562
'<?php "foobar";',
5663
'<?php "foo" . "bar";',
@@ -88,5 +95,22 @@ public function provideFixCases(): iterable
8895
$f = "f" . "f";
8996
',
9097
];
98+
99+
yield [
100+
'<?php
101+
"ab";
102+
$c . "d";
103+
"f"/* f */ . "g";
104+
"h" . $i;
105+
"j"./** k */"l";
106+
',
107+
'<?php
108+
"a" . "b";
109+
$c . "d";
110+
"f"/* f */ . "g";
111+
"h" . $i;
112+
"j"./** k */"l";
113+
',
114+
];
91115
}
92116
}

0 commit comments

Comments
 (0)