Completed
Pull Request — master (#291)
by Juliette
02:26
created

DeprecatedIniDirectivesSniff   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 300
Duplicated Lines 14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 42
loc 300
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 39 39 5
A getItemArray() 0 4 1
A getErrorInfo() 3 11 3
A getErrorMsgTemplate() 0 4 1
A getAlternativeOptionTemplate() 0 4 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_DeprecatedIniDirectivesSniff.
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_DeprecatedIniDirectivesSniff.
15
 *
16
 * Discourages the use of deprecated INI directives through ini_set() or ini_get().
17
 *
18
 * @category  PHP
19
 * @package   PHPCompatibility
20
 * @author    Wim Godden <[email protected]>
21
 * @copyright 2012 Cu.be Solutions bvba
22
 */
23
class PHPCompatibility_Sniffs_PHP_DeprecatedIniDirectivesSniff
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...
24
    extends PHPCompatibility_AbstractRemovedFeatureSniff
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
25
{
26
    /**
27
     * A list of deprecated INI directives.
28
     *
29
     * The array lists : version number with false (deprecated) and true (removed).
30
     * If's sufficient to list the first version where the ini directive was deprecated/removed.
31
     *
32
     * @var array(string)
33
     */
34
    protected $deprecatedIniDirectives = array(
35
        'fbsql.batchSize' => array(
36
            '5.1' => true,
37
            'alternative' => 'fbsql.batchsize',
38
        ),
39
40
        'ifx.allow_persistent' => array(
41
            '5.2.1' => true
42
        ),
43
        'ifx.blobinfile' => array(
44
            '5.2.1' => true
45
        ),
46
        'ifx.byteasvarchar' => array(
47
            '5.2.1' => true
48
        ),
49
        'ifx.charasvarchar' => array(
50
            '5.2.1' => true
51
        ),
52
        'ifx.default_host' => array(
53
            '5.2.1' => true
54
        ),
55
        'ifx.default_password' => array(
56
            '5.2.1' => true
57
        ),
58
        'ifx.default_user' => array(
59
            '5.2.1' => true
60
        ),
61
        'ifx.max_links' => array(
62
            '5.2.1' => true
63
        ),
64
        'ifx.max_persistent' => array(
65
            '5.2.1' => true
66
        ),
67
        'ifx.nullformat' => array(
68
            '5.2.1' => true
69
        ),
70
        'ifx.textasvarchar' => array(
71
            '5.2.1' => true
72
        ),
73
74
        'zend.ze1_compatibility_mode' => array(
75
            '5.3' => true,
76
        ),
77
78
        'allow_call_time_pass_reference' => array(
79
            '5.3' => false,
80
            '5.4' => true
81
        ),
82
        'define_syslog_variables' => array(
83
            '5.3' => false,
84
            '5.4' => true
85
        ),
86
        'detect_unicode' => array(
87
            '5.4'         => true,
88
            'alternative' => 'zend.detect_unicode',
89
        ),
90
        'highlight.bg' => array(
91
            '5.3' => false,
92
            '5.4' => true
93
        ),
94
        'magic_quotes_gpc' => array(
95
            '5.3' => false,
96
            '5.4' => true
97
        ),
98
        'magic_quotes_runtime' => array(
99
            '5.3' => false,
100
            '5.4' => true
101
        ),
102
        'magic_quotes_sybase' => array(
103
            '5.3' => false,
104
            '5.4' => true
105
        ),
106
        'mbstring.script_encoding' => array(
107
            '5.4'         => true,
108
            'alternative' => 'zend.script_encoding',
109
        ),
110
        'register_globals' => array(
111
            '5.3' => false,
112
            '5.4' => true
113
        ),
114
        'register_long_arrays' => array(
115
            '5.3' => false,
116
            '5.4' => true
117
        ),
118
        'safe_mode' => array(
119
            '5.3' => false,
120
            '5.4' => true
121
        ),
122
        'safe_mode_allowed_env_vars' => array(
123
            '5.3' => false,
124
            '5.4' => true
125
        ),
126
        'safe_mode_exec_dir' => array(
127
            '5.3' => false,
128
            '5.4' => true
129
        ),
130
        'safe_mode_gid' => array(
131
            '5.3' => false,
132
            '5.4' => true
133
        ),
134
        'safe_mode_include_dir' => array(
135
            '5.3' => false,
136
            '5.4' => true
137
        ),
138
        'safe_mode_protected_env_vars' => array(
139
            '5.3' => false,
140
            '5.4' => true
141
        ),
142
        'session.bug_compat_42' => array(
143
            '5.3' => false,
144
            '5.4' => true
145
        ),
146
        'session.bug_compat_warn' => array(
147
            '5.3' => false,
148
            '5.4' => true
149
        ),
150
        'y2k_compliance' => array(
151
            '5.3' => false,
152
            '5.4' => true
153
        ),
154
155
        'always_populate_raw_post_data' => array(
156
            '5.6' => false,
157
            '7.0' => true
158
        ),
159
        'iconv.input_encoding' => array(
160
            '5.6' => false
161
        ),
162
        'iconv.output_encoding' => array(
163
            '5.6' => false
164
        ),
165
        'iconv.internal_encoding' => array(
166
            '5.6' => false
167
        ),
168
        'mbstring.http_input' => array(
169
            '5.6' => false
170
        ),
171
        'mbstring.http_output' => array(
172
            '5.6' => false
173
        ),
174
        'mbstring.internal_encoding' => array(
175
            '5.6' => false
176
        ),
177
178
        'asp_tags' => array(
179
            '7.0' => true
180
        ),
181
        'xsl.security_prefs' => array(
182
            '7.0' => true
183
        ),
184
185
        'mcrypt.algorithms_dir' => array(
186
            '7.1' => false
187
        ),
188
        'mcrypt.modes_dir' => array(
189
            '7.1' => false
190
        ),
191
        'session.entropy_file' => array(
192
            '7.1' => true
193
        ),
194
        'session.entropy_length' => array(
195
            '7.1' => true
196
        ),
197
        'session.hash_function' => array(
198
            '7.1' => true
199
        ),
200
        'session.hash_bits_per_character' => array(
201
            '7.1' => true
202
        ),
203
    );
204
205
    /**
206
     * Returns an array of tokens this test wants to listen for.
207
     *
208
     * @return array
209
     */
210
    public function register()
211
    {
212
        return array(T_STRING);
213
214
    }//end register()
215
216
    /**
217
     * Processes this test, when one of its tokens is encountered.
218
     *
219
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
220
     * @param int                  $stackPtr  The position of the current token in the
221
     *                                        stack passed in $tokens.
222
     *
223
     * @return void
224
     */
225 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...
226
    {
227
        $tokens = $phpcsFile->getTokens();
228
229
        $ignore = array(
230
                   T_DOUBLE_COLON,
231
                   T_OBJECT_OPERATOR,
232
                   T_FUNCTION,
233
                   T_CONST,
234
                  );
235
236
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
237
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
238
            // Not a call to a PHP function.
239
            return;
240
        }
241
242
        $functionLc = strtolower($tokens[$stackPtr]['content']);
243
        if (isset($this->iniFunctions[$functionLc]) === false) {
244
            return;
245
        }
246
247
        $iniToken = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->iniFunctions[$functionLc]);
248
        if ($iniToken === false) {
249
            return;
250
        }
251
252
        $filteredToken = $this->stripQuotes($iniToken['raw']);
253
        if (isset($this->deprecatedIniDirectives[$filteredToken]) === false) {
254
            return;
255
        }
256
257
        $itemInfo = array(
258
            'name'       => $filteredToken,
259
            'functionLc' => $functionLc,
260
        );
261
        $this->handleFeature($phpcsFile, $iniToken['end'], $itemInfo);
262
263
    }//end process()
