Completed
Push — in-portal ( 542489...3b6de0 )
by Alexander
04:02
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 1
    public function register()
42
    {
43
         return array(
44 1
                 T_DEC,
45 1
                 T_INC,
46 1
                );
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 1
    public function process(File $phpcsFile, $stackPtr)
60
    {
61 1
        $tokens     = $phpcsFile->getTokens();
62 1
        $modifyLeft = substr($tokens[($stackPtr - 1)]['content'], 0, 1) === '$'
63 1
            || $tokens[($stackPtr + 1)]['content'] === ';';
64
65 1
        if ($modifyLeft === true && $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
66 1
            $error = 'There must not be a single space before an unary operator statement';
67 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ExtraSpaceBefore');
68
69 1
            if ($fix === true) {
70 1
                $phpcsFile->fixer->beginChangeset();
71 1
                $phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
72 1
                $phpcsFile->fixer->endChangeset();
73 1
            }
74 1
        }
75
76 1
        if ($modifyLeft === false && substr($tokens[($stackPtr + 1)]['content'], 0, 1) !== '$') {
77 1
            $error = 'A unary operator statement must not followed by a single space';
78 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ExtraSpaceAfter');
79
80 1
            if ($fix === true) {
81 1
                $phpcsFile->fixer->beginChangeset();
82 1
                $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
83 1
                $phpcsFile->fixer->endChangeset();
84 1
            }
85 1
        }
86 1
    }//end process()
87
}//end class
88