1<?php
2/**
3 * Squiz_Sniffs_WhiteSpace_PropertyLabelSpacingSniff.
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 * Squiz_Sniffs_WhiteSpace_PropertyLabelSpacingSniff.
17 *
18 * Ensures that the colon in a property or label definition has a single
19 * space after it and no space before it.
20 *
21 * @category  PHP
22 * @package   PHP_CodeSniffer
23 * @author    Greg Sherwood <gsherwood@squiz.net>
24 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
25 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26 * @version   Release: @package_version@
27 * @link      http://pear.php.net/package/PHP_CodeSniffer
28 */
29class Squiz_Sniffs_WhiteSpace_PropertyLabelSpacingSniff implements PHP_CodeSniffer_Sniff
30{
31
32    /**
33     * A list of tokenizers this sniff supports.
34     *
35     * @var array
36     */
37    public $supportedTokenizers = array('JS');
38
39
40    /**
41     * Returns an array of tokens this test wants to listen for.
42     *
43     * @return array
44     */
45    public function register()
46    {
47        return array(
48                T_PROPERTY,
49                T_LABEL,
50               );
51
52    }//end register()
53
54
55    /**
56     * Processes this test, when one of its tokens is encountered.
57     *
58     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
59     * @param int                  $stackPtr  The position of the current token
60     *                                        in the stack passed in $tokens.
61     *
62     * @return void
63     */
64    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
65    {
66        $tokens = $phpcsFile->getTokens();
67
68        $colon = $phpcsFile->findNext(T_COLON, ($stackPtr + 1));
69
70        if ($colon !== ($stackPtr + 1)) {
71            $error = 'There must be no space before the colon in a property/label declaration';
72            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Before');
73            if ($fix === true) {
74                $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
75            }
76        }
77
78        if ($tokens[($colon + 1)]['code'] !== T_WHITESPACE || $tokens[($colon + 1)]['content'] !== ' ') {
79            $error = 'There must be a single space after the colon in a property/label declaration';
80            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'After');
81            if ($fix === true) {
82                if ($tokens[($colon + 1)]['code'] === T_WHITESPACE) {
83                    $phpcsFile->fixer->replaceToken(($colon + 1), ' ');
84                } else {
85                    $phpcsFile->fixer->addContent($colon, ' ');
86                }
87            }
88        }
89
90    }//end process()
91
92
93}//end class
94