Completed
Push — master ( 3bacb7...a63167 )
by Wim
02:56 queued 01:08
created

NewPasswordAlgoConstantValuesSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 95
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bowOutEarly() 0 4 1
A processParameters() 0 30 5
1
<?php
2
/**
3
 * PHPCompatibility, an external standard for PHP_CodeSniffer.
4
 *
5
 * @package   PHPCompatibility
6
 * @copyright 2012-2019 PHPCompatibility Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCompatibility/PHPCompatibility
9
 */
10
11
namespace PHPCompatibility\Sniffs\ParameterValues;
12
13
use PHPCompatibility\AbstractFunctionCallParameterSniff;
14
use PHP_CodeSniffer_File as File;
15
use PHP_CodeSniffer_Tokens as Tokens;
16
17
/**
18
 * The constant value of the password hash algorithm constants has changed in PHP 7.4.
19
 *
20
 * Applications correctly using the constants PASSWORD_DEFAULT, PASSWORD_BCRYPT,
21
 * PASSWORD_ARGON2I, and PASSWORD_ARGON2ID will continue to function correctly.
22
 * Using an int will still work, but will produce a deprecation warning.
23
 *
24
 * PHP version 7.4
25
 *
26
 * @link https://wiki.php.net/rfc/password_registry
27
 *
28
 * @since 9.3.0
29
 */
30
class NewPasswordAlgoConstantValuesSniff extends AbstractFunctionCallParameterSniff
31
{
32
33
    /**
34
     * Functions to check for.
35
     *
36
     * Key is the function name, value the 1-based parameter position of
37
     * the $encoding parameter.
38
     *
39
     * @since 9.3.0
40
     *
41
     * @var array
42
     */
43
    protected $targetFunctions = array(
44
        'password_hash'         => 2,
45
        'password_needs_rehash' => 2,
46
    );
47
48
    /**
49
     * Tokens types which indicate that the parameter passed is not the PHP native constant.
50
     *
51
     * @since 9.3.0
52
     *
53
     * @var array
54
     */
55
    private $invalidTokenTypes = array(
56
        \T_NULL                     => true,
57
        \T_TRUE                     => true,
58
        \T_FALSE                    => true,
59
        \T_LNUMBER                  => true,
60
        \T_DNUMBER                  => true,
61
        \T_CONSTANT_ENCAPSED_STRING => true,
62
        \T_DOUBLE_QUOTED_STRING     => true,
63
        \T_HEREDOC                  => true,
64
        \T_NOWDOC                   => true,
65
    );
66
67
68
    /**
69
     * Do a version check to determine if this sniff needs to run at all.
70
     *
71
     * @since 9.3.0
72
     *
73
     * @return bool
74
     */
75
    protected function bowOutEarly()
76
    {
77
        return ($this->supportsAbove('7.4') === false);
78
    }
79
80
81
    /**
82
     * Process the parameters of a matched function.
83
     *
84
     * @since 9.3.0
85
     *
86
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
87
     * @param int                   $stackPtr     The position of the current token in the stack.
88
     * @param string                $functionName The token content (function name) which was matched.
89
     * @param array                 $parameters   Array with information about the parameters.
90
     *
91
     * @return int|void Integer stack pointer to skip forward or void to continue
92
     *                  normal file processing.
93
     */
94
    public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
95
    {
96
        $functionLC = strtolower($functionName);
97
        if (isset($parameters[$this->targetFunctions[$functionLC]]) === false) {
98
            return;
99
        }
100
101
        $targetParam = $parameters[$this->targetFunctions[$functionLC]];
102
        $tokens      = $phpcsFile->getTokens();
103
104
        for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
105
            if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
106
                continue;
107
            }
108
109
            if (isset($this->invalidTokenTypes[$tokens[$i]['code']]) === true) {
110
                $phpcsFile->addWarning(
111
                    'The value of the password hash algorithm constants has changed in PHP 7.4. Pass a PHP native constant to the %s() function instead of using the value of the constant. Found: %s',
112
                    $stackPtr,
113
                    'NotAlgoConstant',
114
                    array(
115
                        $functionName,
116
                        $targetParam['raw'],
117
                    )
118
                );
119
120
                break;
121
            }
122
        }
123
    }
124
}
125