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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood
  • Date: 2015-06-24 13:41:36 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20150624134136-dv60dnl6s20tdxwr
Tags: 2.3.3-1
[ Greg Sherwood ]
Prepare for 2.3.3 release

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-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
 * 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-2014 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: 2.3.3
 
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
     * Should this sniff check function braces?
 
37
     *
 
38
     * @var bool
 
39
     */
 
40
    public $checkFunctions = true;
 
41
 
 
42
    /**
 
43
     * Should this sniff check closure braces?
 
44
     *
 
45
     * @var bool
 
46
     */
 
47
    public $checkClosures = false;
 
48
 
 
49
 
 
50
    /**
 
51
     * Registers the tokens that this sniff wants to listen for.
 
52
     *
 
53
     * @return void
 
54
     */
 
55
    public function register()
 
56
    {
 
57
        return array(
 
58
                T_FUNCTION,
 
59
                T_CLOSURE,
 
60
               );
 
61
 
 
62
    }//end register()
 
63
 
 
64
 
 
65
    /**
 
66
     * Processes this test, when one of its tokens is encountered.
 
67
     *
 
68
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
69
     * @param int                  $stackPtr  The position of the current token in the
 
70
     *                                        stack passed in $tokens.
 
71
     *
 
72
     * @return void
 
73
     */
 
74
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
75
    {
 
76
        $tokens = $phpcsFile->getTokens();
 
77
 
 
78
        if (isset($tokens[$stackPtr]['scope_opener']) === false) {
 
79
            return;
 
80
        }
 
81
 
 
82
        if (($tokens[$stackPtr]['code'] === T_FUNCTION
 
83
            && (bool) $this->checkFunctions === false)
 
84
            || ($tokens[$stackPtr]['code'] === T_CLOSURE
 
85
            && (bool) $this->checkClosures === false)
 
86
        ) {
 
87
            return;
 
88
        }
 
89
 
 
90
        $openingBrace = $tokens[$stackPtr]['scope_opener'];
 
91
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
 
92
        if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
 
93
            $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
 
94
            if ($use !== false) {
 
95
                $openBracket  = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
 
96
                $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
 
97
            }
 
98
        }
 
99
 
 
100
        $functionLine = $tokens[$closeBracket]['line'];
 
101
        $braceLine    = $tokens[$openingBrace]['line'];
 
102
 
 
103
        $lineDifference = ($braceLine - $functionLine);
 
104
 
 
105
        if ($lineDifference > 0) {
 
106
            $phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'new line');
 
107
            $error = 'Opening brace should be on the same line as the declaration';
 
108
            $fix   = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
 
109
            if ($fix === true) {
 
110
                $phpcsFile->fixer->beginChangeset();
 
111
                $phpcsFile->fixer->addContent($closeBracket, ' {');
 
112
                $phpcsFile->fixer->replaceToken($openingBrace, '');
 
113
                $phpcsFile->fixer->endChangeset();
 
114
            }
 
115
        }
 
116
 
 
117
        $phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'same line');
 
118
 
 
119
        $next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true);
 
120
        if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
 
121
            if ($next === $tokens[$stackPtr]['scope_closer']) {
 
122
                // Ignore empty functions.
 
123
                return;
 
124
            }
 
125
 
 
126
            $error = 'Opening brace must be the last content on the line';
 
127
            $fix   = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
 
128
            if ($fix === true) {
 
129
                $phpcsFile->fixer->addNewline($openingBrace);
 
130
            }
 
131
        }
 
132
 
 
133
        // Only continue checking if the opening brace looks good.
 
134
        if ($lineDifference > 0) {
 
135
            return;
 
136
        }
 
137
 
 
138
        if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
 
139
            $length = 0;
 
140
        } else if ($tokens[($closeBracket + 1)]['content'] === "\t") {
 
141
            $length = '\t';
 
142
        } else {
 
143
            $length = strlen($tokens[($closeBracket + 1)]['content']);
 
144
        }
 
145
 
 
146
        if ($length !== 1) {
 
147
            $error = 'Expected 1 space after closing parenthesis; found %s';
 
148
            $data  = array($length);
 
149
            $fix   = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceAfterBracket', $data);
 
150
            if ($fix === true) {
 
151
                if ($length === 0 || $length === '\t') {
 
152
                    $phpcsFile->fixer->addContent($closeBracket, ' ');
 
153
                } else {
 
154
                    $phpcsFile->fixer->replaceToken(($closeBracket + 1), ' ');
 
155
                }
 
156
            }
 
157
        }
 
158
 
 
159
    }//end process()
 
160
 
 
161
 
 
162
}//end class