PhpCsFixer\Fixer\CastNotation\ModernizeTypesCastingFixer::fix PHP Method

fix() public method

public fix ( SplFileInfo $file, Tokens $tokens )
$file SplFileInfo
$tokens PhpCsFixer\Tokenizer\Tokens
    public function fix(\SplFileInfo $file, Tokens $tokens)
    {
        // replacement patterns
        static $replacement = array('intval' => array(T_INT_CAST, '(int)'), 'floatval' => array(T_DOUBLE_CAST, '(float)'), 'doubleval' => array(T_DOUBLE_CAST, '(float)'), 'strval' => array(T_STRING_CAST, '(string)'), 'boolval' => array(T_BOOL_CAST, '(bool)'));
        foreach ($replacement as $functionIdentity => $newToken) {
            $currIndex = 0;
            while (null !== $currIndex) {
                // try getting function reference and translate boundaries for humans
                $boundaries = $this->find($functionIdentity, $tokens, $currIndex, $tokens->count() - 1);
                if (null === $boundaries) {
                    // next function search, as current one not found
                    continue 2;
                }
                list($functionName, $openParenthesis, $closeParenthesis) = $boundaries;
                // analysing cursor shift
                $currIndex = $openParenthesis;
                // indicator that the function is overriden
                if (1 !== $this->countArguments($tokens, $openParenthesis, $closeParenthesis)) {
                    continue;
                }
                // check if something complex passed as an argument and preserve parenthesises then
                $countParamTokens = 0;
                for ($paramContentIndex = $openParenthesis + 1; $paramContentIndex < $closeParenthesis; ++$paramContentIndex) {
                    //not a space, means some sensible token
                    if (!$tokens[$paramContentIndex]->isGivenKind(T_WHITESPACE)) {
                        ++$countParamTokens;
                    }
                }
                $preserveParenthesises = $countParamTokens > 1;
                // analyse namespace specification (root one or none) and decide what to do
                $prevTokenIndex = $tokens->getPrevMeaningfulToken($functionName);
                if ($tokens[$prevTokenIndex]->isGivenKind(T_NS_SEPARATOR)) {
                    // get rid of root namespace when it used
                    $tokens->removeTrailingWhitespace($prevTokenIndex);
                    $tokens[$prevTokenIndex]->clear();
                }
                // perform transformation
                $replacementSequence = array(new Token($newToken), new Token(array(T_WHITESPACE, ' ')));
                if (!$preserveParenthesises) {
                    // closing parenthesis removed with leading spaces
                    $tokens->removeLeadingWhitespace($closeParenthesis);
                    $tokens[$closeParenthesis]->clear();
                    // opening parenthesis removed with trailing spaces
                    $tokens->removeLeadingWhitespace($openParenthesis);
                    $tokens->removeTrailingWhitespace($openParenthesis);
                    $tokens[$openParenthesis]->clear();
                } else {
                    // we'll need to provide a space after a casting operator
                    $tokens->removeTrailingWhitespace($functionName);
                }
                $tokens->overrideRange($functionName, $functionName, $replacementSequence);
                // nested transformations support
                $currIndex = $functionName;
            }
        }
    }
ModernizeTypesCastingFixer