Completed
Push — master ( 570156...b64a33 )
by Alexander
02:47
created

AssignmentInConditionSniff::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 9.52
c 0
b 0
f 0
1
<?php
2
/**
3
 * CodingStandard_Sniffs_ControlStructures_AssignmentInConditionSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  PHP_CodeSniffer
9
 * @author   Alexander Obuhovich <[email protected]>
10
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
11
 * @link     https://github.com/aik099/CodingStandard
12
 */
13
14
/**
15
 * Checks that assignments in conditions are not used.
16
 *
17
 * @category PHP
18
 * @package  PHP_CodeSniffer
19
 * @author   Alexander Obuhovich <[email protected]>
20
 * @license  https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause
21
 * @link     https://github.com/aik099/CodingStandard
22
 */
23
class CodingStandard_Sniffs_ControlStructures_AssignmentInConditionSniff implements PHP_CodeSniffer_Sniff
24
{
25
    /**
26
     * A list of tokenizers this sniff supports.
27
     *
28
     * @var array
29
     */
30
    public $supportedTokenizers = array('PHP');
31
32
33
    /**
34
     * Returns an array of tokens this test wants to listen for.
35
     *
36
     * @return array
37
     */
38 1
    public function register()
39
    {
40
        return array(
41 1
                T_IF,
42
                T_ELSEIF,
43
               );
44
45
    }//end register()
46
47
48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
52
     * @param int                  $stackPtr  The position of the current token in
53
     *                                        the stack passed in $tokens.
54
     *
55
     * @return void
56
     */
57 1
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
    {
59 1
        $tokens = $phpcsFile->getTokens();
60
61 1
        if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) {
62
            return;
63
        }
64
65 1
        $equalOperator = $phpcsFile->findNext(
66 1
            T_EQUAL,
67 1
            $tokens[$stackPtr]['parenthesis_opener'],
68 1
            $tokens[$stackPtr]['parenthesis_closer']
69
        );
70
71 1
        if ($equalOperator === false) {
72 1
            return;
73
        }
74
75 1
        $phpcsFile->addError(
76 1
            'Assignment in "' . $tokens[$stackPtr]['content'] . '" control structure is forbidden',
77 1
            $equalOperator,
78 1
            'Forbidden'
79
        );
80
81 1
    }//end process()
82
83
84
}//end class
85