Skip to content

Commit f53758d

Browse files
committed
[Enhancement] DiscouragedFunction rule improvement
1 parent 1b65bd6 commit f53758d

7 files changed

+114
-44
lines changed

Magento2/Sniffs/PHP/DiscouragedFunctionSniff.php renamed to Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* Copyright © Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento2\Sniffs\PHP;
6+
namespace Magento2\Sniffs\Functions;
77

8-
use PHP_CodeSniffer\Files\File;
98
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
109

1110
/**
@@ -20,13 +19,19 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
2019
*/
2120
protected $patternMatch = true;
2221

22+
/**
23+
* If true, an error will be thrown; otherwise a warning.
24+
*
25+
* @var boolean
26+
*/
27+
public $error = false;
28+
2329
/**
2430
* List of patterns for forbidden functions.
2531
*
2632
* @var array
2733
*/
2834
public $forbiddenFunctions = [
29-
'^assert$' => null,
3035
'^bind_textdomain_codeset$' => null,
3136
'^bindtextdomain$' => null,
3237
'^bz.*$' => null,
@@ -52,7 +57,6 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
5257
'^dirname$' => null,
5358
'^dngettext$' => null,
5459
'^domxml_.*$' => null,
55-
'^exec$' => null,
5660
'^fbsql_.*$' => null,
5761
'^fdf_add_doc_javascript$' => null,
5862
'^fdf_open$' => null,
@@ -93,7 +97,6 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
9397
'^parse_str$' => null,
9498
'^parse_url$' => null,
9599
'^parsekit_compile_string$' => null,
96-
'^passthru$' => null,
97100
'^pathinfo$' => null,
98101
'^pcntl_.*$' => null,
99102
'^posix_.*$' => null,
@@ -122,14 +125,12 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
122125
'^setcookie$' => null,
123126
'^setlocale$' => null,
124127
'^setrawcookie$' => null,
125-
'^shell_exec$' => null,
126128
'^sleep$' => null,
127129
'^socket_.*$' => null,
128130
'^stream_.*$' => null,
129131
'^sybase_.*$' => null,
130132
'^symlink$' => null,
131133
'^syslog$' => null,
132-
'^system$' => null,
133134
'^touch$' => null,
134135
'^trigger_error$' => null,
135136
'^unlink$' => null,
@@ -220,34 +221,5 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
220221
'^is_null$' => 'strict comparison "=== null"',
221222
'^intval$' => '(int) construction',
222223
'^strval$' => '(string) construction',
223-
'^md5$' => 'improved hash functions (SHA-256, SHA-512 etc.)',
224-
'^serialize$' => 'json_encode',
225-
'^unserialize$' => 'json_decode',
226224
];
227-
228-
/**
229-
* Generates warning for this sniff.
230-
*
231-
* @param File $phpcsFile The file being scanned.
232-
* @param int $stackPtr The position of the forbidden function in the token array.
233-
* @param string $function The name of the forbidden function.
234-
* @param string $pattern The pattern used for the match.
235-
*
236-
* @return void
237-
*/
238-
protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
239-
{
240-
$data = [$function];
241-
$warningMessage = 'The use of function %s() is discouraged';
242-
$warningCode = 'Found';
243-
if ($pattern === null) {
244-
$pattern = $function;
245-
}
246-
if ($this->forbiddenFunctions[$pattern] !== null) {
247-
$warningCode .= 'WithAlternative';
248-
$data[] = $this->forbiddenFunctions[$pattern];
249-
$warningMessage .= '; use %s instead.';
250-
}
251-
$phpcsFile->addWarning($warningMessage, $stackPtr, $warningCode, $data);
252-
}
253225
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Functions;
7+
8+
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
9+
10+
/**
11+
* Detects the use of insecure functions.
12+
*/
13+
class InsecureFunctionSniff extends ForbiddenFunctionsSniff
14+
{
15+
/**
16+
* If true, an error will be thrown; otherwise a warning.
17+
*
18+
* @var boolean
19+
*/
20+
public $error = false;
21+
22+
/**
23+
* List of patterns for forbidden functions.
24+
*
25+
* @var array
26+
*/
27+
public $forbiddenFunctions = [
28+
'assert' => null,
29+
'exec' => null,
30+
'passthru' => null,
31+
'shell_exec' => null,
32+
'system' => null,
33+
'md5' => 'improved hash functions (SHA-256, SHA-512 etc.)',
34+
'serialize' => 'json_encode',
35+
'unserialize' => 'json_decode',
36+
];
37+
}

