Completed
Pull Request — master (#242)
by Juliette
02:24
created

PHPCompatibility_Sniffs_PHP_NewHashAlgorithmsSniff   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 152
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 20
loc 152
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 20 20 5
A getErrorInfo() 0 15 4
A addError() 0 12 1

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_NewHashAlgorithmsSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_NewHashAlgorithmsSniff.
12
 *
13
 * @category  PHP
14
 * @package   PHPCompatibility
15
 * @author    Juliette Reinders Folmer <[email protected]>
16
 */
17
class PHPCompatibility_Sniffs_PHP_NewHashAlgorithmsSniff 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...
18
{
19
    /**
20
     * A list of new hash algorithms, not present in older versions.
21
     *
22
     * The array lists : version number with false (not present) or true (present).
23
     * If's sufficient to list the first version where the hash algorithm appears.
24
     *
25
     * @var array(string => array(string => bool))
26
     */
27
    protected $newAlgorithms = array(
28
        'md2' => array(
29
            '5.2' => false,
30
            '5.3' => true,
31
        ),
32
        'ripemd256' => array(
33
            '5.2' => false,
34
            '5.3' => true,
35
        ),
36
        'ripemd320' => array(
37
            '5.2' => false,
38
            '5.3' => true,
39
        ),
40
        'salsa10' => array(
41
            '5.2' => false,
42
            '5.3' => true,
43
        ),
44
        'salsa20' => array(
45
            '5.2' => false,
46
            '5.3' => true,
47
        ),
48
        'snefru256' => array(
49
            '5.2' => false,
50
            '5.3' => true,
51
        ),
52
        'sha224' => array(
53
            '5.2' => false,
54
            '5.3' => true,
55
        ),
56
        'joaat' => array(
57
            '5.3' => false,
58
            '5.4' => true,
59
        ),
60
        'fnv132' => array(
61
            '5.3' => false,
62
            '5.4' => true,
63
        ),
64
        'fnv164' => array(
65
            '5.3' => false,
66
            '5.4' => true,
67
        ),
68
        'gost-crypto' => array(
69
            '5.5' => false,
70
            '5.6' => true,
71
        ),
72
    );
73
74
75
    /**
76
     * Returns an array of tokens this test wants to listen for.
77
     *
78
     * @return array
79
     */
80
    public function register()
81
    {
82
        return array(T_STRING);
83
84
    }//end register()
85
86
87
    /**
88
     * Processes this test, when one of its tokens is encountered.
89
     *
90
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
91
     * @param int                  $stackPtr  The position of the current token in the
92
     *                                        stack passed in $tokens.
93
     *
94
     * @return void
95
     */
96 View Code Duplication
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
97
    {
98
        $algo = $this->maybeGetHashAlgorithmParameter($phpcsFile, $stackPtr);
99
        if (empty($algo) || is_string($algo) === false) {
100
            return;
101
        }
102
103
        // Bow out if not one of the algorithms we're targetting.
104
        if (isset($this->newAlgorithms[$algo]) === false) {
105
            return;
106
        }
107
108
        // Check if the algorithm used is new.
109
        $errorInfo = $this->getErrorInfo($algo);
110
111
        if ($errorInfo['not_in_version'] !== '') {
112
            $this->addError($phpcsFile, $stackPtr, $algo, $errorInfo);
113
        }
114
115
    }//end process()
116
117
118
    /**
119
     * Retrieve the relevant (version) information for the error message.
120
     *
121
     * @param string $algorithm The name of the algorithm.
122
     *
123
     * @return array
124
     */
125
    protected function getErrorInfo($algorithm)
126
    {
127
        $errorInfo  = array(
128
            'not_in_version' => '',
129
        );
130
131
        foreach ($this->newAlgorithms[$algorithm] as $version => $present) {
132
            if ($present === false && $this->supportsBelow($version)) {
133
                $errorInfo['not_in_version'] = $version;
134
            }
135
        }
136
137
        return $errorInfo;
138
139
    }//end getErrorInfo()
140
141
142
    /**
143
     * Generates the error or warning for this sniff.
144
     *
145
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
146
     * @param int                  $stackPtr  The position of the function
147
     *                                        in the token array.
148
     * @param string               $algorithm The name of the algorithm.
149
     * @param array                $errorInfo Array with details about the versions
150
     *                                        in which the algorithm was deprecated
151
     *                                        and/or removed.
152
     *
153
     * @return void
154
     */
155
    protected function addError($phpcsFile, $stackPtr, $algorithm, $errorInfo)
156
    {
157
        $error     = 'The %s hash algorithm is not present in PHP version %s or earlier ';
158
        $errorCode = $algorithm . 'Found';
159
        $data      = array(
160
            $algorithm,
161
            $errorInfo['not_in_version'],
162
        );
163
164
        $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
165
166
    }//end addError()
167
168
}//end class
169