Skip to content

Commit

Permalink
DX: PhpdocAlignFixer - refactor to use DocBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and keradus committed Jan 4, 2019
1 parent 26f360b commit 9b98c63
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/Fixer/Phpdoc/PhpdocAlignFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace PhpCsFixer\Fixer\Phpdoc;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
Expand All @@ -23,7 +24,6 @@
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Utils;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand Down Expand Up @@ -175,7 +175,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
}

$content = $token->getContent();
$newContent = $this->fixDocBlock($content);
$docBlock = new DocBlock($content);
$this->fixDocBlock($docBlock);
$newContent = $docBlock->getContent();
if ($newContent !== $content) {
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
}
Expand Down Expand Up @@ -215,18 +217,15 @@ protected function createConfigurationDefinition()
}

/**
* @param string $content
*
* @return string
* @param DocBlock $docBlock
*/
private function fixDocBlock($content)
private function fixDocBlock(DocBlock $docBlock)
{
$lineEnding = $this->whitespacesConfig->getLineEnding();
$lines = Utils::splitLines($content);

for ($i = 0, $l = \count($lines); $i < $l; ++$i) {
for ($i = 0, $l = \count($docBlock->getLines()); $i < $l; ++$i) {
$items = [];
$matches = $this->getMatches($lines[$i]);
$matches = $this->getMatches($docBlock->getLine($i)->getContent());

if (null === $matches) {
continue;
Expand All @@ -236,11 +235,11 @@ private function fixDocBlock($content)
$items[] = $matches;

while (true) {
if (!isset($lines[++$i])) {
if (null === $docBlock->getLine(++$i)) {
break 2;
}

$matches = $this->getMatches($lines[$i], true);
$matches = $this->getMatches($docBlock->getLine($i)->getContent(), true);
if (null === $matches) {
break;
}
Expand Down Expand Up @@ -269,7 +268,7 @@ private function fixDocBlock($content)
foreach ($items as $j => $item) {
if (null === $item['tag']) {
if ('@' === $item['desc'][0]) {
$lines[$current + $j] = $item['indent'].' * '.$item['desc'].$lineEnding;
$docBlock->getLine($current + $j)->setContent($item['indent'].' * '.$item['desc'].$lineEnding);

continue;
}
Expand All @@ -290,7 +289,7 @@ private function fixDocBlock($content)
.$item['desc']
.$lineEnding;

$lines[$current + $j] = $line;
$docBlock->getLine($current + $j)->setContent($line);

continue;
}
Expand Down Expand Up @@ -324,11 +323,9 @@ private function fixDocBlock($content)
$line .= $lineEnding;
}

$lines[$current + $j] = $line;
$docBlock->getLine($current + $j)->setContent($line);
}
}

return implode('', $lines);
}

/**
Expand Down

0 comments on commit 9b98c63

Please sign in to comment.