Failed Conditions
Push — master ( a46fbb...c35fd9 )
by Alexander
01:13
created

ObjectOperatorSpacingSniff::process()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
nc 9
nop 2
dl 0
loc 24
ccs 21
cts 21
cp 1
crap 7
rs 8.6026
c 0
b 0
f 0
1
<?php
2
/**
3
 * CodingStandard_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Greg Sherwood <[email protected]>
10
 * @author   Marc McIntyre <[email protected]>
11
 * @author   Alexander Obuhovich <[email protected]>
12
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
13
 * @link     https://github.com/aik099/CodingStandard
14
 */
15
16
namespace CodingStandard\Sniffs\WhiteSpace;
17
18
use PHP_CodeSniffer\Files\File;
19
use PHP_CodeSniffer\Sniffs\Sniff;
20
21
/**
22
 * CodingStandard_Sniffs_WhiteSpace_ObjectOperatorSpacingSniff.
23
 *
24
 * Ensure there is no whitespace before/after a object operator.
25
 *
26
 * @category PHP
27
 * @package  PHP_CodeSniffer
28
 * @author   Greg Sherwood <[email protected]>
29
 * @author   Marc McIntyre <[email protected]>
30
 * @author   Alexander Obuhovich <[email protected]>
31
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
32
 * @link     https://github.com/aik099/CodingStandard
33
 */
34
class ObjectOperatorSpacingSniff implements Sniff
35
{
36
37
38
    /**
39
     * Returns an array of tokens this test wants to listen for.
40
     *
41
     * @return array
42
     */
43 1
    public function register()
44
    {
45 1
        return array(T_OBJECT_OPERATOR);
46
    }//end register()
47
48
49
    /**
50
     * Processes this test, when one of its tokens is encountered.
51
     *
52
     * @param File $phpcsFile The file being scanned.
53
     * @param int  $stackPtr  The position of the current token in the
54
     *                        stack passed in $tokens.
55
     *
56
     * @return void
57
     */
58 1
    public function process(File $phpcsFile, $stackPtr)
59
    {
60 1
        $tokens = $phpcsFile->getTokens();
61
62 1
        if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE
63 1
            && $tokens[($stackPtr - 2)]['line'] === $tokens[$stackPtr]['line']
64 1
        ) {
65 1
            $error = 'Space found before object operator';
66 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'Before');
67 1
            if ($fix === true) {
68 1
                $phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
69 1
            }
70 1
        }
71
72 1
        if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
73 1
            && $tokens[($stackPtr + 2)]['line'] === $tokens[$stackPtr]['line']
74 1
        ) {
75 1
            $error = 'Space found after object operator';
76 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'After');
77 1
            if ($fix === true) {
78 1
                $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
79 1
            }
80 1
        }
81 1
    }//end process()
82
}//end class
83