PageRenderTime 29ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php

https://gitlab.com/trang1104/portable_project
PHP | 211 lines | 129 code | 27 blank | 55 comment | 25 complexity | 7996f6fd4ed01491f0cd635ae215ed65 MD5 | raw file
  1<?php
  2/**
  3 * Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
  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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 12 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 13 * @link      http://pear.php.net/package/PHP_CodeSniffer
 14 */
 15
 16/**
 17 * Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff.
 18 *
 19 * Checks that control structures have the correct spacing around brackets.
 20 *
 21 * @category  PHP
 22 * @package   PHP_CodeSniffer
 23 * @author    Greg Sherwood <gsherwood@squiz.net>
 24 * @author    Marc McIntyre <mmcintyre@squiz.net>
 25 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 26 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 27 * @version   Release: 1.3.3
 28 * @link      http://pear.php.net/package/PHP_CodeSniffer
 29 */
 30class Squiz_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_CodeSniffer_Sniff
 31{
 32
 33    /**
 34     * A list of tokenizers this sniff supports.
 35     *
 36     * @var array
 37     */
 38    public $supportedTokenizers = array(
 39                                   'PHP',
 40                                   'JS',
 41                                  );
 42
 43
 44    /**
 45     * Returns an array of tokens this test wants to listen for.
 46     *
 47     * @return array
 48     */
 49    public function register()
 50    {
 51        return array(
 52                T_IF,
 53                T_WHILE,
 54                T_FOREACH,
 55                T_FOR,
 56                T_SWITCH,
 57                T_DO,
 58                T_ELSE,
 59                T_ELSEIF,
 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
 70     *                                        in the 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_closer']) === false) {
 79            return;
 80        }
 81
 82        if (isset($tokens[$stackPtr]['parenthesis_opener']) === true) {
 83            $parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
 84            $parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
 85            if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) {
 86                $gap   = strlen($tokens[($parenOpener + 1)]['content']);
 87                $error = 'Expected 0 spaces after opening bracket; %s found';
 88                $data  = array($gap);
 89                $phpcsFile->addError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data);
 90            }
 91
 92            if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']
 93                && $tokens[($parenCloser - 1)]['code'] === T_WHITESPACE
 94            ) {
 95                $gap   = strlen($tokens[($parenCloser - 1)]['content']);
 96                $error = 'Expected 0 spaces before closing bracket; %s found';
 97                $data  = array($gap);
 98                $phpcsFile->addError($error, ($parenCloser - 1), 'SpaceBeforeCloseBrace', $data);
 99            }
100        }//end if
101
102        $scopeOpener = $tokens[$stackPtr]['scope_opener'];
103        $scopeCloser = $tokens[$stackPtr]['scope_closer'];
104
105        $firstContent = $phpcsFile->findNext(
106            T_WHITESPACE,
107            ($scopeOpener + 1),
108            null,
109            true
110        );
111
112        if ($tokens[$firstContent]['line'] !== ($tokens[$scopeOpener]['line'] + 1)) {
113            $error = 'Blank line found at start of control structure';
114            $phpcsFile->addError($error, $scopeOpener, 'SpacingBeforeOpen');
115        }
116
117        $lastContent = $phpcsFile->findPrevious(
118            T_WHITESPACE,
119            ($scopeCloser - 1),
120            null,
121            true
122        );
123
124        if ($tokens[$lastContent]['line'] !== ($tokens[$scopeCloser]['line'] - 1)) {
125            $errorToken = $scopeCloser;
126            for ($i = ($scopeCloser - 1); $i > $lastContent; $i--) {
127                if ($tokens[$i]['line'] < $tokens[$scopeCloser]['line']) {
128                    $errorToken = $i;
129                    break;
130                }
131            }
132
133            $error = 'Blank line found at end of control structure';
134            $phpcsFile->addError($error, $errorToken, 'SpacingAfterClose');
135        }
136
137        $trailingContent = $phpcsFile->findNext(
138            T_WHITESPACE,
139            ($scopeCloser + 1),
140            null,
141            true
142        );
143
144        if ($tokens[$trailingContent]['code'] === T_ELSE) {
145            if ($tokens[$stackPtr]['code'] === T_IF) {
146                // IF with ELSE.
147                return;
148            }
149        }
150
151        if ($tokens[$trailingContent]['code'] === T_COMMENT) {
152            if ($tokens[$trailingContent]['line'] === $tokens[$scopeCloser]['line']) {
153                if (substr($tokens[$trailingContent]['content'], 0, 5) === '//end') {
154                    // There is an end comment, so we have to get the next piece
155                    // of content.
156                    $trailingContent = $phpcsFile->findNext(
157                        T_WHITESPACE,
158                        ($trailingContent + 1),
159                        null,
160                        true
161                    );
162                }
163            }
164        }
165
166        if ($tokens[$trailingContent]['code'] === T_BREAK) {
167            // If this BREAK is closing a CASE, we don't need the
168            // blank line after this control structure.
169            if (isset($tokens[$trailingContent]['scope_condition']) === true) {
170                $condition = $tokens[$trailingContent]['scope_condition'];
171                if ($tokens[$condition]['code'] === T_CASE
172                    || $tokens[$condition]['code'] === T_DEFAULT
173                ) {
174                    return;
175                }
176            }
177        }
178
179        if ($tokens[$trailingContent]['code'] === T_CLOSE_TAG) {
180            // At the end of the script or embedded code.
181            return;
182        }
183
184        if ($tokens[$trailingContent]['code'] === T_CLOSE_CURLY_BRACKET) {
185            // Another control structure's closing brace.
186            if (isset($tokens[$trailingContent]['scope_condition']) === true) {
187                $owner = $tokens[$trailingContent]['scope_condition'];
188                if ($tokens[$owner]['code'] === T_FUNCTION) {
189                    // The next content is the closing brace of a function
190                    // so normal function rules apply and we can ignore it.
191                    return;
192                }
193            }
194
195            if ($tokens[$trailingContent]['line'] !== ($tokens[$scopeCloser]['line'] + 1)) {
196                $error = 'Blank line found after control structure';
197                $phpcsFile->addError($error, $scopeCloser, 'LineAfterClose');
198            }
199        } else {
200            if ($tokens[$trailingContent]['line'] === ($tokens[$scopeCloser]['line'] + 1)) {
201                $error = 'No blank line found after control structure';
202                $phpcsFile->addError($error, $scopeCloser, 'NoLineAfterClose');
203            }
204        }
205
206    }//end process()
207
208
209}//end class
210
211?>