* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Fixer\Comment; use PhpCsFixer\AbstractFixer; use PhpCsFixer\FixerDefinition\CodeSample; use PhpCsFixer\FixerDefinition\FixerDefinition; use PhpCsFixer\Preg; use PhpCsFixer\Tokenizer\Token; use PhpCsFixer\Tokenizer\Tokens; /** * @author Filippo Tessarotto */ final class MultilineCommentOpeningClosingFixer extends AbstractFixer { /** * {@inheritdoc} */ public function getDefinition() { return new FixerDefinition( 'DocBlocks must start with two asterisks, multiline comments must start with a single asterisk, after the opening slash. Both must end with a single asterisk before the closing slash.', [ new CodeSample( <<<'EOT' isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]); } /** * {@inheritdoc} */ protected function applyFix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens as $index => $token) { $originalContent = $token->getContent(); if ( !$token->isGivenKind(T_DOC_COMMENT) && !($token->isGivenKind(T_COMMENT) && 0 === strpos($originalContent, '/*')) ) { continue; } $newContent = $originalContent; // Fix opening if ($token->isGivenKind(T_COMMENT)) { $newContent = Preg::replace('/^\\/\\*\\*+/', '/*', $newContent); } // Fix closing $newContent = Preg::replace('/\\*+\\*\\/$/', '*/', $newContent); if ($newContent !== $originalContent) { $tokens[$index] = new Token([$token->getId(), $newContent]); } } } }