Completed
Push — argument_functions_report_curr... ( ad95d7 )
by Wim
01:44
created

process()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 6.7272
cc 7
eloc 23
nc 7
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\ArgumentFunctionsReportCurrentValue.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Wim Godden <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\ArgumentFunctionsReportCurrentValue.
18
 *
19
 * Functions inspecting function arguments report the current value instead of the original since PHP 7.0.
20
 *
21
 * PHP version 7.0
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Wim Godden <[email protected]>
26
 */
27
class ArgumentFunctionsReportCurrentValueSniff extends Sniff
28
{
29
    /**
30
     * A list of functions that, when called, have a different behaviour in PHP 7 when dealing with parameters of the function they're called in.
31
     * @var array(string)
32
     */
33
    
34
    protected $changedFunctions = array(
35
        'func_get_arg',
36
        'func_get_args',
37
        'debug_backtrace'
38
    );
39
    
40
    /**
41
     * Returns an array of tokens this test wants to listen for.
42
     *
43
     * @return array
44
     */
45
    public function register()
46
    {
47
        return array(T_STRING);
48
49
    }//end register()
50
51
    /**
52
     * Processes this test, when one of its tokens is encountered.
53
     *
54
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
55
     * @param int                   $stackPtr  The position of the current token
56
     *                                         in the stack passed in $tokens.
57
     *
58
     * @return void
59
     */
60
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61
    {
62
        if ($this->supportsAbove('7.0') === false) {
63
            return;
64
        }
65
        
66
        $tokens = $phpcsFile->getTokens();
67
        
68
        $ignore = array(
69
            T_DOUBLE_COLON,
70
            T_OBJECT_OPERATOR,
71
            T_FUNCTION,
72
            T_CLASS,
73
            T_CONST,
74
            T_USE,
75
            T_NS_SEPARATOR,
76
        );
77
        
78
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
79
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
80
            // Not a call to a PHP function.
81
            return;
82
        }
83
        
84
        $function   = $tokens[$stackPtr]['content'];
85
        $functionLc = strtolower($function);
86
87
        if (in_array($functionLc, $this->changedFunctions) === false) {
88
            return;
89
        }
90
91
        if (isset($tokens[$stackPtr]['conditions'])) {
92
            foreach ($tokens[$stackPtr]['conditions'] as $condition => $notimportant) {
93
                if ($tokens[$condition]['type'] == 'T_FUNCTION') {
94
                    $this->addMessage($phpcsFile, 'Functions inspecting arguments report the current parameter value Function since PHP 7.0. Verify if the value is changed somewhere.', $stackPtr, false);
95
                }
96
            }
97
        }
98
    }//end process()
99
100
101
}//end class
102