PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/Operators/ValidLogicalOperatorsSniff.php

https://gitlab.com/trang1104/portable_project
PHP | 87 lines | 32 code | 13 blank | 42 comment | 1 complexity | 1bdd5d5dcc354b1bee5952da7dc8c811 MD5 | raw file
 1<?php
 2/**
 3 * Squiz_Sniffs_Operators_ValidLogicalOperatorsSniff.
 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_Operators_ValidLogicalOperatorsSniff.
18 *
19 * Checks to ensure that the logical operators 'and' and 'or' are not used.
20 * Use the && and || operators instead.
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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
27 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28 * @version   Release: 1.3.3
29 * @link      http://pear.php.net/package/PHP_CodeSniffer
30 */
31class Squiz_Sniffs_Operators_ValidLogicalOperatorsSniff implements PHP_CodeSniffer_Sniff
32{
33
34
35    /**
36     * Returns an array of tokens this test wants to listen for.
37     *
38     * @return array
39     */
40    public function register()
41    {
42        return array(
43                T_LOGICAL_AND,
44                T_LOGICAL_OR,
45                T_LOGICAL_XOR,
46               );
47
48    }//end register()
49
50
51    /**
52     * Processes this test, when one of its tokens is encountered.
53     *
54     * @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
55     * @param int                  $stackPtr  The position of the current token in the
56     *                                        stack passed in $tokens.
57     *
58     * @return void
59     */
60    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61    {
62        $tokens = $phpcsFile->getTokens();
63
64        $replacements = array(
65                         'and' => '&&',
66                         'or'  => '||',
67                         'xor' => '^',
68                        );
69
70        $operator = strtolower($tokens[$stackPtr]['content']);
71        if (isset($replacements[$operator]) === false) {
72            return;
73        }
74
75        $error = 'Logical operator "%s" is prohibited; use "%s" instead';
76        $data  = array(
77                  $operator,
78                  $replacements[$operator],
79                 );
80        $phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);
81
82    }//end process()
83
84
85}//end class
86
87?>