StrictComparisonOperatorSniff   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A process() 0 13 1
1
<?php
2
/**
3
 * CodeIgniter_Sniffs_Operators_StrictComparisonOperatorSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Thomas Ernest <[email protected]>
10
 * @copyright 2006 Thomas Ernest
11
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
13
 */
14
15
/**
16
 * CodeIgniter_Sniffs_Operators_StrictComparisonOperatorSniff.
17
 *
18
 * Ensures that only strict comparison operators are used instead of
19
 * equal and not equal operators.
20
 *
21
 * @category  PHP
22
 * @package   PHP_CodeSniffer
23
 * @author    Thomas Ernest <[email protected]>
24
 * @copyright 2006 Thomas Ernest
25
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
26
 * @link      http://pear.php.net/package/PHP_CodeSniffer
27
 */
28
29
namespace CodeIgniter\Sniffs\Operators;
30
31
use PHP_CodeSniffer\Sniffs\Sniff;
32
use PHP_CodeSniffer\Files\File;
33
34
class StrictComparisonOperatorSniff implements Sniff
35
{
36
    private static $_replacements = array(
37
        T_IS_EQUAL     => '===',
38
        T_IS_NOT_EQUAL => '!==',
39
    );
40
41
    /**
42
     * Returns an array of tokens this test wants to listen for.
43
     *
44
     * @return array
45
     */
46
    public function register()
47
    {
48
        return array(
49
            T_IS_EQUAL,
50
            T_IS_NOT_EQUAL,
51
        );
52
    }//end register()
53
54
55
    /**
56
     * Processes this test, when one of its tokens is encountered.
57
     *
58
     * @param File $phpcsFile The current file being scanned.
59
     * @param int                  $stackPtr  The position of the current token
60
     *                                        in the stack passed in $tokens.
61
     *
62
     * @return void
63
     */
64
    public function process(File $phpcsFile, $stackPtr)
65
    {
66
        $tokens = $phpcsFile->getTokens();
67
68
        $operator_token = $tokens[$stackPtr];
69
        $operator_string = $operator_token['content'];
70
        $operator_code = $operator_token['code'];
71
72
        $error_message = '"==" and "!=" are prohibited; use "'
73
            . self::$_replacements[$operator_code] . '" instead of "'
74
            . $operator_string . '".';
75
        $phpcsFile->addError($error_message, $stackPtr, 'NonStrictComparisonUsed');
76
    }//end process()
77
78
79
}//end class
80
81
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...