Skip to content
Permalink
Browse files

PHPMD - decrease cyclomatic complexity report level to 8 (#170)

  • Loading branch information...
kubawerlos committed Oct 24, 2019
1 parent 1753f57 commit 1063223165df1c49b7c64da98a80dc13f7ddc531
@@ -11,7 +11,7 @@

<rule ref='rulesets/codesize.xml/CyclomaticComplexity'>
<properties>
<property name='reportLevel' value='9' />
<property name='reportLevel' value='8' />
</properties>
</rule>
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />
@@ -41,6 +41,11 @@ public function testFoo() {
);
}
public function getPriority(): int
{
return -21;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
@@ -59,11 +64,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
}
}
public function getPriority(): int
{
return -21;
}
private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endIndex): void
{
for ($index = $startIndex; $index < $endIndex; $index++) {
@@ -85,9 +85,6 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
/** @var int $semicolonIndex */
$semicolonIndex = $tokens->getNextMeaningfulToken($closingBraceIndex);
if (!$tokens[$semicolonIndex]->equals(';')) {
continue;
}
/** @var int $returnIndex */
$returnIndex = $tokens->getNextMeaningfulToken($semicolonIndex);
@@ -26,6 +26,12 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
return 6;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
@@ -39,11 +45,7 @@ public function isRisky(): bool
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(T_DOC_COMMENT)) {
continue;
}
if (\stripos($token->getContent(), '@var') === false) {
if (!$this->isTokenCandidate($token)) {
continue;
}
@@ -76,10 +78,9 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
}
}
public function getPriority(): int
private function isTokenCandidate(Token $token): bool
{
// must be run before NoEmptyCommentFixer, NoEmptyPhpdocFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer, NoUnusedImportsFixer and NoWhitespaceInBlankLineFixer
return 6;
return $token->isGivenKind(T_DOC_COMMENT) && \stripos($token->getContent(), '@var') !== false;
}
private function removeVarAnnotation(Tokens $tokens, int $index, array $allowedVariables): void
@@ -132,13 +133,18 @@ private function removeVarAnnotationNotMatchingPattern(Tokens $tokens, int $inde
return;
}
$tokens[$index] = new Token([T_DOC_COMMENT, $this->ensureDocCommentContent($content)]);
}
private function ensureDocCommentContent(string $content): string
{
if (\strpos($content, '/**') !== 0) {
$content = '/** ' . $content;
}
if (\strpos($content, '*/') === false) {
$content .= \str_replace(\ltrim($content), '', $content) . '*/';
}
$tokens[$index] = new Token([T_DOC_COMMENT, $content]);
return $content;
}
}
@@ -31,6 +31,11 @@ class Foo {
);
}
public function getPriority(): int
{
return 0;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_CLASS, T_INTERFACE]) && $tokens->isTokenKindFound(T_DOC_COMMENT);
@@ -42,35 +47,40 @@ public function isRisky(): bool
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
$namespaces = (new NamespacesAnalyzer())->getDeclarations($tokens);
foreach ($namespaces as $namespace) {
$this->fixPhpdocSelfAccessor($tokens, $namespace->getScopeStartIndex(), $namespace->getScopeEndIndex(), $namespace->getFullName());
}
}
private function fixPhpdocSelfAccessor(Tokens $tokens, int $namespaceStartIndex, int $namespaceEndIndex, string $fullName): void
{
$tokensAnalyzer = new TokensAnalyzer($tokens);
foreach ((new NamespacesAnalyzer())->getDeclarations($tokens) as $namespace) {
for ($index = $namespace->getScopeStartIndex(); $index < $namespace->getScopeEndIndex(); $index++) {
if (!$tokens[$index]->isGivenKind([T_CLASS, T_INTERFACE]) || $tokensAnalyzer->isAnonymousClass($index)) {
continue;
}
$index = $namespaceStartIndex;
while ($index < $namespaceEndIndex) {
$index++;
/** @var int $nameIndex */
$nameIndex = $tokens->getNextTokenOfKind($index, [[T_STRING]]);
if (!$tokens[$index]->isGivenKind([T_CLASS, T_INTERFACE]) || $tokensAnalyzer->isAnonymousClass($index)) {
continue;
}
/** @var int $startIndex */
$startIndex = $tokens->getNextTokenOfKind($nameIndex, ['{']);
/** @var int $nameIndex */
$nameIndex = $tokens->getNextTokenOfKind($index, [[T_STRING]]);
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
/** @var int $startIndex */
$startIndex = $tokens->getNextTokenOfKind($nameIndex, ['{']);
$name = $tokens[$nameIndex]->getContent();
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
$this->replaceNameOccurrences($tokens, $namespace->getFullName(), $name, $startIndex, $endIndex);
$name = $tokens[$nameIndex]->getContent();
$index = $endIndex;
}
}
}
$this->replaceNameOccurrences($tokens, $fullName, $name, $startIndex, $endIndex);
public function getPriority(): int
{
return 0;
$index = $endIndex;
}
}
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
continue;
}
$docBlock = new DocBlock($tokens[$index]->getContent());
$newContent = $this->getNewContent($tokens[$index]->getContent(), $namespace, $name);
if ($newContent === $tokens[$index]->getContent()) {
continue;
}
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
}
}
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $name;
private function getNewContent(string $content, string $namespace, string $name): string
{
$docBlock = new DocBlock($content);
foreach ($docBlock->getAnnotations() as $annotation) {
if (!$annotation->supportTypes()) {
continue;
}
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $name;
$types = [];
foreach ($annotation->getTypes() as $type) {
/** @var string $type */
$type = Preg::replace(
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|\Q%s\E)\b(?!\\\\)/', $name, $fqcn),
'self',
$type
);
foreach ($docBlock->getAnnotations() as $annotation) {
if (!$annotation->supportTypes()) {
continue;
}
$types[] = $type;
}
$types = [];
foreach ($annotation->getTypes() as $type) {
/** @var string $type */
$type = Preg::replace(
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|\Q%s\E)\b(?!\\\\)/', $name, $fqcn),
'self',
$type
);
if ($types === $annotation->getTypes()) {
continue;
}
$annotation->setTypes($types);
$tokens[$index] = new Token([T_DOC_COMMENT, $docBlock->getContent()]);
$types[] = $type;
}
$annotation->setTypes($types);
}
return $docBlock->getContent();
}
}
@@ -182,6 +182,10 @@ public function bar(...$params) {}
'<?php
namespace Some\Thing;
interface Foo {
/**
* @return Bar
*/
public function init();
/**
* @return self
*/
@@ -190,6 +194,10 @@ public function getInstance();
'<?php
namespace Some\Thing;
interface Foo {
/**
* @return Bar
*/
public function init();
/**
* @return Foo
*/

0 comments on commit 1063223

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