123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /*
- * This file is part of the PHP CS utility.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace Symfony\CS\Fixer\All;
- use Symfony\CS\FixerInterface;
- use Symfony\CS\Token;
- use Symfony\CS\Tokens;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
- class ReturnFixer implements FixerInterface
- {
- /**
- * {@inheritdoc}
- */
- public function fix(\SplFileInfo $file, $content)
- {
- $tokens = Tokens::fromCode($content);
- for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
- $token = $tokens[$index];
- if (!$token->isGivenKind(T_RETURN)) {
- continue;
- }
- $prevNonWhitespaceToken = $tokens->getPrevNonWhitespace($index);
- if (!in_array($prevNonWhitespaceToken->content, array(';', '}'), true)) {
- continue;
- }
- $prevToken = $tokens[$index - 1];
- if ($prevToken->isWhitespace()) {
- $parts = explode("\n", $prevToken->content);
- $countParts = count($parts);
- if (1 === $countParts) {
- $prevToken->content = rtrim($prevToken->content, " \t")."\n\n";
- } elseif (count($parts) <= 2) {
- $prevToken->content = "\n".$prevToken->content;
- }
- } else {
- $tokens->insertAt($index, new Token(array(T_WHITESPACE, "\n\n")));
- ++$index;
- ++$limit;
- }
- }
- return $tokens->generateCode();
- }
- /**
- * {@inheritdoc}
- */
- public function getLevel()
- {
- return FixerInterface::ALL_LEVEL;
- }
- /**
- * {@inheritdoc}
- */
- public function getPriority()
- {
- return 0;
- }
- /**
- * {@inheritdoc}
- */
- public function supports(\SplFileInfo $file)
- {
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function getName()
- {
- return 'return';
- }
- /**
- * {@inheritdoc}
- */
- public function getDescription()
- {
- return 'An empty line feed should precede a return statement.';
- }
- }
|