1<?php
2/**
3 * Squiz_Sniffs_Whitespace_ScopeClosingBraceSniff.
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_ScopeClosingBraceSniff.
18 *
19 * Checks that the closing braces of scopes are aligned correctly.
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_ScopeClosingBraceSniff implements PHP_CodeSniffer_Sniff
31{
32
33
34    /**
35     * Returns an array of tokens this test wants to listen for.
36     *
37     * @return array
38     */
39    public function register()
40    {
41        return PHP_CodeSniffer_Tokens::$scopeOpeners;
42
43    }//end register()
44
45
46    /**
47     * Processes this test, when one of its tokens is encountered.
48     *
49     * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
50     * @param int                  $stackPtr  The position of the current token in the
51     *                                        stack passed in $tokens.
52     *
53     * @return void
54     */
55    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
56    {
57        $tokens = $phpcsFile->getTokens();
58
59        // If this is an inline condition (ie. there is no scope opener), then
60        // return, as this is not a new scope.
61        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
62            return;
63        }
64
65        // We need to actually find the first piece of content on this line,
66        // as if this is a method with tokens before it (public, static etc)
67        // or an if with an else before it, then we need to start the scope
68        // checking from there, rather than the current token.
69        $lineStart = $phpcsFile->findFirstOnLine(array(T_WHITESPACE, T_INLINE_HTML), $stackPtr, true);
70
71        $startColumn = $tokens[$lineStart]['column'];
72        $scopeStart  = $tokens[$stackPtr]['scope_opener'];
73        $scopeEnd    = $tokens[$stackPtr]['scope_closer'];
74
75        // Check that the closing brace is on it's own line.
76        $lastContent = $phpcsFile->findPrevious(array(T_INLINE_HTML, T_WHITESPACE, T_OPEN_TAG), ($scopeEnd - 1), $scopeStart, true);
77        if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
78            $error = 'Closing brace must be on a line by itself';
79            $fix   = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore');
80            if ($fix === true) {
81                $phpcsFile->fixer->addNewlineBefore($scopeEnd);
82            }
83
84            return;
85        }
86
87        // Check now that the closing brace is lined up correctly.
88        $lineStart   = $phpcsFile->findFirstOnLine(array(T_WHITESPACE, T_INLINE_HTML), $scopeEnd, true);
89        $braceIndent = $tokens[$lineStart]['column'];
90        if ($tokens[$stackPtr]['code'] !== T_DEFAULT
91            && $tokens[$stackPtr]['code'] !== T_CASE
92            && $braceIndent !== $startColumn
93        ) {
94            $error = 'Closing brace indented incorrectly; expected %s spaces, found %s';
95            $data  = array(
96                      ($startColumn - 1),
97                      ($braceIndent - 1),
98                     );
99
100            $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Indent', $data);
101            if ($fix === true) {
102                $diff = ($startColumn - $braceIndent);
103                if ($diff > 0) {
104                    $phpcsFile->fixer->addContentBefore($lineStart, str_repeat(' ', $diff));
105                } else {
106                    $phpcsFile->fixer->substrToken(($lineStart - 1), 0, $diff);
107                }
108            }
109        }//end if
110
111    }//end process()
112
113
114}//end class
115