Failed Conditions
Push — master ( c35fd9...2f93af )
by Alexander
02:57
created

ItemAssignmentSniff::hasOnlySpaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Formatting_ItemAssignmentSniff.
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_ItemAssignmentSniff.
22
 *
23
 * Checks if the item assignment operator (=>) has
24
 * - a space before and after
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 ItemAssignmentSniff implements Sniff
34
{
35
36
37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return integer[]
41
     */
42 1
    public function register()
43
    {
44 1
        return array(T_DOUBLE_ARROW);
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 1
        $this->checkSpacing($phpcsFile, $stackPtr, true);
60 1
        $this->checkSpacing($phpcsFile, $stackPtr, false);
61 1
    }//end process()
62
63
64
    /**
65
     * Checks spacing at given position.
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    Determines direction in which to check spacing.
71
     *
72
     * @return void
73
     */
74 1
    protected function checkSpacing(File $phpcsFile, $stackPtr, $before)
75
    {
76 1
        if ($before === true) {
77 1
            $stackPtrDiff = -1;
78 1
            $errorWord    = 'prefix';
79 1
            $errorCode    = 'Before';
80
        } else {
81 1
            $stackPtrDiff = 1;
82 1
            $errorWord    = 'follow';
83 1
            $errorCode    = 'After';
84
        }
85
86 1
        $tokens    = $phpcsFile->getTokens();
87 1
        $tokenData = $tokens[($stackPtr + $stackPtrDiff)];
88
89 1
        if ($tokenData['code'] !== T_WHITESPACE) {
90 1
            $error = 'Whitespace must '.$errorWord.' the item assignment operator =>';
91 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpacing'.$errorCode);
92 1
            if ($fix === true) {
93 1
                $phpcsFile->fixer->beginChangeset();
94
95 1
                if ($before === true) {
96 1
                    $phpcsFile->fixer->addContentBefore($stackPtr, ' ');
97
                } else {
98 1
                    $phpcsFile->fixer->addContent($stackPtr, ' ');
99
                }
100
101 1
                $phpcsFile->fixer->endChangeset();
102
            }
103
104 1
            return;
105
        }
106
107 1
        if (isset($tokenData['orig_content']) === true) {
108
            $content = $tokenData['orig_content'];
109
        } else {
110 1
            $content = $tokenData['content'];
111
        }
112
113 1
        if ($this->hasOnlySpaces($content) === false) {
114 1
            $error = 'Spaces must be used to '.$errorWord.' the item assignment operator =>';
115 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'MixedWhitespace'.$errorCode);
116 1
            if ($fix === true) {
117 1
                $phpcsFile->fixer->beginChangeset();
118 1
                $phpcsFile->fixer->replaceToken(
119 1
                    ($stackPtr + $stackPtrDiff),
120 1
                    str_repeat(' ', strlen($content))
121
                );
122 1
                $phpcsFile->fixer->endChangeset();
123
            }
124
        }
125 1
    }//end checkSpacing()
126
127
128
    /**
129
     * Detects, that string contains only spaces.
130
     *
131
     * @param string $string String.
132
     *
133
     * @return bool
134
     */
135 1
    protected function hasOnlySpaces($string)
136
    {
137 1
        return substr_count($string, ' ') === strlen($string);
138
    }//end hasOnlySpaces()
139
}//end class
140