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

ConcatenationSpacingSniff   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 5 1
B checkContent() 0 62 8
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Strings_ConcatenationSpacingSniff.
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\Strings;
16
17
use PHP_CodeSniffer\Files\File;
18
use PHP_CodeSniffer\Sniffs\Sniff;
19
20
/**
21
 * CodingStandard_Sniffs_Strings_ConcatenationSpacingSniff.
22
 *
23
 * Makes sure there are the needed spaces between the concatenation operator (.) and
24
 * the strings being concatenated.
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 ConcatenationSpacingSniff implements Sniff
34
{
35
36
37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return integer[]
41
     */
42
    public function register()
43
    {
44
        return array(T_STRING_CONCAT);
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
    public function process(File $phpcsFile, $stackPtr)
58
    {
59
        $this->checkContent($phpcsFile, $stackPtr, true);
60
        $this->checkContent($phpcsFile, $stackPtr, false);
61
    }//end process()
62
63
64
    /**
65
     * Checks content before concat operator.
66
     *
67
     * @param File $phpcsFile The file being scanned.
68
     * @param int  $stackPtr  The position of the current token in the
69
     *                        stack passed in $tokens.
70
     * @param bool $before    Check content before concat operator.
71
     *
72
     * @return void
73
     */
74
    protected function checkContent(File $phpcsFile, $stackPtr, $before)
75
    {
76
        if ($before === true) {
77
            $contentToken = ($phpcsFile->findPrevious(
78
                T_WHITESPACE,
79
                ($stackPtr - 1),
80
                null,
81
                true
82
            ) + 1);
83
            $errorWord    = 'before';
84
        } else {
85
            $contentToken = ($phpcsFile->findNext(
86
                T_WHITESPACE,
87
                ($stackPtr + 1),
88
                null,
89
                true
90
            ) - 1);
91
            $errorWord    = 'after';
92
        }
93
94
        $tokens      = $phpcsFile->getTokens();
95
        $contentData = $tokens[$contentToken];
96
97
        if ($contentData['line'] !== $tokens[$stackPtr]['line']) {
98
            // Ignore concat operator split across several lines.
99
            return;
100
        }
101
102
        if ($contentData['code'] !== T_WHITESPACE) {
103
            $fix = $phpcsFile->addFixableError(
104
                'Expected 1 space '.$errorWord.' concat operator; 0 found',
105
                $stackPtr,
106
                'NoSpace'.ucfirst($errorWord)
107
            );
108
109
            if ($fix === true) {
110
                $phpcsFile->fixer->beginChangeset();
111
112
                if ($before === true) {
113
                    $phpcsFile->fixer->addContentBefore($stackPtr, ' ');
114
                } else {
115
                    $phpcsFile->fixer->addContent($stackPtr, ' ');
116
                }
117
118
                $phpcsFile->fixer->endChangeset();
119
            }
120
        } elseif ($contentData['length'] !== 1) {
121
            $data = array($contentData['length']);
122
            $fix  = $phpcsFile->addFixableError(
123
                'Expected 1 space '.$errorWord.' concat operator; %s found',
124
                $stackPtr,
125
                'SpaceBefore'.ucfirst($errorWord),
126
                $data
127
            );
128
129
            if ($fix === true) {
130
                $phpcsFile->fixer->beginChangeset();
131
                $phpcsFile->fixer->replaceToken($contentToken, ' ');
132
                $phpcsFile->fixer->endChangeset();
133
            }
134
        }
135
    }//end checkContent()
136
}//end class
137