~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-07-12 15:16:25 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130712151625-4autdc0twzbueha9
Tags: 1.5.0~rc2-1
* New upstream release.
* Refreshed patch.
* Standards-Version is now 3.9.4.
* Canonical VCS URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff.
 
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-2012 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
 * Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff.
 
18
 *
 
19
 * Checks that the opening brace of a function is on the line after the
 
20
 * function declaration.
 
21
 *
 
22
 * @category  PHP
 
23
 * @package   PHP_CodeSniffer
 
24
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
25
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
26
 * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
 
27
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
28
 * @version   Release: 1.5.0RC2
 
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
30
 */
 
31
class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff
 
32
{
 
33
 
 
34
 
 
35
    /**
 
36
     * Registers the tokens that this sniff wants to listen for.
 
37
     *
 
38
     * @return void
 
39
     */
 
40
    public function register()
 
41
    {
 
42
        return array(T_FUNCTION);
 
43
 
 
44
    }//end register()
 
45
 
 
46
 
 
47
    /**
 
48
     * Processes this test, when one of its tokens is encountered.
 
49
     *
 
50
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
51
     * @param int                  $stackPtr  The position of the current token in the
 
52
     *                                        stack passed in $tokens.
 
53
     *
 
54
     * @return void
 
55
     */
 
56
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
57
    {
 
58
        $tokens = $phpcsFile->getTokens();
 
59
 
 
60
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
 
61
            return;
 
62
        }
 
63
 
 
64
        $openingBrace = $tokens[$stackPtr]['scope_opener'];
 
65
 
 
66
        // The end of the function occurs at the end of the argument list. Its
 
67
        // like this because some people like to break long function declarations
 
68
        // over multiple lines.
 
69
        $functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
 
70
        $braceLine    = $tokens[$openingBrace]['line'];
 
71
 
 
72
        $lineDifference = ($braceLine - $functionLine);
 
73
 
 
74
        if ($lineDifference === 0) {
 
75
            $error = 'Opening brace should be on a new line';
 
76
            $phpcsFile->addError($error, $openingBrace, 'BraceOnSameLine');
 
77
            return;
 
78
        }
 
79
 
 
80
        if ($lineDifference > 1) {
 
81
            $error = 'Opening brace should be on the line after the declaration; found %s blank line(s)';
 
82
            $data  = array(($lineDifference - 1));
 
83
            $phpcsFile->addError($error, $openingBrace, 'BraceSpacing', $data);
 
84
            return;
 
85
        }
 
86
 
 
87
        // We need to actually find the first piece of content on this line,
 
88
        // as if this is a method with tokens before it (public, static etc)
 
89
        // or an if with an else before it, then we need to start the scope
 
90
        // checking from there, rather than the current token.
 
91
        $lineStart = $stackPtr;
 
92
        while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) {
 
93
            if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) {
 
94
                break;
 
95
            }
 
96
        }
 
97
 
 
98
        // We found a new line, now go forward and find the first non-whitespace
 
99
        // token.
 
100
        $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true);
 
101
 
 
102
        // The opening brace is on the correct line, now it needs to be
 
103
        // checked to be correctly indented.
 
104
        $startColumn = $tokens[$lineStart]['column'];
 
105
        $braceIndent = $tokens[$openingBrace]['column'];
 
106
 
 
107
        if ($braceIndent !== $startColumn) {
 
108
            $error = 'Opening brace indented incorrectly; expected %s spaces, found %s';
 
109
            $data  = array(
 
110
                      ($startColumn - 1),
 
111
                      ($braceIndent - 1),
 
112
                     );
 
113
            $phpcsFile->addError($error, $openingBrace, 'BraceIndent', $data);
 
114
        }
 
115
 
 
116
    }//end process()
 
117
 
 
118
 
 
119
}//end class
 
120
 
 
121
?>