Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX: PhpdocAlignFixer - refactor to use DocBlock #4205

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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