~ubuntu-branches/ubuntu/saucy/php-codesniffer/saucy

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jack Bates
  • Date: 2008-10-01 17:39:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20081001173943-2dy06n1e8zwyw1o8
Tags: 1.1.0-1
* New upstream release
* Acknowledge NMU, thanks Jan

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