Skip to content
Permalink
Browse files

Merge pull request #161 from kubawerlos/phpmd-cc-12

PHPMD - decrease cyclomatic complexity to 12
  • Loading branch information...
kubawerlos committed Oct 21, 2019
2 parents f62f798 + adec814 commit ef969ad479cf145e53987082c299a95ba997315b
Showing with 54 additions and 34 deletions.
  1. +1 −1 phpmd.xml
  2. +53 −33 src/Fixer/NoImportFromGlobalNamespaceFixer.php
@@ -11,7 +11,7 @@

<rule ref='rulesets/codesize.xml/CyclomaticComplexity'>
<properties>
<property name='reportLevel' value='13' />
<property name='reportLevel' value='12' />
</properties>
</rule>
<rule ref='rulesets/codesize.xml/ExcessiveMethodLength' />
@@ -60,52 +60,72 @@ private function fixImports(Tokens $tokens, int $startIndex, int $endIndex, bool
$token = $tokens[$index];
if ($token->isGivenKind(T_USE)) {
/** @var int $classNameIndex */
$classNameIndex = $tokens->getNextMeaningfulToken($index);
if ($tokens[$classNameIndex]->isGivenKind(T_NS_SEPARATOR)) {
/** @var int $classNameIndex */
$classNameIndex = $tokens->getNextMeaningfulToken($classNameIndex);
}
/** @var int $semicolonIndex */
$semicolonIndex = $tokens->getNextMeaningfulToken($classNameIndex);
if ($tokens[$semicolonIndex]->getContent() === ';') {
$imports[] = $tokens[$classNameIndex]->getContent();
$tokens->clearRange($index, $semicolonIndex);
TokenRemover::removeWithLinesIfPossible($tokens, $semicolonIndex);
$index = $semicolonIndex + 1;
}
$imports = $this->removeImportFromGlobalNamespace($tokens, $imports, $index);
continue;
}
if ($token->isGivenKind(T_DOC_COMMENT)) {
$content = $token->getContent();
foreach ($imports as $import) {
$content = Preg::replace(\sprintf('/\b(?<!\\\\)%s\b/', $import), '\\' . $import, $content);
}
if ($content !== $token->getContent()) {
$tokens[$index] = new Token([T_DOC_COMMENT, $content]);
}
if ($isInGlobalNamespace) {
continue;
}
if (!$token->isGivenKind(T_STRING)) {
if ($token->isGivenKind(T_DOC_COMMENT)) {
$this->updateComment($tokens, $imports, $index);
continue;
}
if (!\in_array($token->getContent(), $imports, true)) {
if (!$token->isGivenKind(T_STRING)) {
continue;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind([T_CONST, T_DOUBLE_COLON, T_NS_SEPARATOR, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
continue;
}
$this->updateUsage($tokens, $imports, $index);
}
}
if (!$isInGlobalNamespace) {
$tokens->insertAt($index, new Token([T_NS_SEPARATOR, '\\']));
$index++;
}
private function removeImportFromGlobalNamespace(Tokens $tokens, array $imports, int $index): array
{
/** @var int $classNameIndex */
$classNameIndex = $tokens->getNextMeaningfulToken($index);
if ($tokens[$classNameIndex]->isGivenKind(T_NS_SEPARATOR)) {
/** @var int $classNameIndex */
$classNameIndex = $tokens->getNextMeaningfulToken($classNameIndex);
}
/** @var int $semicolonIndex */
$semicolonIndex = $tokens->getNextMeaningfulToken($classNameIndex);
if ($tokens[$semicolonIndex]->getContent() === ';') {
$imports[] = $tokens[$classNameIndex]->getContent();
$tokens->clearRange($index, $semicolonIndex);
TokenRemover::removeWithLinesIfPossible($tokens, $semicolonIndex);
}
return $imports;
}
private function updateComment(Tokens $tokens, array $imports, int $index): void
{
$content = $tokens[$index]->getContent();
foreach ($imports as $import) {
$content = Preg::replace(\sprintf('/\b(?<!\\\\)%s\b/', $import), '\\' . $import, $content);
}
if ($content !== $tokens[$index]->getContent()) {
$tokens[$index] = new Token([T_DOC_COMMENT, $content]);
}
}
private function updateUsage(Tokens $tokens, array $imports, int $index): void
{
if (!\in_array($tokens[$index]->getContent(), $imports, true)) {
return;
}
$prevIndex = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevIndex]->isGivenKind([T_CONST, T_DOUBLE_COLON, T_NS_SEPARATOR, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
return;
}
$tokens->insertAt($index, new Token([T_NS_SEPARATOR, '\\']));
}
}

0 comments on commit ef969ad

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