Completed
Branch master (205409)
by Timothy
02:36
created

LogicalNotSpacingSniff::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * CodeIgniter_Sniffs_WhiteSpace_LogicalNotSpacingSniff.
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_WhiteSpace_LogicalNotSpacingSniff.
17
 *
18
 * Ensures that at exactly a space precedes and follows the logical operator !.
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\WhiteSpace;
29
30
use PHP_CodeSniffer\Sniffs\Sniff;
31
use PHP_CodeSniffer\Files\File;
32
33
class LogicalNotSpacingSniff implements Sniff
34
{
35
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(
44
            T_BOOLEAN_NOT,
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];
0 ignored issues
show
Unused Code introduced by
$operator_token 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...
63
64
        $previous_token = $tokens[$stackPtr - 1];
65
        $next_token = $tokens[$stackPtr + 1];
66
        if (T_WHITESPACE !== $previous_token['code'] || T_WHITESPACE !== $next_token['code']) {
67
            $error = 'Logical operator ! should always be preceded and followed with a whitespace.';
68
            $phpcsFile->addError($error, $stackPtr);
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $code.

This check looks for function calls that miss required arguments.

Loading history...
69
        }
70
    }//end process()
71
72
73
}//end class
74
75
?>
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...