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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20140721144241-t0v9knmgtzftbsw6
Tags: upstream-1.5.3
Import upstream version 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff.
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_OpeningFunctionBraceKernighanRitchieSniff.
18
 
 *
19
 
 * Checks that the opening brace of a function is on the same line
20
 
 * as the 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_OpeningFunctionBraceKernighanRitchieSniff 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 the same line as the declaration';
76
 
            $phpcsFile->addError($error, $openingBrace, 'BraceOnNewLine');
77
 
            return;
78
 
        }
79
 
 
80
 
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
81
 
        if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
82
 
            $length = 0;
83
 
        } else if ($tokens[($closeBracket + 1)]['content'] === "\t") {
84
 
            $length = '\t';
85
 
        } else {
86
 
            $length = strlen($tokens[($closeBracket + 1)]['content']);
87
 
        }
88
 
 
89
 
        if ($length !== 1) {
90
 
            $error = 'Expected 1 space after closing parenthesis; found %s';
91
 
            $data  = array($length);
92
 
            $phpcsFile->addError($error, $closeBracket, 'SpaceAfterBracket', $data);
93
 
            return;
94
 
        }
95
 
 
96
 
        $closeBrace = $tokens[$stackPtr]['scope_opener'];
97
 
        if ($tokens[($closeBrace - 1)]['code'] !== T_WHITESPACE) {
98
 
            $length = 0;
99
 
        } else if ($tokens[($closeBrace - 1)]['content'] === "\t") {
100
 
            $length = '\t';
101
 
        } else {
102
 
            $length = strlen($tokens[($closeBrace - 1)]['content']);
103
 
        }
104
 
 
105
 
        if ($length !== 1) {
106
 
            $error = 'Expected 1 space before opening brace; found %s';
107
 
            $data  = array($length);
108
 
            $phpcsFile->addError($error, $openingBrace, 'SpaceBeforeBrace', $data);
109
 
            return;
110
 
        }
111
 
 
112
 
    }//end process()
113
 
 
114
 
 
115
 
}//end class
116
 
 
117
 
?>