Magento2/Tests/PHP/DiscouragedFunctionUnitTest.php renamed to Magento2/Tests/Functions/DiscouragedFunctionUnitTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright © Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento2\Tests\PHP;
6+
namespace Magento2\Tests\Functions;
77

88
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
99

@@ -26,7 +26,6 @@ public function getErrorList()
2626
public function getWarningList()
2727
{
2828
return [
29-
3 => 1,
3029
6 => 1,
3130
7 => 1,
3231
9 => 1,
@@ -63,7 +62,6 @@ public function getWarningList()
6362
65 => 1,
6463
67 => 1,
6564
69 => 1,
66-
71 => 1,
6765
73 => 1,
6866
74 => 1,
6967
76 => 1,
@@ -122,7 +120,6 @@ public function getWarningList()
122120
166 => 1,
123121
169 => 1,
124122
171 => 1,
125-
173 => 1,
126123
175 => 1,
127124
177 => 1,
128125
179 => 1,
@@ -152,7 +149,6 @@ public function getWarningList()
152149
229 => 1,
153150
231 => 1,
154151
233 => 1,
155-
235 => 1,
156152
237 => 1,
157153
239 => 1,
158154
241 => 1,
@@ -162,7 +158,6 @@ public function getWarningList()
162158
247 => 1,
163159
249 => 1,
164160
251 => 1,
165-
253 => 1,
166161
255 => 1,
167162
258 => 1,
168163
261 => 1,
@@ -257,7 +252,6 @@ public function getWarningList()
257252
458 => 1,
258253
460 => 1,
259254
462 => 1,
260-
464 => 1,
261255
];
262256
}
263257
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
assert($a === true);
4+
5+
exec('echo 1;');
6+
7+
passthru('echo 1;');
8+
9+
shell_exec('echo 1;');
10+
11+
system('echo 1;');
12+
13+
md5($text);
14+
15+
serialize([]);
16+
17+
unserialize('');
18+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Functions;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class InsecureFunctionUnitTest
12+
*/
13+
class InsecureFunctionUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function getWarningList()
27+
{
28+
return [
29+
3 => 1,
30+
5 => 1,
31+
7 => 1,
32+
9 => 1,
33+
11 => 1,
34+
13 => 1,
35+
15 => 1,
36+
17 => 1,
37+
];
38+
}
39+
}

Magento2/ruleset.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<rule ref="Magento2.Security.IncludeFile">
5151
<severity>10</severity>
5252
<type>error</type>
53+
<exclude-pattern>*/Test/*</exclude-pattern>
5354
</rule>
5455
<rule ref="Magento2.Security.LanguageConstruct">
5556
<severity>10</severity>
@@ -86,7 +87,7 @@
8687
<severity>9</severity>
8788
<type>warning</type>
8889
</rule>
89-
<rule ref="Magento2.PHP.DiscouragedFunction">
90+
<rule ref="Magento2.Security.InsecureFunction">
9091
<severity>9</severity>
9192
<type>warning</type>
9293
</rule>
@@ -116,6 +117,7 @@
116117
<rule ref="Magento2.Classes.ObjectInstantiation">
117118
<severity>8</severity>
118119
<type>warning</type>
120+
<exclude-pattern>*/Test/*</exclude-pattern>
119121
</rule>
120122
<rule ref="Magento2.Exceptions.DirectThrow">
121123
<severity>8</severity>
@@ -125,9 +127,16 @@
125127
<severity>8</severity>
126128
<type>warning</type>
127129
</rule>
130+
<rule ref="Magento2.Functions.DiscouragedFunction">
131+
<severity>8</severity>
132+
<type>warning</type>
133+
<exclude-pattern>*/lib/*</exclude-pattern>
134+
<exclude-pattern>*/Test/*</exclude-pattern>
135+
</rule>
128136
<rule ref="Magento2.Functions.StaticFunction">
129137
<severity>8</severity>
130138
<type>warning</type>
139+
<exclude-pattern>*/Test/*</exclude-pattern>
131140
</rule>
132141
<rule ref="Magento2.Files.LineLength">
133142
<severity>8</severity>
@@ -226,6 +235,7 @@
226235
<rule ref="Squiz.Functions.GlobalFunction">
227236
<severity>7</severity>
228237
<type>warning</type>
238+
<exclude-pattern>*/Test/*</exclude-pattern>
229239
</rule>
230240
<rule ref="Squiz.Operators.IncrementDecrementUsage">
231241
<severity>7</severity>

0 commit comments

Comments
 (0)