Skip to content

Commit 1063223

Browse files
authored
PHPMD - decrease cyclomatic complexity report level to 8 (#170)
1 parent 1753f57 commit 1063223

File tree

5 files changed

+87
-58
lines changed

5 files changed

+87
-58
lines changed

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='9' />
14+
<property name='reportLevel' value='8' />
1515
</properties>
1616
</rule>
1717
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />

src/Fixer/PhpUnitNoUselessReturnFixer.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function testFoo() {
4141
);
4242
}
4343

44+
public function getPriority(): int
45+
{
46+
return -21;
47+
}
48+
4449
public function isCandidate(Tokens $tokens): bool
4550
{
4651
return $tokens->isTokenKindFound(T_STRING);
@@ -59,11 +64,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5964
}
6065
}
6166

62-
public function getPriority(): int
63-
{
64-
return -21;
65-
}
66-
6767
private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endIndex): void
6868
{
6969
for ($index = $startIndex; $index < $endIndex; $index++) {
@@ -85,9 +85,6 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
8585

8686
/** @var int $semicolonIndex */
8787
$semicolonIndex = $tokens->getNextMeaningfulToken($closingBraceIndex);
88-
if (!$tokens[$semicolonIndex]->equals(';')) {
89-
continue;
90-
}
9188

9289
/** @var int $returnIndex */
9390
$returnIndex = $tokens->getNextMeaningfulToken($semicolonIndex);

src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public function getDefinition(): FixerDefinitionInterface
2626
);
2727
}
2828

29+
public function getPriority(): int
30+
{
31+
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
32+
return 6;
33+
}
34+
2935
public function isCandidate(Tokens $tokens): bool
3036
{
3137
return $tokens->isTokenKindFound(T_DOC_COMMENT);
@@ -39,11 +45,7 @@ public function isRisky(): bool
3945
public function fix(\SplFileInfo $file, Tokens $tokens): void
4046
{
4147
foreach ($tokens as $index => $token) {
42-
if (!$token->isGivenKind(T_DOC_COMMENT)) {
43-
continue;
44-
}
45-
46-
if (\stripos($token->getContent(), '@var') === false) {
48+
if (!$this->isTokenCandidate($token)) {
4749
continue;
4850
}
4951

@@ -76,10 +78,9 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7678
}
7779
}
7880

79-
public function getPriority(): int
81+
private function isTokenCandidate(Token $token): bool
8082
{
81-
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
82-
return 6;
83+
return $token->isGivenKind(T_DOC_COMMENT) && \stripos($token->getContent(), '@var') !== false;
8384
}
8485

8586
private function removeVarAnnotation(Tokens $tokens, int $index, array $allowedVariables): void
@@ -132,13 +133,18 @@ private function removeVarAnnotationNotMatchingPattern(Tokens $tokens, int $inde
132133

133134
return;
134135
}
136+
$tokens[$index] = new Token([T_DOC_COMMENT, $this->ensureDocCommentContent($content)]);
137+
}
135138

139+
private function ensureDocCommentContent(string $content): string
140+
{
136141
if (\strpos($content, '/**') !== 0) {
137142
$content = '/** ' . $content;
138143
}
139144
if (\strpos($content, '*/') === false) {
140145
$content .= \str_replace(\ltrim($content), '', $content) . '*/';
141146
}
142-
$tokens[$index] = new Token([T_DOC_COMMENT, $content]);
147+
148+
return $content;
143149
}
144150
}

src/Fixer/PhpdocSelfAccessorFixer.php

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class Foo {
3131
);
3232
}
3333

34+
public function getPriority(): int
35+
{
36+
return 0;
37+
}
38+
3439
public function isCandidate(Tokens $tokens): bool
3540
{
3641
return $tokens->isAnyTokenKindsFound([T_CLASS, T_INTERFACE]) && $tokens->isTokenKindFound(T_DOC_COMMENT);
@@ -42,35 +47,40 @@ public function isRisky(): bool
4247
}
4348

