Completed
Push — master ( 6a4ee6...24bb48 )
by Juliette
01:33
created

DeprecatedIniDirectivesSniff   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 303
Duplicated Lines 13.86 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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