1<?php
2/**
3 * Bans the use of the PHP long array syntax.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Bans the use of the PHP long array syntax.
17 *
18 * @category  PHP
19 * @package   PHP_CodeSniffer
20 * @author    Greg Sherwood <gsherwood@squiz.net>
21 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
22 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23 * @version   Release: @package_version@
24 * @link      http://pear.php.net/package/PHP_CodeSniffer
25 */
26class Generic_Sniffs_Arrays_DisallowLongArraySyntaxSniff implements PHP_CodeSniffer_Sniff
27{
28
29
30    /**
31     * Registers the tokens that this sniff wants to listen for.
32     *
33     * @return int[]
34     */
35    public function register()
36    {
37        return array(T_ARRAY);
38
39    }//end register()
40
41
42    /**
43     * Processes this test, when one of its tokens is encountered.
44     *
45     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46     * @param int                  $stackPtr  The position of the current token
47     *                                        in the stack passed in $tokens.
48     *
49     * @return void
50     */
51    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52    {
53        $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
54
55        $error = 'Short array syntax must be used to define arrays';
56        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Found');
57
58        if ($fix === true) {
59            $tokens = $phpcsFile->getTokens();
60            $opener = $tokens[$stackPtr]['parenthesis_opener'];
61            $closer = $tokens[$stackPtr]['parenthesis_closer'];
62
63            $phpcsFile->fixer->beginChangeset();
64
65            if ($opener === null) {
66                $phpcsFile->fixer->replaceToken($stackPtr, '[]');
67            } else {
68                $phpcsFile->fixer->replaceToken($stackPtr, '');
69                $phpcsFile->fixer->replaceToken($opener, '[');
70                $phpcsFile->fixer->replaceToken($closer, ']');
71            }
72
73            $phpcsFile->fixer->endChangeset();
74        }
75
76    }//end process()
77
78
79}//end class
80