Hoa central
Public Member Functions | List of all members
Hoa\Devtools\Resource\PHPCSFixer\Fixer\ControlFlowStatement Class Reference
Inheritance diagram for Hoa\Devtools\Resource\PHPCSFixer\Fixer\ControlFlowStatement:

Public Member Functions

 fix (SplFileInfo $file, $content)
 
 getDescription ()
 
 getName ()
 
 getLevel ()
 

Detailed Description

Class .

Add a newline before return, break and continue if needed. Inspired by the ReturnFixer class, provided with php-cs-fixer.

Definition at line 53 of file ControlFlowStatement.php.

Member Function Documentation

Hoa\Devtools\Resource\PHPCSFixer\Fixer\ControlFlowStatement::fix ( SplFileInfo  $file,
  $content 
)

Definition at line 55 of file ControlFlowStatement.php.

56  {
57  $tokens = Tokens::fromCode($content);
58 
59  for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
60  $token = $tokens[$index];
61 
62  if (!$token->isGivenKind([T_RETURN, T_BREAK, T_CONTINUE])) {
63  continue;
64  }
65 
66  $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
67 
68  if (!$prevNonWhitespaceToken->equalsAny([';', '}'])) {
69  continue;
70  }
71 
72  $prevToken = $tokens[$index - 1];
73 
74  if ($prevToken->isWhitespace()) {
75  $parts = explode("\n", $prevToken->getContent());
76  $countParts = count($parts);
77 
78  if (1 === $countParts) {
79  $prevToken->setContent(rtrim($prevToken->getContent(), " \t") . "\n\n");
80  } elseif (count($parts) <= 2) {
81  $prevToken->setContent("\n" . $prevToken->getContent());
82  }
83  } else {
84  $tokens->insertAt($index, new Token([T_WHITESPACE, "\n\n"]));
85 
86  ++$index;
87  ++$limit;
88  }
89  }
90 
91  return $tokens->generateCode();
92  }
$content
Definition: Hoa.php:119
Hoa\Devtools\Resource\PHPCSFixer\Fixer\ControlFlowStatement::getDescription ( )

Definition at line 94 of file ControlFlowStatement.php.

95  {
96  return 'Add a newline before `return`, `break` and `continue` if needed.';
97  }
Hoa\Devtools\Resource\PHPCSFixer\Fixer\ControlFlowStatement::getLevel ( )

Definition at line 104 of file ControlFlowStatement.php.

105  {
106  return FixerInterface::CONTRIB_LEVEL;
107  }
Hoa\Devtools\Resource\PHPCSFixer\Fixer\ControlFlowStatement::getName ( )

Definition at line 99 of file ControlFlowStatement.php.

100  {
101  return 'control_flow_statement';
102  }

The documentation for this class was generated from the following file: