Completed
Pull Request — master (#240)
by Juliette
02:22
created

RemovedHashAlgorithmsSniff   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 151
Duplicated Lines 3.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 5
loc 151
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 0 37 6
B getErrorInfo() 0 22 6
B addError() 5 25 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_RemovedHashAlgorithmsSniff.
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2012 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_RemovedHashAlgorithmsSniff.
15
 *
16
 * Discourages the use of deprecated and removed hash algorithms.
17
 *
18
 * PHP version 5.4
19
 *
20
 * @category  PHP
21
 * @package   PHPCompatibility
22
 * @author    Wim Godden <[email protected]>
23
 * @copyright 2012 Cu.be Solutions bvba
24
 */
25
class PHPCompatibility_Sniffs_PHP_RemovedHashAlgorithmsSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
26
{
27
28
    /**
29
     * A list of removed hash algorithms, which were present in older versions.
30
     *
31
     * The array lists : version number with false (deprecated) and true (removed).
32
     * If's sufficient to list the first version where the hash algorithm was deprecated/removed.
33
     *
34
     * @var array(string => array(string => bool))
35
     */
36
    protected $removedAlgorithms = array(
37
        'salsa10' => array(
38
            '5.4' => true,
39
        ),
40
        'salsa20' => array(
41
            '5.4' => true,
42
        ),
43
    );
44
45
    /**
46
     * Returns an array of tokens this test wants to listen for.
47
     *
48
     * @return array
49
     */
50
    public function register()
51
    {
52
        return array(T_STRING);
53
54
    }//end register()
55
56
57
    /**
58
     * Processes this test, when one of its tokens is encountered.
59
     *
60
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
     * @param int                  $stackPtr  The position of the current token in the
62
     *                                        stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        $tokens         = $phpcsFile->getTokens();
69
        $functionName   = $tokens[$stackPtr]['content'];
70
        $functionNameLc = strtolower($functionName);
71
72
        // Bow out if not one of the functions we're targetting.
73
        if (isset($this->hashAlgoFunctions[$functionNameLc]) === false) {
74
            return;
75
        }
76
77
        // Get the parameter from the function call which should contain the algorithm name.
78
        $algoParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->hashAlgoFunctions[$functionNameLc]);
79
        if ($algoParam === false) {
80
            return;
81
        }
82
83
        /**
84
         * Algorithm is a T_CONSTANT_ENCAPSED_STRING, so we need to remove the quotes.
85
         */
86
        $algo = strtolower(trim($algoParam['raw']));
87
        $algo = $this->stripQuotes($algo);
88
89
90
        // Bow out if not one of the algorithms we're targetting.
91
        if (isset($this->removedAlgorithms[$algo]) === false) {
92
            return;
93
        }
94
95
        // Check if the algorithm used is deprecated or removed.
96
        $errorInfo = $this->getErrorInfo($algo);
97
98
        if ($errorInfo['deprecated'] !== '' || $errorInfo['removed'] !== '') {
99
            $this->addError($phpcsFile, $algoParam['start'], $algo, $errorInfo);
100
        }
101
102
    }//end process()
103
104
105
    /**
106
     * Retrieve the relevant (version) information for the error message.
107
     *
108
     * @param string $algorithm The name of the algorithm.
109
     *
110
     * @return array
111
     */
112
    protected function getErrorInfo($algorithm)
113
    {
114
        $errorInfo  = array(
115
            'deprecated'  => '',
116
            'removed'     => '',
117
            'error'       => false,
118
        );
119
120
        foreach ($this->removedAlgorithms[$algorithm] as $version => $removed) {
121
            if ($this->supportsAbove($version)) {
122
                if ($removed === true && $errorInfo['removed'] === '') {
123
                    $errorInfo['removed'] = $version;
124
                    $errorInfo['error']   = true;
125
                } elseif ($errorInfo['deprecated'] === '') {
126
                    $errorInfo['deprecated'] = $version;
127
                }
128
            }
129
        }
130
131
        return $errorInfo;
132
133
    }//end getErrorInfo()
134
135
136
    /**
137
     * Generates the error or warning for this sniff.
138
     *
139
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
140
     * @param int                  $stackPtr  The position of the function
141
     *                                        in the token array.
142
     * @param string               $algorithm The name of the algorithm.
143
     * @param array                $errorInfo Array with details about the versions
144
     *                                        in which the algorithm was deprecated
145
     *                                        and/or removed.
146
     *
147
     * @return void
148
     */
149
    protected function addError($phpcsFile, $stackPtr, $algorithm, $errorInfo)
150
    {
151
        $error     = 'The %s hash algorithm is ';
152
        $errorCode = $algorithm . 'Found';
153
        $data      = array($algorithm);
154
155
        if ($errorInfo['deprecated'] !== '') {
156
            $error .= 'deprecated since PHP version %s and ';
157
            $data[] = $errorInfo['deprecated'];
158
        }
159
        if ($errorInfo['removed'] !== '') {
160
            $error .= 'removed since PHP version %s and ';
161
            $data[] = $errorInfo['removed'];
162
        }
163
164
        // Remove the last 'and' from the message.
165
        $error = substr($error, 0, strlen($error) - 5);
166
167 View Code Duplication
        if ($errorInfo['error'] === true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
            $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
169
        } else {
170
            $phpcsFile->addWarning($error, $stackPtr, $errorCode, $data);
171
        }
172
173
    }//end addError()
174
175
}//end class
176