Completed
Push — master ( bf8a98...570156 )
by Alexander
03:11
created

CodingStandard_Sniffs_Array_ArraySniff::process()   F

Complexity

Conditions 21
Paths 114

Size

Total Lines 110

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 76
CRAP Score 21

Importance

Changes 0
Metric Value
cc 21
nc 114
nop 2
dl 0
loc 110
rs 3.24
c 0
b 0
f 0
ccs 76
cts 76
cp 1
crap 21

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 1
    public function register()
38
    {
39 1
        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 1
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
    {
55 1
        $tokens     = $phpcsFile->getTokens();
56 1
        $arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
57 1
        $arrayEnd   = $tokens[$arrayStart]['parenthesis_closer'];
58
59 1
        if ($arrayStart !== ($stackPtr + 1)) {
60 1
            $error = 'There must be no space between the Array keyword and the opening parenthesis';
61 1
            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword');
62 1
            if ($fix === true) {
63 1
                $phpcsFile->fixer->beginChangeset();
64
65 1
                for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) {
66 1
                    $phpcsFile->fixer->replaceToken($i, '');
67 1
                }
68
69 1
                $phpcsFile->fixer->endChangeset();
70 1
            }
71 1
        }
72
73
        // Check for empty arrays.
74 1
        $content = $phpcsFile->findNext(array(T_WHITESPACE), ($arrayStart + 1), ($arrayEnd + 1), true);
75 1
        if ($content === $arrayEnd) {
76
            // Empty array, but if the brackets aren't together, there's a problem.
77 1
            if (($arrayEnd - $arrayStart) !== 1) {
78 1
                $error = 'Empty array declaration must have no space between the parentheses';
79 1
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceInEmptyArray');
80 1
                if ($fix === true) {
81 1
                    $phpcsFile->fixer->beginChangeset();
82
83 1
                    for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) {
84 1
                        $phpcsFile->fixer->replaceToken($i, '');
85 1
                    }
86
87 1
                    $phpcsFile->fixer->endChangeset();
88 1
                }
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 1
                return;
93
            }
94 1
        }
95
96 1
        $lastItem = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($arrayEnd - 1), $stackPtr, true);
97
98
        // Empty array.
99 1
        if ($lastItem === $arrayStart) {
100 1
            return;
101
        }
102
103
        // Inline array.
104 1
        $isInlineArray = $tokens[$arrayStart]['line'] === $tokens[$arrayEnd]['line'];
105
106
        // Check if the last item in a multiline array has a "closing" comma.
107 1
        if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false) {
108 1
            $error = 'A comma should follow the last multiline array item. Found: '.$tokens[$lastItem]['content'];
109 1
            $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 1
            if ($fix === true) {
111 1
                $phpcsFile->fixer->beginChangeset();
112 1
                $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 1
                $phpcsFile->fixer->endChangeset();
114 1
            }
115
116 1
            return;
117
        }
118
119 1
        if ($isInlineArray === true) {
120 1
            if ($tokens[$lastItem]['code'] === T_COMMA) {
121 1
                $error = 'Comma not allowed after last value in single-line array declaration';
122 1
                $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 1
                if ($fix === true) {
124 1
                    $phpcsFile->fixer->beginChangeset();
125 1
                    $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 1
                    $phpcsFile->fixer->endChangeset();
127 1
                }
128
129 1
                return;
130
            }
131
132
            // Inline array must not have spaces within parenthesis.
133 1
            if ($content !== ($arrayStart + 1)) {
134 1
                $error = 'Space found after opening parenthesis of Array';
135 1
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen');
136 1
                if ($fix === true) {
137 1
                    $phpcsFile->fixer->beginChangeset();
138
139 1
                    for ($i = ($arrayStart + 1); $i < $content; $i++) {
140 1
                        $phpcsFile->fixer->replaceToken($i, '');
141 1
                    }
142
143 1
                    $phpcsFile->fixer->endChangeset();
144 1
                }
145 1
            }
146
147 1
            if ($lastItem !== ($arrayEnd - 1)) {
148 1
                $error = 'Space found before closing parenthesis of Array';
149 1
                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose');
150 1
                if ($fix === true) {
151 1
                    $phpcsFile->fixer->beginChangeset();
152
153 1
                    for ($i = ($lastItem + 1); $i < $arrayEnd; $i++) {
154 1
                        $phpcsFile->fixer->replaceToken($i, '');
155 1
                    }
156
157 1
                    $phpcsFile->fixer->endChangeset();
158 1
                }
159 1
            }
160 1
        }//end if
161
162 1
    }//end process()
163
164
165
}//end class
166