123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?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\PSR2;
- use Symfony\CS\AbstractFixer;
- use Symfony\CS\Tokenizer\Tokens;
- /**
- * Fixer for rules defined in PSR2 ¶4.6.
- *
- * @author Varga Bence <vbence@czentral.org>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
- class FunctionCallSpaceFixer extends AbstractFixer
- {
- /**
- * {@inheritdoc}
- */
- public function fix(\SplFileInfo $file, $content)
- {
- $tokens = Tokens::fromCode($content);
- $functionyTokens = $this->getFunctionyTokenKinds();
- $languageConstructionTokens = $this->getLanguageConstructionTokenKinds();
- foreach ($tokens as $index => $token) {
- // looking for start brace
- if (!$token->equals('(')) {
- continue;
- }
- // last non-whitespace token
- $lastTokenIndex = $tokens->getPrevNonWhitespace($index);
- if (null === $lastTokenIndex) {
- continue;
- }
- // check for ternary operator
- $endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
- $nextNonWhiteSpace = $tokens->getNextMeaningfulToken($endParenthesisIndex);
- if (
- !empty($nextNonWhiteSpace)
- && $tokens[$nextNonWhiteSpace]->equals('?')
- && $tokens[$lastTokenIndex]->isGivenKind($languageConstructionTokens)
- ) {
- continue;
- }
- // check if it is a function call
- if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) {
- $this->fixFunctionCall($tokens, $index);
- }
- }
- return $tokens->generateCode();
- }
- /**
- * {@inheritdoc}
- */
- public function getDescription()
- {
- return 'When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis.';
- }
- /**
- * Fixes whitespaces around braces of a function(y) call.
- *
- * @param Tokens $tokens tokens to handle
- * @param int $index index of token
- */
- private function fixFunctionCall(Tokens $tokens, $index)
- {
- // remove space before opening brace
- if ($tokens[$index - 1]->isWhitespace()) {
- $tokens[$index - 1]->clear();
- }
- }
- /**
- * Gets the token kinds which can work as function calls.
- *
- * @return int[] Token names.
- */
- private function getFunctionyTokenKinds()
- {
- static $tokens = null;
- if (null === $tokens) {
- $tokens = array(
- T_ARRAY,
- T_ECHO,
- T_EMPTY,
- T_EVAL,
- T_EXIT,
- T_INCLUDE,
- T_INCLUDE_ONCE,
- T_ISSET,
- T_LIST,
- T_PRINT,
- T_REQUIRE,
- T_REQUIRE_ONCE,
- T_STRING, // for real function calls
- T_UNSET,
- );
- }
- return $tokens;
- }
- /**
- * Gets the token kinds of actually language construction.
- *
- * @return int[]
- */
- private function getLanguageConstructionTokenKinds()
- {
- static $languageConstructionTokens = array(
- T_ECHO,
- T_PRINT,
- T_INCLUDE,
- T_INCLUDE_ONCE,
- T_REQUIRE,
- T_REQUIRE_ONCE,
- );
- return $languageConstructionTokens;
- }
- }
|