4449
public function fix(\SplFileInfo $file, Tokens $tokens): void
50+
{
51+
$namespaces = (new NamespacesAnalyzer())->getDeclarations($tokens);
52+
53+
foreach ($namespaces as $namespace) {
54+
$this->fixPhpdocSelfAccessor($tokens, $namespace->getScopeStartIndex(), $namespace->getScopeEndIndex(), $namespace->getFullName());
55+
}
56+
}
57+
58+
private function fixPhpdocSelfAccessor(Tokens $tokens, int $namespaceStartIndex, int $namespaceEndIndex, string $fullName): void
4559
{
4660
$tokensAnalyzer = new TokensAnalyzer($tokens);
4761

48-
foreach ((new NamespacesAnalyzer())->getDeclarations($tokens) as $namespace) {
49-
for ($index = $namespace->getScopeStartIndex(); $index < $namespace->getScopeEndIndex(); $index++) {
50-
if (!$tokens[$index]->isGivenKind([T_CLASS, T_INTERFACE]) || $tokensAnalyzer->isAnonymousClass($index)) {
51-
continue;
52-
}
62+
$index = $namespaceStartIndex;
63+
while ($index < $namespaceEndIndex) {
64+
$index++;
5365

54-
/** @var int $nameIndex */
55-
$nameIndex = $tokens->getNextTokenOfKind($index, [[T_STRING]]);
66+
if (!$tokens[$index]->isGivenKind([T_CLASS, T_INTERFACE]) || $tokensAnalyzer->isAnonymousClass($index)) {
67+
continue;
68+
}
5669

57-
/** @var int $startIndex */
58-
$startIndex = $tokens->getNextTokenOfKind($nameIndex, ['{']);
70+
/** @var int $nameIndex */
71+
$nameIndex = $tokens->getNextTokenOfKind($index, [[T_STRING]]);
5972

60-
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
73+
/** @var int $startIndex */
74+
$startIndex = $tokens->getNextTokenOfKind($nameIndex, ['{']);
6175

62-
$name = $tokens[$nameIndex]->getContent();
76+
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
6377

64-
$this->replaceNameOccurrences($tokens, $namespace->getFullName(), $name, $startIndex, $endIndex);
78+
$name = $tokens[$nameIndex]->getContent();
6579

66-
$index = $endIndex;
67-
}
68-
}
69-
}
80+
$this->replaceNameOccurrences($tokens, $fullName, $name, $startIndex, $endIndex);
7081

71-
public function getPriority(): int
72-
{
73-
return 0;
82+
$index = $endIndex;
83+
}
7484
}
7585

7686
private function replaceNameOccurrences(Tokens $tokens, string $namespace, string $name, int $startIndex, int $endIndex): void
@@ -80,33 +90,41 @@ private function replaceNameOccurrences(Tokens $tokens, string $namespace, strin
8090
continue;
8191
}
8292

83-
$docBlock = new DocBlock($tokens[$index]->getContent());
93+
$newContent = $this->getNewContent($tokens[$index]->getContent(), $namespace, $name);
94+
95+
if ($newContent === $tokens[$index]->getContent()) {
96+
continue;
97+
}
98+
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
99+
}
100+
}
84101

85-
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $name;
102+
private function getNewContent(string $content, string $namespace, string $name): string
103+
{
104+
$docBlock = new DocBlock($content);
86105

87-
foreach ($docBlock->getAnnotations() as $annotation) {
88-
if (!$annotation->supportTypes()) {
89-
continue;
90-
}
106+
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $name;
91107

92-
$types = [];
93-
foreach ($annotation->getTypes() as $type) {
94-
/** @var string $type */
95-
$type = Preg::replace(
96-
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|\Q%s\E)\b(?!\\\\)/', $name, $fqcn),
97-
'self',
98-
$type
99-
);
108+
foreach ($docBlock->getAnnotations() as $annotation) {
109+
if (!$annotation->supportTypes()) {
110+
continue;
111+
}
100112

101-
$types[] = $type;
102-
}
113+
$types = [];
114+
foreach ($annotation->getTypes() as $type) {
115+
/** @var string $type */
116+
$type = Preg::replace(
117+
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|\Q%s\E)\b(?!\\\\)/', $name, $fqcn),
118+
'self',
119+
$type
120+
);
103121

104-
if ($types === $annotation->getTypes()) {
105-
continue;
106-
}
107-
$annotation->setTypes($types);
108-
$tokens[$index] = new Token([T_DOC_COMMENT, $docBlock->getContent()]);
122+
$types[] = $type;
109123
}
124+
125+
$annotation->setTypes($types);
110126
}
127+
128+
return $docBlock->getContent();
111129
}
112130
}

tests/Fixer/PhpdocSelfAccessorFixerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ public function bar(...$params) {}
182182
'<?php
183183
namespace Some\Thing;
184184
interface Foo {
185+
/**
186+
* @return Bar
187+
*/
188+
public function init();
185189
/**
186190
* @return self
187191
*/
@@ -190,6 +194,10 @@ public function getInstance();
190194
'<?php
191195
namespace Some\Thing;
192196
interface Foo {
197+
/**
198+
* @return Bar
199+
*/
200+
public function init();
193201
/**
194202
* @return Foo
195203
*/

0 commit comments

Comments
 (0)