Skip to content

Commit ae34825

Browse files
authored
Merge pull request #2581 from rodrigoprimo/add-abstract-function-restrictions-tests
AbstractFunctionRestrictions sniffs: add tests for namespaced names
2 parents 93c8284 + 97b2e96 commit ae34825

20 files changed

+213
-120
lines changed

WordPress/Tests/DB/RestrictedFunctionsUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ WP_Date_Query::build_mysql_datetime(); // Ok.
9494
myFictionFunction(); // Bad.
9595
myFictional(); // OK.
9696
Myfictional(); // OK.
97+
98+
/*
99+
* Safeguard correct handling of all types of namespaced function calls.
100+
*/
101+
\mysql_connect();
102+
MyNamespace\mysql_connect();
103+
\MyNamespace\mysql_connect();
104+
namespace\mysql_connect(); // The sniff should start flagging this once it can resolve relative namespaces.

WordPress/Tests/DB/RestrictedFunctionsUnitTest.php

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,54 +63,56 @@ protected function tearDown(): void {
6363
*/
6464
public function getErrorList() {
6565
return array(
66-
25 => 1,
67-
26 => 1,
68-
27 => 1,
69-
28 => 1,
70-
29 => 1,
71-
30 => 1,
72-
31 => 1,
73-
32 => 1,
74-
33 => 1,
66+
25 => 1,
67+
26 => 1,
68+
27 => 1,
69+
28 => 1,
70+
29 => 1,
71+
30 => 1,
72+
31 => 1,
73+
32 => 1,
74+
33 => 1,
7575

76-
36 => 1,
77-
37 => 1,
78-
38 => 1,
79-
39 => 1,
80-
40 => 1,
81-
41 => 1,
82-
42 => 1,
83-
43 => 1,
84-
44 => 1,
76+
36 => 1,
77+
37 => 1,
78+
38 => 1,
79+
39 => 1,
80+
40 => 1,
81+
41 => 1,
82+
42 => 1,
83+
43 => 1,
84+
44 => 1,
8585

86-
47 => 1,
87-
48 => 1,
88-
49 => 1,
89-
50 => 1,
90-
51 => 1,
86+
47 => 1,
87+
48 => 1,
88+
49 => 1,
89+
50 => 1,
90+
51 => 1,
9191

92-
54 => 1,
93-
55 => 1,
94-
56 => 1,
95-
57 => 1,
92+
54 => 1,
93+
55 => 1,
94+
56 => 1,
95+
57 => 1,
9696

97-
60 => 1,
97+
60 => 1,
9898

99-
63 => 1,
99+
63 => 1,
100100

101-
66 => 1,
102-
67 => 1,
103-
68 => 1,
104-
69 => 1,
105-
70 => 1,
106-
71 => 1,
107-
72 => 1,
108-
73 => 1,
109-
74 => 1,
110-
75 => 1,
111-
76 => 1,
101+
66 => 1,
102+
67 => 1,
103+
68 => 1,
104+
69 => 1,
105+
70 => 1,
106+
71 => 1,
107+
72 => 1,
108+
73 => 1,
109+
74 => 1,
110+
75 => 1,
111+
76 => 1,
112112

113-
94 => 1,
113+
94 => 1,
114+
115+
101 => 1,
114116
);
115117
}
116118

WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ $date->setTimezone( new DateTimeZone( 'America/Toronto' ) ); // Yay!
77

88
$post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), date( __( 'g:i a' ), $now ) ); // Error.
99
$post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), gmdate( __( 'F j, Y' ), $now ), gmdate( __( 'g:i a' ), $now ) ); // OK.
10+
11+
/*
12+
* Safeguard correct handling of all types of namespaced function calls.
13+
*/
14+
\date_default_timezone_set( 'Foo/Bar' );
15+
MyNamespace\date_default_timezone_set( 'Foo/Bar' );
16+
\MyNamespace\date_default_timezone_set( 'Foo/Bar' );
17+
namespace\date_default_timezone_set( 'Foo/Bar' ); // The sniff should start flagging this once it can resolve relative namespaces.

WordPress/Tests/DateTime/RestrictedFunctionsUnitTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ final class RestrictedFunctionsUnitTest extends AbstractSniffUnitTest {
2727
*/
2828
public function getErrorList() {
2929
return array(
30-
3 => 1,
31-
8 => 2,
30+
3 => 1,
31+
8 => 2,
32+
14 => 1,
3233
);
3334
}
3435

WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.inc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@ phpinfo(); // Error.
3535

3636
Wrapper_Class::var_dump(); // OK, not the native PHP function.
3737
$wrapper ->var_dump(); // OK, not the native PHP function.
38-
namespace\var_dump(); // OK as long as the file is namespaced.
39-
MyNamespace\var_dump(); // OK, namespaced function.
38+
39+
/*
40+
* Safeguard correct handling of all types of namespaced function calls.
41+
*/
42+
\var_dump( $value );
43+
MyNamespace\var_dump( $value );
44+
\MyNamespace\var_dump( $value );
45+
namespace\var_dump( $value ); // The sniff should start flagging this once it can resolve relative namespaces.

WordPress/Tests/PHP/DevelopmentFunctionsUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function getWarningList() {
5252
24 => 1,
5353
33 => 1,
5454
34 => 1,
55+
42 => 1,
5556
);
5657
}
5758
}

WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
4-
5-
6-
7-
3+
// Safeguard against false positives on namespaced function calls.
4+
MyNamespace\serialize( $value );
5+
\MyNamespace\serialize( $value );
6+
namespace\serialize( $value ); // The sniff should start flagging this once it can resolve relative namespaces.
87

98
serialize(); // Warning.
9+
\serialize( $value ); // Warning.
1010
unserialize(); // Warning.
1111

1212
urlencode(); // Warning.

WordPress/Tests/PHP/DiscouragedPHPFunctionsUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function getErrorList() {
3737
*/
3838
public function getWarningList() {
3939
return array(
40+
8 => 1,
4041
9 => 1,
4142
10 => 1,
4243
12 => 1,

WordPress/Tests/PHP/DontExtractUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ my_extract(); // Ok.
77
My_Object::extract(); // Ok.
88
$this->extract(); // Ok.
99
$my_object->extract(); // Ok.
10+
11+
/*
12+
* Safeguard correct handling of all types of namespaced function calls.
13+
*/
14+
\extract( array( 'a' => 1 ) );
15+
MyNamespace\extract( array( 'a' => 1 ) );
16+
\MyNamespace\extract( array( 'a' => 1 ) );
17+
namespace\extract( array( 'a' => 1 ) ); // The sniff should start flagging this once it can resolve relative namespaces.

WordPress/Tests/PHP/DontExtractUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ final class DontExtractUnitTest extends AbstractSniffUnitTest {
2929
*/
3030
public function getErrorList() {
3131
return array(
32-
3 => 1,
32+
3 => 1,
33+
14 => 1,
3334
);
3435
}
3536

0 commit comments

Comments
 (0)