1<?php
2/**
3 * Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @author    Marc McIntyre <mmcintyre@squiz.net>
11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13 * @link      http://pear.php.net/package/PHP_CodeSniffer
14 */
15
16/**
17 * Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff.
18 *
19 * Checks that there is no empty line after the opening brace of a function.
20 *
21 * @category  PHP
22 * @package   PHP_CodeSniffer
23 * @author    Greg Sherwood <gsherwood@squiz.net>
24 * @author    Marc McIntyre <mmcintyre@squiz.net>
25 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27 * @version   Release: @package_version@
28 * @link      http://pear.php.net/package/PHP_CodeSniffer
29 */
30class Squiz_Sniffs_WhiteSpace_FunctionOpeningBraceSpaceSniff implements PHP_CodeSniffer_Sniff
31{
32
33    /**
34     * A list of tokenizers this sniff supports.
35     *
36     * @var array
37     */
38    public $supportedTokenizers = array(
39                                   'PHP',
40                                   'JS',
41                                  );
42
43
44    /**
45     * Returns an array of tokens this test wants to listen for.
46     *
47     * @return array
48     */
49    public function register()
50    {
51        return array(
52                T_FUNCTION,
53                T_CLOSURE,
54               );
55
56    }//end register()
57
58
59    /**
60     * Processes this test, when one of its tokens is encountered.
61     *
62     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
63     * @param int                  $stackPtr  The position of the current token
64     *                                        in the stack passed in $tokens.
65     *
66     * @return void
67     */
68    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69    {
70        $tokens = $phpcsFile->getTokens();
71
72        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
73            // Probably an interface method.
74            return;
75        }
76
77        $openBrace   = $tokens[$stackPtr]['scope_opener'];
78        $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($openBrace + 1), null, true);
79
80        if ($nextContent === $tokens[$stackPtr]['scope_closer']) {
81             // The next bit of content is the closing brace, so this
82             // is an empty function and should have a blank line
83             // between the opening and closing braces.
84            return;
85        }
86
87        $braceLine = $tokens[$openBrace]['line'];
88        $nextLine  = $tokens[$nextContent]['line'];
89
90        $found = ($nextLine - $braceLine - 1);
91        if ($found > 0) {
92            $error = 'Expected 0 blank lines after opening function brace; %s found';
93            $data  = array($found);
94            $fix   = $phpcsFile->addFixableError($error, $openBrace, 'SpacingAfter', $data);
95            if ($fix === true) {
96                $phpcsFile->fixer->beginChangeset();
97                for ($i = ($openBrace + 1); $i < $nextContent; $i++) {
98                    if ($tokens[$i]['line'] === $nextLine) {
99                        break;
100                    }
101
102                    $phpcsFile->fixer->replaceToken($i, '');
103                }
104
105                $phpcsFile->fixer->addNewline($openBrace);
106                $phpcsFile->fixer->endChangeset();
107            }
108        }
109
110    }//end process()
111
112
113}//end class
114