Skip to content
Permalink
Browse files

PHPMD - decrease cyclomatic complexity report level to 11 (#164)

  • Loading branch information...
kubawerlos committed Oct 22, 2019
1 parent ff7bc5f commit aff40bd1e6aff875eb0f4e21202ebaa5e711cd19
@@ -8,7 +8,7 @@

[![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)
[![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)
![Tests](https://img.shields.io/badge/tests-1348-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1351-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![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)

@@ -11,7 +11,7 @@

<rule ref='rulesets/codesize.xml/CyclomaticComplexity'>
<properties>
<property name='reportLevel' value='12' />
<property name='reportLevel' value='11' />
</properties>
</rule>
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />
@@ -53,27 +53,7 @@ private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool
continue;
}
/** @var int $prevIndex */
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
if ($tokens[$prevIndex]->isGivenKind(T_STRING)) {
continue;
}
} elseif (!$isInGlobalNamespace) {
continue;
}
if ($tokens[$prevIndex]->isGivenKind([T_AS, T_CLASS, T_CONST, T_DOUBLE_COLON, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
continue;
}
$nextIndex = $tokens->getNextMeaningfulToken($index);
if ($tokens[$nextIndex]->isGivenKind(T_NS_SEPARATOR)) {
continue;
}
if (!$tokens[$prevIndex]->isGivenKind([T_NEW]) && $tokens[$nextIndex]->equals('(')) {
if (!$this->isGlobalClassUsage($tokens, $index, $isInGlobalNamespace)) {
continue;
}
@@ -87,6 +67,31 @@ private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool
}
}
private function isGlobalClassUsage(Tokens $tokens, int $index, bool $isInGlobalNamespace): bool
{
/** @var int $prevIndex */
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
if ($tokens[$prevIndex]->isGivenKind(T_STRING)) {
return false;
}
} elseif (!$isInGlobalNamespace) {
return false;
}
if ($tokens[$prevIndex]->isGivenKind([T_AS, T_CLASS, T_CONST, T_DOUBLE_COLON, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
return false;
}
$nextIndex = $tokens->getNextMeaningfulToken($index);
if ($tokens[$nextIndex]->isGivenKind(T_NS_SEPARATOR)) {
return false;
}
return $tokens[$prevIndex]->isGivenKind([T_NEW]) || !$tokens[$nextIndex]->equals('(');
}
private function getCorrectCase(string $className): string
{
static $classes;
@@ -31,18 +31,15 @@ public function isRisky(): bool
return false;
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
public function getPriority(): int
{
for ($index = 0; $index < $tokens->count(); $index++) {
if ($tokens[$index]->getContent() !== '.') {
continue;
}
if ($tokens[$index - 1]->isGivenKind(T_WHITESPACE) && Preg::match('/\R/', $tokens[$index - 1]->getContent()) === 1) {
continue;
}
return -1;
}
if ($tokens[$index + 1]->isGivenKind(T_WHITESPACE) && Preg::match('/\R/', $tokens[$index + 1]->getContent()) === 1) {
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = $tokens->count() - 1; $index > 0; $index--) {
if (!$tokens[$index]->equals('.')) {
continue;
}
@@ -51,26 +48,47 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
if (!$tokens[$prevIndex]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
continue;
}
$prevStringBorder = $tokens[$prevIndex]->getContent()[0];
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $prevIndex, $index)) {
continue;
}
/** @var int $nextIndex */
$nextIndex = $tokens->getNextMeaningfulToken($index);
if (!$tokens[$nextIndex]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
continue;
}
if ($prevStringBorder !== $tokens[$nextIndex]->getContent()[0]) {
if (!$this->areOnlyHorizontalWhitespacesBetween($tokens, $index, $nextIndex)) {
continue;
}
$tokens[$prevIndex] = new Token([T_CONSTANT_ENCAPSED_STRING, \substr($tokens[$prevIndex]->getContent(), 0, -1) . \substr($tokens[$nextIndex]->getContent(), 1)]);
for ($i = $prevIndex + 1; $i <= $nextIndex; $i++) {
$tokens->clearAt($i);
$this->fixConcat($tokens, $prevIndex, $nextIndex);
}
}
private function areOnlyHorizontalWhitespacesBetween(Tokens $tokens, int $indexStart, int $indexEnd): bool
{
for ($index = $indexStart + 1; $index < $indexEnd; $index++) {
if (!$tokens[$index]->isGivenKind(T_WHITESPACE)) {
return false;
}
if (Preg::match('/\R/', $tokens[$index]->getContent()) === 1) {
return false;
}
}
return true;
}
public function getPriority(): int
private function fixConcat(Tokens $tokens, int $prevIndex, int $nextIndex): void
{
return -1;
if ($tokens[$prevIndex]->getContent()[0] !== $tokens[$nextIndex]->getContent()[0]) {
return;
}
$tokens->overrideRange(
$prevIndex,
$nextIndex,
[new Token([T_CONSTANT_ENCAPSED_STRING, \substr($tokens[$prevIndex]->getContent(), 0, -1) . \substr($tokens[$nextIndex]->getContent(), 1)])]
);
}
}
@@ -51,6 +51,13 @@ public function provideFixCases(): iterable
yield ['<?php "foo" // comment
. "bar";'];
yield ['<?php "foo"/* comment
*/. "bar";'];
yield [
'<?php "foo"/* comment */. "bar";',
];
yield [
'<?php "foobar";',
'<?php "foo" . "bar";',
@@ -88,5 +95,22 @@ public function provideFixCases(): iterable
$f = "f" . "f";
',
];
yield [
'<?php
"ab";
$c . "d";
"f"/* f */ . "g";
"h" . $i;
"j"./** k */"l";
',
'<?php
"a" . "b";
$c . "d";
"f"/* f */ . "g";
"h" . $i;
"j"./** k */"l";
',
];
}
}

0 comments on commit aff40bd

Please sign in to comment.
You can’t perform that action at this time.