1: <?php
2: /**
3: * Slight modifications for Azalea
4: */
5:
6: /**
7: * Squiz_Sniffs_Whitespace_ScopeClosingBraceSniff.
8: *
9: * PHP version 5
10: *
11: * @category PHP
12: * @package PHP_CodeSniffer
13: * @author Greg Sherwood <gsherwood@squiz.net>
14: * @author Marc McIntyre <mmcintyre@squiz.net>
15: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
16: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
17: * @link http://pear.php.net/package/PHP_CodeSniffer
18: */
19:
20: /**
21: * Squiz_Sniffs_Whitespace_ScopeClosingBraceSniff.
22: *
23: * Checks that the closing braces of scopes are aligned correctly.
24: *
25: * @category PHP
26: * @package PHP_CodeSniffer
27: * @author Greg Sherwood <gsherwood@squiz.net>
28: * @author Marc McIntyre <mmcintyre@squiz.net>
29: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
30: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
31: * @version Release: @package_version@
32: * @link http://pear.php.net/package/PHP_CodeSniffer
33: */
34: class Azalea_Sniffs_WhiteSpace_ScopeClosingBraceSniff implements PHP_CodeSniffer_Sniff
35: {
36:
37:
38: /**
39: * Returns an array of tokens this test wants to listen for.
40: *
41: * @return array
42: */
43: public function register()
44: {
45: return PHP_CodeSniffer_Tokens::$scopeOpeners;
46:
47: }//end register()
48:
49:
50: /**
51: * Processes this test, when one of its tokens is encountered.
52: *
53: * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
54: * @param int $stackPtr The position of the current token in the
55: * stack passed in $tokens.
56: *
57: * @return void
58: */
59: public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60: {
61: $tokens = $phpcsFile->getTokens();
62:
63: // If this is an inline condition (ie. there is no scope opener), then
64: // return, as this is not a new scope.
65: if (isset($tokens[$stackPtr]['scope_closer']) === false) {
66: return;
67: }
68:
69: // We need to actually find the first piece of content on this line,
70: // as if this is a method with tokens before it (public, static etc)
71: // or an if with an else before it, then we need to start the scope
72: // checking from there, rather than the current token.
73: $lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
74:
75: $startColumn = $tokens[$lineStart]['column'];
76: $scopeStart = $tokens[$stackPtr]['scope_opener'];
77: $scopeEnd = $tokens[$stackPtr]['scope_closer'];
78:
79: // Check that the closing brace is on it's own line.
80: $lastContent = $phpcsFile->findPrevious(array(T_INLINE_HTML, T_WHITESPACE, T_OPEN_TAG), ($scopeEnd - 1), $scopeStart, true);
81: if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) {
82: $error = 'Closing brace must be on a line by itself';
83: $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore');
84: if ($fix === true) {
85: $phpcsFile->fixer->addNewlineBefore($scopeEnd);
86: }
87:
88: return;
89: }
90:
91: // Check now that the closing brace is lined up correctly.
92: $lineStart = $phpcsFile->findFirstOnLine(T_WHITESPACE, $scopeEnd, true);
93: $braceIndent = $tokens[$lineStart]['column'];
94: if ($tokens[$stackPtr]['code'] !== T_DEFAULT
95: && $tokens[$stackPtr]['code'] !== T_CASE
96: && $braceIndent !== $startColumn
97: ) {
98: $error = 'Closing brace indented incorrectly; expected %s spaces, found %s';
99: $data = array(
100: ($startColumn - 1),
101: ($braceIndent - 1),
102: );
103:
104: $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Indent', $data);
105: if ($fix === true) {
106: $diff = ($startColumn - $braceIndent);
107: if ($diff > 0) {
108: $phpcsFile->fixer->addContentBefore($scopeEnd, str_repeat("\t", $diff));
109: } else {
110: $phpcsFile->fixer->substrToken(($scopeEnd - 1), 0, $diff);
111: }
112: }
113: }//end if
114:
115: }//end process()
116:
117:
118: }//end class
119: