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

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.3/CodeSniffer/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140721144241-g4orlcuk4jzn9mhs
Tags: 1.5.3-1
* Team upload
* Focus on stable release
* Update copyright
* Bump standards version to 3.9.5
* Update Homepage
* Use ${phppear:…} instead of hardcoding them
* Run tests in dh_auto_test
* Update patch with gbp pq
* Simplify configuration installation
* Edit package.xml to move script
* Add DEP-8 tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * PSR2_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
 
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
 * PSR2_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
 
17
 *
 
18
 * Checks that control structures have the correct spacing around brackets.
 
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: 1.5.3
 
26
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
27
 */
 
28
class PSR2_Sniffs_ControlStructures_ControlStructureSpacingSniff implements PHP_CodeSniffer_Sniff
 
29
{
 
30
 
 
31
 
 
32
    /**
 
33
     * How many spaces should follow the opening bracket.
 
34
     *
 
35
     * @var int
 
36
     */
 
37
    public $requiredSpacesAfterOpen = 0;
 
38
 
 
39
    /**
 
40
     * How many spaces should precede the closing bracket.
 
41
     *
 
42
     * @var int
 
43
     */
 
44
    public $requiredSpacesBeforeClose = 0;
 
45
 
 
46
 
 
47
    /**
 
48
     * Returns an array of tokens this test wants to listen for.
 
49
     *
 
50
     * @return array
 
51
     */
 
52
    public function register()
 
53
    {
 
54
        return array(
 
55
                T_IF,
 
56
                T_WHILE,
 
57
                T_FOREACH,
 
58
                T_FOR,
 
59
                T_SWITCH,
 
60
                T_DO,
 
61
                T_ELSE,
 
62
                T_ELSEIF,
 
63
               );
 
64
 
 
65
    }//end register()
 
66
 
 
67
 
 
68
    /**
 
69
     * Processes this test, when one of its tokens is encountered.
 
70
     *
 
71
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
72
     * @param int                  $stackPtr  The position of the current token
 
73
     *                                        in the stack passed in $tokens.
 
74
     *
 
75
     * @return void
 
76
     */
 
77
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
78
    {
 
79
        $this->requiredSpacesAfterOpen   = (int) $this->requiredSpacesAfterOpen;
 
80
        $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
 
81
        $tokens = $phpcsFile->getTokens();
 
82
 
 
83
        if (isset($tokens[$stackPtr]['parenthesis_opener']) === true) {
 
84
            $parenOpener    = $tokens[$stackPtr]['parenthesis_opener'];
 
85
            $parenCloser    = $tokens[$stackPtr]['parenthesis_closer'];
 
86
            $spaceAfterOpen = 0;
 
87
            if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) {
 
88
                $spaceAfterOpen = strlen(rtrim($tokens[($parenOpener + 1)]['content'], $phpcsFile->eolChar));
 
89
            }
 
90
 
 
91
            if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
 
92
                $error = 'Expected %s spaces after opening bracket; %s found';
 
93
                $data  = array(
 
94
                          $this->requiredSpacesAfterOpen,
 
95
                          $spaceAfterOpen,
 
96
                         );
 
97
                $phpcsFile->addError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data);
 
98
            }
 
99
 
 
100
            if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line'] ) {
 
101
                $spaceBeforeClose = 0;
 
102
                if ($tokens[($parenCloser - 1)]['code'] === T_WHITESPACE) {
 
103
                    $spaceBeforeClose = strlen(ltrim($tokens[($parenCloser - 1)]['content'], $phpcsFile->eolChar));
 
104
                }
 
105
 
 
106
                if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
 
107
                    $error = 'Expected %s spaces before closing bracket; %s found';
 
108
                    $data  = array(
 
109
                              $this->requiredSpacesBeforeClose,
 
110
                              $spaceBeforeClose,
 
111
                             );
 
112
                    $phpcsFile->addError($error, ($parenCloser - 1), 'SpaceBeforeCloseBrace', $data);
 
113
                }
 
114
            }
 
115
        }//end if
 
116
 
 
117
    }//end process()
 
118
 
 
119
 
 
120
}//end class