src/Fixer/PhpTag/FullOpeningTagFixer.php
protected function applyFix(\SplFileInfo $file, Tokens $tokensOrg)
    {
        $content = $tokensOrg->generateCode();

        // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
        $newContent = Preg::replace('/<\?(?:phP|pHp|pHP|Php|PhP|PHp|PHP)?(\s|$)/', '<?php$1', $content, -1, $count);

        if (!$count) {
            return;
        }

        /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
         * > echo '<? ';
         * with
         * > echo '<?php ';
         */
        $tokens = Tokens::fromCode($newContent);

        $tokensOldContent = '';
        $tokensOldContentLength = 0;

        foreach ($tokens as $index => $token) {
            if ($token->isGivenKind(T_OPEN_TAG)) {
                $tokenContent = $token->getContent();

                if ('<?php' !== strtolower(substr($content, $tokensOldContentLength, 5))) {
                    $tokenContent = '<? ';
                }

                $tokensOldContent .= $tokenContent;
                $tokensOldContentLength += \strlen($tokenContent);

                continue;
            }

            if ($token->isGivenKind([T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING])) {
                $tokenContent = '';
                $tokenContentLength = 0;
                $parts = explode('<?php', $token->getContent());
                $iLast = \count($parts) - 1;

                foreach ($parts as $i => $part) {
                    $tokenContent .= $part;
                    $tokenContentLength += \strlen($part);

                    if ($i !== $iLast) {
                        $originalTokenContent = substr($content, $tokensOldContentLength + $tokenContentLength, 5);
                        if ('<?php' === strtolower($originalTokenContent)) {
                            $tokenContent .= $originalTokenContent;
                            $tokenContentLength += 5;
                        } else {
                            $tokenContent .= '<?';
                            $tokenContentLength += 2;
                        }
                    }
                }

                $tokens[$index] = new Token([$token->getId(), $tokenContent]);
                $token = $tokens[$index];
            }

            $tokensOldContent .= $token->getContent();
            $tokensOldContentLength += \strlen($token->getContent());
        }

        $tokensOrg->overrideRange(0, $tokensOrg->count() - 1, $tokens);
    }
protected function applyFix(\SplFileInfo $file, Tokens $tokensOrg)
    {
        $content = $tokensOrg->generateCode();

        // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
        $newContent = Preg::replace('/<\?(?:phP|pHp|pHP|Php|PhP|PHp|PHP)?(\s|$)/', '<?php$1', $content, -1, $count);

        if (!$count) {
            return;
        }

        /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
         * > echo '<? ';
         * with
         * > echo '<?php ';
         */
        $tokens = Tokens::fromCode($newContent);

        $tokensOldContent = '';
        $tokensOldContentLength = 0;

        foreach ($tokens as $index => $token) {
            if ($token->isGivenKind(T_OPEN_TAG)) {
                $tokenContent = $token->getContent();

                if ('<?php' !== strtolower(substr($content, $tokensOldContentLength, 5))) {
                    $tokenContent = '<? ';
                }

                $tokensOldContent .= $tokenContent;
                $tokensOldContentLength += \strlen($tokenContent);

                continue;
            }

            if ($token->isGivenKind([T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING])) {
                $tokenContent = '';
                $tokenContentLength = 0;
                $parts = explode('<?php', $token->getContent());
                $iLast = \count($parts) - 1;

                foreach ($parts as $i => $part) {
                    $tokenContent .= $part;
                    $tokenContentLength += \strlen($part);

                    if ($i !== $iLast) {
                        $originalTokenContent = substr($content, $tokensOldContentLength + $tokenContentLength, 5);
                        if ('<?php' === strtolower($originalTokenContent)) {
                            $tokenContent .= $originalTokenContent;
                            $tokenContentLength += 5;
                        } else {
                            $tokenContent .= '<?';
                            $tokenContentLength += 2;
                        }
                    }
                }

                $tokens[$index] = new Token([$token->getId(), $tokenContent]);
                $token = $tokens[$index];
            }

            $tokensOldContent .= $token->getContent();
            $tokensOldContentLength += \strlen($token->getContent());
        }

        $tokensOrg->overrideRange(0, $tokensOrg->count() - 1, $tokens);
    }
protected function applyFix ( \ SplFileInfo $ file , Tokens $ tokensOrg ) { $ content = $ tokensOrg -> generateCode ( ) ; // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled $ newContent = Preg :: replace ( '/<\?(?:phP|pHp|pHP|Php|PhP|PHp|PHP)?(\s|$)/' , '<?php$1' , $ content , - 1 , $ count ) ; if ( ! $ count ) { return ; } /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
         * > echo '<? ';
         * with
         * > echo '<?php ';
         */ $ tokens = Tokens :: fromCode ( $ newContent ) ; $ tokensOldContent = '' ; $ tokensOldContentLength = 0 ; foreach ( $ tokens as $ index => $ token ) { if ( $ token -> isGivenKind ( T_OPEN_TAG ) ) { $ tokenContent = $ token -> getContent ( ) ; if ( '<?php' !== strtolower ( substr ( $ content , $ tokensOldContentLength , 5 ) ) ) { $ tokenContent = '<? ' ; } $ tokensOldContent .= $ tokenContent ; $ tokensOldContentLength += \ strlen ( $ tokenContent ) ; continue ; } if ( $ token -> isGivenKind ( [ T_COMMENT , T_DOC_COMMENT , T_CONSTANT_ENCAPSED_STRING , T_ENCAPSED_AND_WHITESPACE , T_STRING ] ) ) { $ tokenContent = '' ; $ tokenContentLength = 0 ; $ parts = explode ( '<?php' , $ token -> getContent ( ) ) ; $ iLast = \ count ( $ parts ) - 1 ; foreach ( $ parts as $ i => $ part ) { $ tokenContent .= $ part ; $ tokenContentLength += \ strlen ( $ part ) ; if ( $ i !== $ iLast ) { $ originalTokenContent = substr ( $ content , $ tokensOldContentLength + $ tokenContentLength , 5 ) ; if ( '<?php' === strtolower ( $ originalTokenContent ) ) { $ tokenContent .= $ originalTokenContent ; $ tokenContentLength += 5 ; } else { $ tokenContent .= '<?' ; $ tokenContentLength += 2 ; } } } $ tokens [ $ index ] = new Token ( [ $ token -> getId ( ) , $ tokenContent ] ) ; $ token = $ tokens [ $ index ] ; } $ tokensOldContent .= $ token -> getContent ( ) ; $ tokensOldContentLength += \ strlen ( $ token -> getContent ( ) ) ; } $ tokensOrg -> overrideRange ( 0 , $ tokensOrg -> count ( ) - 1 , $ tokens ) ; }
{@inheritdoc}
{