LogicalOperatorAndSniff   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 12.77 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 6
loc 47
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
A process() 6 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * CodeIgniter_Sniffs_Operators_LogicalOperatorAndSniff.
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_LogicalOperatorAndSniff.
17
 *
18
 * Ensures that the logical operator 'AND' is in upper case and suggest the use of its symbolic equivalent.
19
 *
20
 * @category  PHP
21
 * @package   PHP_CodeSniffer
22
 * @author    Thomas Ernest <[email protected]>
23
 * @copyright 2006 Thomas Ernest
24
 * @license   http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License
25
 * @link      http://pear.php.net/package/PHP_CodeSniffer
26
 */
27
28
namespace CodeIgniter\Sniffs\Operators;
29
30
use PHP_CodeSniffer\Sniffs\Sniff;
31
use PHP_CodeSniffer\Files\File;
32
33
class LogicalOperatorAndSniff implements Sniff
34
{
35
	/**
36
     * Returns an array of tokens this test wants to listen for: symbolic and literal operators and.
37
     *
38
     * @return array
39
     */
40
    public function register()
41
    {
42
        return array(
43
            T_LOGICAL_AND,
44
        );
45
46
    }//end register()
47
48
49
    /**
50
     * Processes this test, when one of its tokens is encountered.
51
     *
52
     * @param File $phpcsFile The current file being scanned.
53
     * @param int                  $stackPtr  The position of the current token
54
     *                                        in the stack passed in $tokens.
55
     *
56
     * @return void
57
     */
58
    public function process(File $phpcsFile, $stackPtr)
59
    {
60
        $tokens = $phpcsFile->getTokens();
61
62
        $operator_token = $tokens[$stackPtr];
63
        $operator_string = $operator_token['content'];
64
        $operator_code = $operator_token['code'];
0 ignored issues
show
Unused Code introduced by
$operator_code is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
65
66 View Code Duplication
        if ($operator_string !== strtoupper($operator_string)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $error_message = 'Logical operator should be in upper case;'
68
                . ' use "' . strtoupper($operator_string)
69
                . '" instead of "' . $operator_string . '"';
70
            $phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator');
71
        }
72
73
        $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"';
74
        $phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator');
75
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...