Completed
Pull Request — master (#663)
by Juliette
07:37 queued 05:21
created

NonCryptoHashSniff::processParameters()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 4
nop 4
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\FunctionParameters\NonCryptoHashSniff.
4
 *
5
 * PHP version 7.2
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\FunctionParameters;
13
14
use PHPCompatibility\AbstractFunctionCallParameterSniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\FunctionParameters\NonCryptoHashSniff.
18
 *
19
 * Detect: "The hash_hmac(), hash_hmac_file(), hash_pbkdf2(), and hash_init()
20
 * (with HASH_HMAC) functions no longer accept non-cryptographic hashes."
21
 *
22
 * PHP version 7.2
23
 *
24
 * @category PHP
25
 * @package  PHPCompatibility
26
 * @author   Juliette Reinders Folmer <[email protected]>
27
 */
28
class NonCryptoHashSniff extends AbstractFunctionCallParameterSniff
29
{
30
31
    /**
32
     * Functions to check for.
33
     *
34
     * @var array
35
     */
36
    protected $targetFunctions = array(
37
        'hash_hmac'      => true,
38
        'hash_hmac_file' => true,
39
        'hash_init'      => true,
40
        'hash_pbkdf2'    => true,
41
    );
42
43
    /**
44
     * List of the non-cryptographic hashes.
45
     *
46
     * @var array
47
     */
48
    protected $disabledCryptos = array(
49
        'adler32' => true,
50
        'crc32'   => true,
51
        'crc32b'  => true,
52
        'fnv132'  => true,
53
        'fnv1a32' => true,
54
        'fnv164'  => true,
55
        'fnv1a64' => true,
56
        'joaat'   => true,
57
    );
58
59
60
    /**
61
     * Do a version check to determine if this sniff needs to run at all.
62
     *
63
     * @return bool
64
     */
65
    protected function bowOutEarly()
66
    {
67
        return ($this->supportsAbove('7.2') === false);
68
    }
69
70
71
    /**
72
     * Process the parameters of a matched function.
73
     *
74
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
75
     * @param int                   $stackPtr     The position of the current token in the stack.
76
     * @param string                $functionName The token content (function name) which was matched.
77
     * @param array                 $parameters   Array with information about the parameters.
78
     *
79
     * @return int|void Integer stack pointer to skip forward or void to continue
80
     *                  normal file processing.
81
     */
82
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
83
    {
84
        if (isset($parameters[1]) === false) {
85
            return;
86
        }
87
88
        $targetParam = $parameters[1];
89
90
        if (isset($this->disabledCryptos[$this->stripQuotes($targetParam['raw'])]) === false) {
91
            return;
92
        }
93
94
        if (strtolower($functionName) === 'hash_init'
95
            && (isset($parameters[2]) === false
96
            || ($parameters[2]['raw'] !== 'HASH_HMAC'
97
                && $parameters[2]['raw'] !== (string) HASH_HMAC))
98
        ) {
99
            // For hash_init(), these hashes are only disabled with HASH_HMAC set.
100
            return;
101
        }
102
103
        $phpcsFile->addError(
104
            'Non-cryptographic hashes are no longer accepted by function %s() since PHP 7.2. Found: %s',
105
            $targetParam['start'],
106
            $this->stringToErrorCode($functionName),
107
            array(
108
                $functionName,
109
                $targetParam['raw'],
110
            )
111
        );
112
    }
113
}//end class
114