<commit_before><?php
/**
 * Ensures USE blocks are declared correctly.
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Namespaces;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class UseDeclarationSniff implements Sniff
{


    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return [T_USE];

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token in
     *                                               the stack passed in $tokens.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr)
    {
        if ($this->shouldIgnoreUse($phpcsFile, $stackPtr) === true) {
            return;
        }

        $tokens = $phpcsFile->getTokens();

        // One space after the use keyword.
        if ($tokens[($stackPtr + 1)]['content'] !== ' ') {
            $error = 'There must be a single space after the USE keyword';
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse');
            if ($fix === true) {
                $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
            }
        }

        // Only one USE declaration allowed per statement.
        $next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
        if ($next !== false
            && $tokens[$next]['code'] !== T_SEMICOLON
            && $tokens[$next]['code'] !== T_CLOSE_TAG
        ) {
            $error = 'There must be one USE keyword per declaration';
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations');
            if ($fix === true) {
                if ($tokens[$next]['code'] === T_COMMA) {
                    $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use ');
                } else {
                    $baseUse      = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)));
                    $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));

                    $phpcsFile->fixer->beginChangeset();

                    // Remove base use statement.
                    for ($i = $stackPtr; $i <= $next; $i++) {
                        $phpcsFile->fixer->replaceToken($i, '');
                    }

                    // Convert grouped use statements into full use statements.
                    do {
                        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);

                        $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true);
                        for ($i = ($whitespace + 1); $i < $next; $i++) {
                            $phpcsFile->fixer->replaceToken($i, '');
                        }

                        if ($tokens[$next]['code'] === T_CONST || $tokens[$next]['code'] === T_FUNCTION) {
                            $phpcsFile->fixer->addContentBefore($next, 'use ');
                            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
                            $phpcsFile->fixer->addContentBefore($next, str_replace('use ', '', $baseUse));
                        } else {
                            $phpcsFile->fixer->addContentBefore($next, $baseUse);
                        }

                        $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly);
                        if ($next !== false) {
                            $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar);
                        }
                    } while ($next !== false);

                    $phpcsFile->fixer->replaceToken($closingCurly, '');

                    // Remove any trailing whitespace.
                    $next       = $phpcsFile->findNext(T_SEMICOLON, $closingCurly);
                    $whitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingCurly - 1), null, true);
                    for ($i = ($whitespace + 1); $i < $next; $i++) {
                        $phpcsFile->fixer->replaceToken($i, '');
                    }

                    $phpcsFile->fixer->endChangeset();
                }//end if
            }//end if
        }//end if

        // Make sure this USE comes after the first namespace declaration.
        $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
        if ($prev !== false) {
            $first = $phpcsFile->findNext(T_NAMESPACE, 1);
            if ($prev !== $first) {
                $error = 'USE declarations must go after the first namespace declaration';
                $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace');
            }
        } else {
            $next = $phpcsFile->findNext(T_NAMESPACE, ($stackPtr + 1));
            if ($next !== false) {
                $error = 'USE declarations must go after the namespace declaration';
                $phpcsFile->addError($error, $stackPtr, 'UseBeforeNamespace');
            }
        }

        // Only interested in the last USE statement from here onwards.
        $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1));
        while ($this->shouldIgnoreUse($phpcsFile, $nextUse) === true) {
            $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1));
            if ($nextUse === false) {
                break;<commit_msg>PSR2/UseDeclaration: make some variable names more descriptive<commit_after><?php
/**
 * Ensures USE blocks are declared correctly.
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Standards\PSR2\Sniffs\Namespaces;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class UseDeclarationSniff implements Sniff
{


    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array
     */
    public function register()
    {
        return [T_USE];

    }//end register()


    /**
     * Processes this test, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
     * @param int                         $stackPtr  The position of the current token in
     *                                               the stack passed in $tokens.
     *
     * @return void
     */
    public function process(File $phpcsFile, $stackPtr)
    {
        if ($this->shouldIgnoreUse($phpcsFile, $stackPtr) === true) {
            return;
        }

        $tokens = $phpcsFile->getTokens();

        // One space after the use keyword.
        if ($tokens[($stackPtr + 1)]['content'] !== ' ') {
            $error = 'There must be a single space after the USE keyword';
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterUse');
            if ($fix === true) {
                $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
            }
        }

        // Only one USE declaration allowed per statement.
        $next = $phpcsFile->findNext([T_COMMA, T_SEMICOLON, T_OPEN_USE_GROUP, T_CLOSE_TAG], ($stackPtr + 1));
        if ($next !== false
            && $tokens[$next]['code'] !== T_SEMICOLON
            && $tokens[$next]['code'] !== T_CLOSE_TAG
        ) {
            $error = 'There must be one USE keyword per declaration';
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations');
            if ($fix === true) {
                if ($tokens[$next]['code'] === T_COMMA) {
                    $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use ');
                } else {
                    $baseUse      = rtrim($phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)));
                    $closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));

                    $phpcsFile->fixer->beginChangeset();

                    // Remove base use statement.
                    for ($i = $stackPtr; $i <= $next; $i++) {
                        $phpcsFile->fixer->replaceToken($i, '');
                    }

                    // Convert grouped use statements into full use statements.
                    do {
                        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);

                        $nonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), null, true);
                        for ($i = ($nonWhitespace + 1); $i < $next; $i++) {
                            $phpcsFile->fixer->replaceToken($i, '');
                        }

                        if ($tokens[$next]['code'] === T_CONST || $tokens[$next]['code'] === T_FUNCTION) {
                            $phpcsFile->fixer->addContentBefore($next, 'use ');
                            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), $closingCurly, true);
                            $phpcsFile->fixer->addContentBefore($next, str_replace('use ', '', $baseUse));
                        } else {
                            $phpcsFile->fixer->addContentBefore($next, $baseUse);
                        }

                        $next = $phpcsFile->findNext(T_COMMA, ($next + 1), $closingCurly);
                        if ($next !== false) {
                            $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar);
                        }
                    } while ($next !== false);

                    $phpcsFile->fixer->replaceToken($closingCurly, '');

                    // Remove any trailing whitespace.
                    $next          = $phpcsFile->findNext(T_SEMICOLON, $closingCurly);
                    $nonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingCurly - 1), null, true);
                    for ($i = ($nonWhitespace + 1); $i < $next; $i++) {
                        $phpcsFile->fixer->replaceToken($i, '');
                    }

                    $phpcsFile->fixer->endChangeset();
                }//end if
            }//end if
        }//end if

        // Make sure this USE comes after the first namespace declaration.
        $prev = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
        if ($prev !== false) {
            $first = $phpcsFile->findNext(T_NAMESPACE, 1);
            if ($prev !== $first) {
                $error = 'USE declarations must go after the first namespace declaration';
                $phpcsFile->addError($error, $stackPtr, 'UseAfterNamespace');
            }
        } else {
            $next = $phpcsFile->findNext(T_NAMESPACE, ($stackPtr + 1));
            if ($next !== false) {
                $error = 'USE declarations must go after the namespace declaration';
                $phpcsFile->addError($error, $stackPtr, 'UseBeforeNamespace');
            }
        }

        // Only interested in the last USE statement from here onwards.
        $nextUse = $phpcsFile->findNext(T_USE, ($stackPtr + 1));
        while ($this->shouldIgnoreUse($phpcsFile, $nextUse) === true) {
            $nextUse = $phpcsFile->findNext(T_USE, ($nextUse + 1));
            if ($nextUse === false) {
                break;
{'fraction_non_alphanumeric': 0.1404465655360491, 'fraction_numerical': 0.006221237429691495, 'mean_word_length': 1.7107877107877107, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 103, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7588130', 'n_tokens_mistral': 3423, 'n_tokens_neox': 3229, 'n_words': 737}