12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace Symfony\CS\Fixer\All;
- use Symfony\CS\AbstractFixer;
- use Symfony\CS\Token;
- use Symfony\CS\Tokens;
- class MultilineArrayTrailingCommaFixer extends AbstractFixer
- {
-
- public function fix(\SplFileInfo $file, $content)
- {
- $tokens = Tokens::fromCode($content);
- for ($index = $tokens->count() - 1; $index >= 0; --$index) {
- if ($tokens->isArray($index)) {
- $this->fixArray($tokens, $index);
- }
- }
- return $tokens->generateCode();
- }
-
- public function getDescription()
- {
- return 'PHP multi-line arrays should have a trailing comma.';
- }
- private function fixArray(Tokens $tokens, $index)
- {
- $bracesLevel = 0;
-
-
- if ($tokens[$index]->isGivenKind(T_ARRAY)) {
- ++$index;
- }
- if (!$tokens->isArrayMultiLine($index)) {
- return ;
- }
- for ($c = $tokens->count(); $index < $c; ++$index) {
- $token = $tokens[$index];
- if ('(' === $token->content || '[' === $token->content) {
- ++$bracesLevel;
- continue;
- }
- if (')' === $token->content || ']' === $token->content) {
- --$bracesLevel;
- if (0 !== $bracesLevel) {
- continue;
- }
- $foundIndex = null;
- $prevToken = $tokens->getTokenNotOfKindSibling($index, -1, array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT)), $foundIndex);
- if (',' !== $prevToken->content) {
- $tokens->insertAt($foundIndex + 1, array(new Token(',')));
- }
- break;
- }
- }
- }
- }
|