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