Completed
Push — master ( 4ecb83...b1425f )
by Wim
03:25 queued 01:05
created

PHPCompatibility_Sniffs_PHP_TernaryOperatorsSniff   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 18 3
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_TernaryOperatorsSniff.
4
 *
5
 * PHP version 5.3
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Ben Selby <[email protected]>
10
 * @copyright 2012 Ben Selby
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_TernaryOperatorsSniff.
15
 *
16
 * Performs checks on ternary operators, specifically that the middle expression
17
 * is not omitted for versions that don't support this.
18
 *
19
 * PHP version 5.3
20
 *
21
 * @category  PHP
22
 * @package   PHPCompatibility
23
 * @author    Ben Selby <[email protected]>
24
 * @copyright 2012 Ben Selby
25
 */
26
class PHPCompatibility_Sniffs_PHP_TernaryOperatorsSniff extends PHPCompatibility_Sniff
27
{
28
29
    /**
30
     * Returns an array of tokens this test wants to listen for.
31
     *
32
     * @return array
33
     */
34
    public function register()
35
    {
36
        return array(T_INLINE_THEN);
37
    }
38
39
    /**
40
     * Processes this test, when one of its tokens is encountered.
41
     *
42
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
43
     * @param int                  $stackPtr  The position of the current token in the
44
     *                                        stack passed in $tokens.
45
     *
46
     * @return void
47
     */
48
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
49
    {
50
        if ($this->supportsAbove('5.3')) {
51
            $tokens = $phpcsFile->getTokens();
52
        
53
            // Get next non-whitespace token, and check it isn't the related inline else
54
            // symbol, which is not allowed prior to PHP 5.3.
55
            $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens,
56
                                         ($stackPtr + 1), null, true);
57
58
            if ($tokens[$next]['code'] == T_INLINE_ELSE) {
59
                $error = sprintf(
60
                    "Middle may not be omitted from ternary operators in PHP < 5.3"
61
                );
62
                $phpcsFile->addWarning($error, $stackPtr);
63
            }
64
        }
65
    }
66
}
67