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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-2.3.2/CodeSniffer/Standards/Generic/Sniffs/Formatting/DisallowMultipleStatementsSniff.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_Formatting_DisallowMultipleStatementsSniff.
4
 
 *
5
 
 * PHP version 5
6
 
 *
7
 
 * @category  PHP
8
 
 * @package   PHP_CodeSniffer
9
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 
 */
14
 
 
15
 
/**
16
 
 * Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff.
17
 
 *
18
 
 * Ensures each statement is on a line by itself.
19
 
 *
20
 
 * @category  PHP
21
 
 * @package   PHP_CodeSniffer
22
 
 * @author    Greg Sherwood <gsherwood@squiz.net>
23
 
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24
 
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
 
 * @version   Release: 2.3.2
26
 
 * @link      http://pear.php.net/package/PHP_CodeSniffer
27
 
 */
28
 
class Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff implements PHP_CodeSniffer_Sniff
29
 
{
30
 
 
31
 
 
32
 
    /**
33
 
     * Returns an array of tokens this test wants to listen for.
34
 
     *
35
 
     * @return array
36
 
     */
37
 
    public function register()
38
 
    {
39
 
        return array(T_SEMICOLON);
40
 
 
41
 
    }//end register()
42
 
 
43
 
 
44
 
    /**
45
 
     * Processes this test, when one of its tokens is encountered.
46
 
     *
47
 
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
 
     * @param int                  $stackPtr  The position of the current token in
49
 
     *                                        the stack passed in $tokens.
50
 
     *
51
 
     * @return void
52
 
     */
53
 
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
 
    {
55
 
        $tokens = $phpcsFile->getTokens();
56
 
 
57
 
        $prev = $phpcsFile->findPrevious(array(T_SEMICOLON, T_OPEN_TAG), ($stackPtr - 1));
58
 
        if ($prev === false || $tokens[$prev]['code'] === T_OPEN_TAG) {
59
 
            $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no');
60
 
            return;
61
 
        }
62
 
 
63
 
        // Ignore multiple statements in a FOR condition.
64
 
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
65
 
            foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) {
66
 
                if (isset($tokens[$bracket]['parenthesis_owner']) === false) {
67
 
                    // Probably a closure sitting inside a function call.
68
 
                    continue;
69
 
                }
70
 
 
71
 
                $owner = $tokens[$bracket]['parenthesis_owner'];
72
 
                if ($tokens[$owner]['code'] === T_FOR) {
73
 
                    return;
74
 
                }
75
 
            }
76
 
        }
77
 
 
78
 
        if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
79
 
            $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'yes');
80
 
 
81
 
            $error = 'Each PHP statement must be on a line by itself';
82
 
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SameLine');
83
 
            if ($fix === true) {
84
 
                $phpcsFile->fixer->beginChangeset();
85
 
                $phpcsFile->fixer->addNewline($prev);
86
 
                if ($tokens[($prev + 1)]['code'] === T_WHITESPACE) {
87
 
                    $phpcsFile->fixer->replaceToken(($prev + 1), '');
88
 
                }
89
 
 
90
 
                $phpcsFile->fixer->endChangeset();
91
 
            }
92
 
        } else {
93
 
            $phpcsFile->recordMetric($stackPtr, 'Multiple statements on same line', 'no');
94
 
        }
95
 
 
96
 
    }//end process()
97
 
 
98
 
 
99
 
}//end class