Completed
Push — master ( ce1586...b79486 )
by Alexander
02:03
created

CodingStandard_Sniffs_Array_ArraySniff   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 138
rs 10
c 0
b 0
f 0
ccs 0
cts 85
cp 0
wmc 22
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
D process() 0 110 21
1
<?php
2
/**
3
 * CodingStandard_Sniffs_Array_ArraySniff.
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
/**
16
 * CodingStandard_Sniffs_Array_ArraySniff.
17
 *
18
 * Checks if the array's are styled in the Drupal way.
19
 * - Comma after the last array element
20
 *
21
 * @category PHP
22
 * @package  PHP_CodeSniffer
23
 * @author   Peter Philipp <[email protected]>
24
 * @author   Alexander Obuhovich <[email protected]>
25
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
26
 * @link     https://github.com/aik099/CodingStandard
27
 */
28
class CodingStandard_Sniffs_Array_ArraySniff implements PHP_CodeSniffer_Sniff
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
29
{
30
31
32
    /**
33
     * Returns an array of tokens this test wants to listen for.
34
     *
35
     * @return integer[]
36
     */
37
    public function register()
38
    {
39
        return array(T_ARRAY);
40
41
    }//end register()
42
43
44
    /**
45
     * Processes this test, when one of its tokens is encountered.
46
     *
47
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
     * @param int                  $stackPtr  The position of the current token in the
49
     *                                        stack passed in $tokens.
50
     *
51
     * @return void
52
     */
53
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55
        $tokens     = $phpcsFile->getTokens();
56
        $arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
57
        $arrayEnd   = $tokens[$arrayStart]['parenthesis_closer'];
58
59
        if ($arrayStart !== ($stackPtr + 1)) {
60
            $error = 'There must be no space between the Array keyword and the opening parenthesis';
61
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword');
62
            if ($fix === true) {
63
                $phpcsFile->fixer->beginChangeset();
64
65
                for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) {
66
                    $phpcsFile->fixer->replaceToken($i, '');
67
                }
68
69
                $phpcsFile->fixer->endChangeset();
70
            }
71
        }
72
73
        // Check for empty arrays.
74
        $content = $phpcsFile->findNext(array(T_WHITESPACE), ($arrayStart + 1), ($arrayEnd + 1), true);
75
        if ($content === $arrayEnd) {
76
            // Empty array, but if the brackets aren't together, there's a problem.
77
            if (($arrayEnd - $arrayStart) !== 1) {
78
                $error = 'Empty array declaration must have no space between the parentheses';
79
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray');
80
                if ($fix === true) {
81
                    $phpcsFile->fixer->beginChangeset();
82
83
                    for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) {
84
                        $phpcsFile->fixer->replaceToken($i, '');
85
                    }
86
87
                    $phpcsFile->fixer->endChangeset();
88
                }
89
90
                // We can return here because there is nothing else to check. All code
91
                // below can assume that the array is not empty.
92
                return;
93
            }
94
        }
95
96
        $lastItem = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($arrayEnd - 1), $stackPtr, true);
97
98
        // Empty array.
99
        if ($lastItem === $arrayStart) {
100
            return;
101
        }
102
103
        // Inline array.
104
        $isInlineArray = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line'];
105
106
        // Check if the last item in a multiline array has a "closing" comma.
107
        if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false) {
108
            $error = 'A comma should follow the last multiline array item. Found: '.$tokens[$lastItem]['content'];
109
            $fix   = $phpcsFile->addFixableWarning($error, $lastItem, 'NoLastComma');
0 ignored issues
show
Bug introduced by
It seems like $lastItem defined by $phpcsFile->findPrevious...d - 1, $stackPtr, true) on line 96 can also be of type boolean; however, PHP_CodeSniffer_File::addFixableWarning() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110
            if ($fix === true) {
111
                $phpcsFile->fixer->beginChangeset();
112
                $phpcsFile->fixer->addContent($lastItem, ',');
0 ignored issues
show
Bug introduced by
It seems like $lastItem defined by $phpcsFile->findPrevious...d - 1, $stackPtr, true) on line 96 can also be of type boolean; however, PHP_CodeSniffer_Fixer::addContent() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113
                $phpcsFile->fixer->endChangeset();
114
            }
115
116
            return;
117
        }
118
119
        if ($isInlineArray === true) {
120
            if ($tokens[$lastItem]['code'] === T_COMMA) {
121
                $error = 'Comma not allowed after last value in single-line array declaration';
122
                $fix   = $phpcsFile->addFixableWarning($error, $lastItem, 'LastComma');
0 ignored issues
show
Bug introduced by
It seems like $lastItem defined by $phpcsFile->findPrevious...d - 1, $stackPtr, true) on line 96 can also be of type boolean; however, PHP_CodeSniffer_File::addFixableWarning() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
                if ($fix === true) {
124
                    $phpcsFile->fixer->beginChangeset();
125
                    $phpcsFile->fixer->replaceToken($lastItem, '');
0 ignored issues
show
Bug introduced by
It seems like $lastItem defined by $phpcsFile->findPrevious...d - 1, $stackPtr, true) on line 96 can also be of type boolean; however, PHP_CodeSniffer_Fixer::replaceToken() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
126
                    $phpcsFile->fixer->endChangeset();
127
                }
128
129
                return;
130
            }
131
132
            // Inline array must not have spaces within parenthesis.
133
            if ($content !== ($arrayStart + 1)) {
134
                $error = 'Space found after opening parenthesis of Array';
135
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen');
136
                if ($fix === true) {
137
                    $phpcsFile->fixer->beginChangeset();
138
139
                    for ($i = ($arrayStart + 1); $i < $content; $i++) {
140
                        $phpcsFile->fixer->replaceToken($i, '');
141
                    }
142
143
                    $phpcsFile->fixer->endChangeset();
144
                }
145
            }
146
147
            if ($lastItem !== ($arrayEnd - 1)) {
148
                $error = 'Space found before closing parenthesis of Array';
149
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose');
150
                if ($fix === true) {
151
                    $phpcsFile->fixer->beginChangeset();
152
153
                    for ($i = ($lastItem + 1); $i < $arrayEnd; $i++) {
154
                        $phpcsFile->fixer->replaceToken($i, '');
155
                    }
156
157
                    $phpcsFile->fixer->endChangeset();
158
                }
159
            }
160
        }//end if
161
162
    }//end process()
163
164
165
}//end class
166