Skip to content
Permalink
Browse files

PHPMD - decrease cyclomatic complexity report level to 10 (#166)

  • Loading branch information...
kubawerlos committed Oct 23, 2019
1 parent 14cd569 commit 7d1dcd86a150c7c1702938bdc09acb1b9ce3f0fb
@@ -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-1352-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1353-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='11' />
<property name='reportLevel' value='10' />
</properties>
</rule>
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />
@@ -21,6 +21,13 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
// Must be run after MultilineCommentOpeningClosingFixer and NoTrailingWhitespaceInCommentFixer
// Must be run before PhpdocTrimFixer
return -1;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
@@ -38,45 +45,57 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}
$content = $token->getContent();
if (Preg::match('/\R/', $content) !== 1) {
if (Preg::match('/\R/', $token->getContent()) !== 1) {
continue;
}
$toFixOpening = Preg::match('#^/\*+\R#', $content) !== 1;
$toFixClosing = Preg::match('#\R\h*\*+/$#', $content) !== 1;
$this->fixOpening($tokens, $index);
$this->fixClosing($tokens, $index);
}
}
if (!$toFixOpening && !$toFixClosing) {
continue;
}
private function fixOpening(Tokens $tokens, int $index): void
{
$token = $tokens[$index];
Preg::match('#\R(\h*)#', $content, $matches);
$indent = $matches[1];
if ($toFixOpening) {
Preg::match('#^(/\*+)(.*?)(\R)(.*)$#s', $content, $matches);
if ($matches === []) {
continue;
}
if ($matches[2][0] === '/') {
$matches[2] = ' ' . $matches[2];
}
$content = $matches[1] . $matches[3] . $indent . '*' . $matches[2] . $matches[3] . $matches[4];
}
if (Preg::match('#^/\*+\R#', $token->getContent()) === 1) {
return;
}
if ($toFixClosing) {
$content = Preg::replace('#(\R)(.+?)\h*(\*+/)$#', \sprintf('$1$2$1%s$3', $indent), $content);
}
Preg::match('#\R(\h*)#', $token->getContent(), $matches);
$indent = $matches[1];
$tokens[$index] = new Token([$token->getId(), $content]);
Preg::match('#^(/\*+)(.*?)(\R)(.*)$#s', $token->getContent(), $matches);
if ($matches === []) {
return;
}
if ($matches[2][0] === '/') {
$matches[2] = ' ' . $matches[2];
}
$newContent = $matches[1] . $matches[3] . $indent . '*' . $matches[2] . $matches[3] . $matches[4];
if ($newContent !== $token->getContent()) {
$tokens[$index] = new Token([$token->getId(), $newContent]);
}
}
public function getPriority(): int
private function fixClosing(Tokens $tokens, int $index): void
{
// Must be run after MultilineCommentOpeningClosingFixer and NoTrailingWhitespaceInCommentFixer
// Must be run before PhpdocTrimFixer
return -1;
$token = $tokens[$index];
if (Preg::match('#\R\h*\*+/$#', $token->getContent()) === 1) {
return;
}
Preg::match('#\R(\h*)#', $token->getContent(), $matches);
$indent = $matches[1];
$newContent = Preg::replace('#(\R)(.+?)\h*(\*+/)$#', \sprintf('$1$2$1%s$3', $indent), $token->getContent());
if ($newContent !== $token->getContent()) {
$tokens[$index] = new Token([$token->getId(), $newContent]);
}
}
}
@@ -45,6 +45,12 @@ public function configure(?array $configuration = null): void
$this->style = $configuration['style'] ?? $this->style;
}
public function getPriority(): int
{
// must be run before NoUnreachableDefaultArgumentValueFixer
return 1;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAllTokenKindsFound([T_FUNCTION]);
@@ -67,39 +73,45 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$paramBlockEndIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $paramBlockStartIndex);
for ($i = $paramBlockEndIndex; $i > $paramBlockStartIndex; $i--) {
if (!$tokens[$i]->equals([T_STRING, 'null'], false)) {
continue;
}
$this->fixFunction($tokens, $paramBlockStartIndex, $paramBlockEndIndex);
}
}
/** @var int $variableIndex */
$variableIndex = $tokens->getPrevTokenOfKind($i, [[T_VARIABLE]]);
private function fixFunction(Tokens $tokens, int $paramBlockStartIndex, int $paramBlockEndIndex): void
{
for ($i = $paramBlockEndIndex; $i > $paramBlockStartIndex; $i--) {
if (!$tokens[$i]->equals([T_STRING, 'null'], false)) {
continue;
}
/** @var int $typeIndex */
$typeIndex = $tokens->getPrevMeaningfulToken($variableIndex);
/** @var int $variableIndex */
$variableIndex = $tokens->getPrevTokenOfKind($i, [[T_VARIABLE]]);
if (!$tokens[$typeIndex]->isGivenKind([CT::T_ARRAY_TYPEHINT, T_CALLABLE, T_STRING])) {
continue;
}
/** @var int $typeIndex */
$typeIndex = $tokens->getPrevMeaningfulToken($variableIndex);
/** @var int $separatorIndex */
$separatorIndex = $tokens->getPrevTokenOfKind($typeIndex, ['(', ',']);
if (!$tokens[$typeIndex]->isGivenKind([CT::T_ARRAY_TYPEHINT, T_CALLABLE, T_STRING])) {
continue;
}
/** @var int $separatorIndex */
$separatorIndex = $tokens->getPrevTokenOfKind($typeIndex, ['(', ',']);
/** @var int $nullableIndex */
$nullableIndex = $tokens->getNextMeaningfulToken($separatorIndex);
/** @var int $nullableIndex */
$nullableIndex = $tokens->getNextMeaningfulToken($separatorIndex);
if ($this->style === 'with_question_mark' && !$tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
if ($this->style === 'with_question_mark') {
if (!$tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
$tokens->insertAt($nullableIndex, new Token([CT::T_NULLABLE_TYPE, '?']));
} elseif ($this->style === 'without_question_mark' && $tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
$tokens->clearAt($nullableIndex);
}
continue;
}
}
}
public function getPriority(): int
{
// must be run before NoUnreachableDefaultArgumentValueFixer
return 1;
if (!$tokens[$nullableIndex]->isGivenKind(CT::T_NULLABLE_TYPE)) {
continue;
}
$tokens->clearAt($nullableIndex);
}
}
}
@@ -82,6 +82,20 @@ public function provideFixCases(): iterable
['style' => 'without_question_mark'],
];
yield [
'<?php function foo(
int $x = null,
int $y = null,
string $z = NULL
) {}',
'<?php function foo(
?int $x = null,
int $y = null,
?string $z = NULL
) {}',
['style' => 'without_question_mark'],
];
yield [
'<?php function foo(int $x = null) {}',
null,

0 comments on commit 7d1dcd8

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