Failed Conditions
Push — phpcs-3-upgrade ( d91341 )
by Alexander
02:06
created

SpaceUnaryOperatorSniff   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 7 1
B process() 0 28 8
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Formatting_SpaceUnaryOperatorSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Peter Philipp <[email protected]>
10
 * @author   Alexander Obuhovich <[email protected]>
11
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
12
 * @link     https://github.com/aik099/CodingStandard
13
 */
14
15
namespace CodingStandard\Sniffs\Formatting;
16
17
use PHP_CodeSniffer\Files\File;
18
use PHP_CodeSniffer\Sniffs\Sniff;
19
20
/**
21
 * CodingStandard_Sniffs_Formatting_SpaceUnaryOperatorSniff.
22
 *
23
 * Ensures there are no spaces on increment / decrement statements.
24
 *
25
 * @category PHP
26
 * @package  PHP_CodeSniffer
27
 * @author   Peter Philipp <[email protected]>
28
 * @author   Alexander Obuhovich <[email protected]>
29
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
30
 * @link     https://github.com/aik099/CodingStandard
31
 */
32
class SpaceUnaryOperatorSniff implements Sniff
33
{
34
35
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return integer[]
40
     */
41
    public function register()
42
    {
43
         return array(
44
                 T_DEC,
45
                 T_INC,
46
                );
47
    }//end register()
48
49
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param File $phpcsFile The file being scanned.
54
     * @param int  $stackPtr  The position of the current token in the
55
     *                        stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(File $phpcsFile, $stackPtr)
60
    {
61
        $tokens     = $phpcsFile->getTokens();
62
        $modifyLeft = substr($tokens[($stackPtr - 1)]['content'], 0, 1) === '$'
63
            || $tokens[($stackPtr + 1)]['content'] === ';';
64
65
        if ($modifyLeft === true && $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
66
            $error = 'There must not be a single space before an unary operator statement';
67
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ExtraSpaceBefore');
68
69
            if ($fix === true) {
70
                $phpcsFile->fixer->beginChangeset();
71
                $phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
72
                $phpcsFile->fixer->endChangeset();
73
            }
74
        }
75
76
        if ($modifyLeft === false && substr($tokens[($stackPtr + 1)]['content'], 0, 1) !== '$') {
77
            $error = 'A unary operator statement must not followed by a single space';
78
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ExtraSpaceAfter');
79
80
            if ($fix === true) {
81
                $phpcsFile->fixer->beginChangeset();
82
                $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
83
                $phpcsFile->fixer->endChangeset();
84
            }
85
        }
86
    }//end process()
87
}//end class
88