Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DisallowLongArraySyntaxSniff.php

class DisallowLongArraySyntaxSniff

Same name in this branch
  1. 11.1.x vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php \Drupal\Sniffs\Arrays\DisallowLongArraySyntaxSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DisallowLongArraySyntaxSniff

1 file declares its use of DisallowLongArraySyntaxSniff
DisallowLongArraySyntaxSniff.php in vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php, line 15

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays
View source
class DisallowLongArraySyntaxSniff implements Sniff {
    
    /**
     * Registers the tokens that this sniff wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        return [
            T_ARRAY,
        ];
    }
    
    //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) {
        $tokens = $phpcsFile->getTokens();
        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
        $error = 'Short array syntax must be used to define arrays';
        if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
            // Live coding/parse error, just show the error, don't try and fix it.
            $phpcsFile->addError($error, $stackPtr, 'Found');
            return;
        }
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
        if ($fix === true) {
            $opener = $tokens[$stackPtr]['parenthesis_opener'];
            $closer = $tokens[$stackPtr]['parenthesis_closer'];
            $phpcsFile->fixer
                ->beginChangeset();
            $phpcsFile->fixer
                ->replaceToken($stackPtr, '');
            $phpcsFile->fixer
                ->replaceToken($opener, '[');
            $phpcsFile->fixer
                ->replaceToken($closer, ']');
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DisallowLongArraySyntaxSniff::process public function Processes this test, when one of its tokens is encountered. Overrides Sniff::process 1
DisallowLongArraySyntaxSniff::register public function Registers the tokens that this sniff wants to listen for. Overrides Sniff::register
RSS feed
Powered by Drupal