Completed
Push — in-portal ( 542489...3b6de0 )
by Alexander
04:02
created

SpaceOperatorSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Formatting_SpaceOperatorSniff.
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
use PHP_CodeSniffer\Util\Tokens;
20
21
/**
22
 * CodingStandard_Sniffs_Formatting_SpaceOperatorSniff.
23
 *
24
 * Ensures there is a single space after a operator
25
 *
26
 * @category PHP
27
 * @package  PHP_CodeSniffer
28
 * @author   Peter Philipp <[email protected]>
29
 * @author   Alexander Obuhovich <[email protected]>
30
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
31
 * @link     https://github.com/aik099/CodingStandard
32
 */
33
class SpaceOperatorSniff implements Sniff
34
{
35
36
37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return array
41
     */
42 1
    public function register()
43
    {
44 1
         return Tokens::$assignmentTokens;
45
    }//end register()
46
47
48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @param File $phpcsFile The file being scanned.
52
     * @param int  $stackPtr  The position of the current token in the
53
     *                        stack passed in $tokens.
54
     *
55
     * @return void
56
     */
57 1
    public function process(File $phpcsFile, $stackPtr)
58
    {
59
        // Only cover places, that are not handled by "Squiz.WhiteSpace.OperatorSpacing" sniff.
60 1
        $tokens = $phpcsFile->getTokens();
61
62 1
        if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
63 1
            return;
64
        }
65
66 1
        if ($tokens[($stackPtr - 2)]['line'] !== $tokens[$stackPtr]['line']) {
67 1
            $found = 'newline';
68 1
        } else {
69 1
            $found = $tokens[($stackPtr - 1)]['length'];
70
        }
71
72 1
        if (isset($phpcsFile->fixer) === true) {
73 1
            $phpcsFile->recordMetric($stackPtr, 'Space before operator', $found);
74 1
        }
75
76 1
        if ($found !== 1) {
77 1
            $error = 'Expected 1 space before "%s"; %s found';
78
            $data  = array(
79 1
                      $tokens[$stackPtr]['content'],
80 1
                      $found,
81 1
                     );
82 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data);
83
84 1
            if ($fix === true) {
85 1
                $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
86 1
            }
87 1
        }
88 1
    }//end process()
89
}//end class
90