264
265
266
    /**
267
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
268
     *
269
     * @param array $itemInfo Base information about the item.
270
     *
271
     * @return array Version and other information about the item.
272
     */
273
    public function getItemArray(array $itemInfo)
274
    {
275
        return $this->deprecatedIniDirectives[$itemInfo['name']];
276
    }
277
278
279
    /**
280
     * Retrieve the relevant detail (version) information for use in an error message.
281
     *
282
     * @param array $itemArray Version and other information about the item.
283
     * @param array $itemInfo  Base information about the item.
284
     *
285
     * @return array
286
     */
287
    public function getErrorInfo(array $itemArray, array $itemInfo)
288
    {
289
        $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
290
291
        // Lower error level to warning if the function used was ini_get.
292 View Code Duplication
        if ($errorInfo['error'] === true && $itemInfo['functionLc'] === 'ini_get') {
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...
293
            $errorInfo['error'] = false;
294
        }
295
296
        return $errorInfo;
297
    }
298
299
300
    /**
301
     * Get the error message template for this sniff.
302
     *
303
     * @return string
304
     */
305
    protected function getErrorMsgTemplate()
306
    {
307
        return "INI directive '%s' is ";
308
    }
309
310
311
    /**
312
     * Get the error message template for suggesting an alternative for a specific sniff.
313
     *
314
     * @return string
315
     */
316
    protected function getAlternativeOptionTemplate()
317
    {
318
        return str_replace("%s", "'%s'", parent::getAlternativeOptionTemplate());
319
    }
320
321
322
}//end class
323