PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://gitlab.com/trang1104/portable_project
PHP | 116 lines | 48 code | 17 blank | 51 comment | 11 complexity | c899f3ad198b828210cfa5b3d637245d MD5 | raw file
  1<?php
  2/**
  3 * Squiz_Sniffs_WhiteSpace_FunctionClosingBraceSpaceSniff.
  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_FunctionClosingBraceSpaceSniff.
 18 *
 19 * Checks that there is one empty line before the closing brace of a function.
 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_FunctionClosingBraceSpaceSniff 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(T_FUNCTION);
 52
 53    }//end register()
 54
 55
 56    /**
 57     * Processes this test, when one of its tokens is encountered.
 58     *
 59     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 60     * @param int                  $stackPtr  The position of the current token
 61     *                                        in the stack passed in $tokens.
 62     *
 63     * @return void
 64     */
 65    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 66    {
 67        $tokens = $phpcsFile->getTokens();
 68
 69        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
 70            // Probably an interface method.
 71            return;
 72        }
 73
 74        $closeBrace  = $tokens[$stackPtr]['scope_closer'];
 75        $prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
 76
 77        // Special case for empty JS functions
 78        if ($phpcsFile->tokenizerType === 'JS' && $prevContent === $tokens[$stackPtr]['scope_opener']) {
 79            // In this case, the opening and closing brace must be
 80            // right next to each other.
 81            if ($tokens[$stackPtr]['scope_closer'] !== ($tokens[$stackPtr]['scope_opener'] + 1)) {
 82                $error = 'The opening and closing braces of empty functions must be directly next to each other; e.g., function () {}';
 83                $phpcsFile->addError($error, $closeBrace, 'SpacingBetween');
 84            }
 85
 86            return;
 87        }
 88
 89        $braceLine = $tokens[$closeBrace]['line'];
 90        $prevLine  = $tokens[$prevContent]['line'];
 91
 92        $found = ($braceLine - $prevLine - 1);
 93        if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true || isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
 94            // Nested function.
 95            if ($found < 0) {
 96                $error = 'Closing brace of nested function must be on a new line';
 97                $phpcsFile->addError($error, $closeBrace, 'ContentBeforeClose');
 98            } else if ($found > 0) {
 99                $error = 'Expected 0 blank lines before closing brace of nested function; %s found';
100                $data  = array($found);
101                $phpcsFile->addError($error, $closeBrace, 'SpacingBeforeNestedClose', $data);
102            }
103        } else {
104            if ($found !== 1) {
105                $error = 'Expected 1 blank line before closing function brace; %s found';
106                $data  = array($found);
107                $phpcsFile->addError($error, $closeBrace, 'SpacingBeforeClose', $data);
108            }
109        }
110
111    }//end process()
112
113
114}//end class
115
116?>