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

ObjectOperatorSpacingSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 24 7
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