Skip to content

Commit 9817abb

Browse files
authored
Merge pull request #2643 from rodrigoprimo/more-namespace-tests-2
AbstractClassRestrictionsSniff sniffs: add tests for namespaced names
2 parents 9fd75c9 + a6b8b32 commit 9817abb

7 files changed

Lines changed: 107 additions & 8 deletions

WordPress/Tests/DB/RestrictedClassesUnitTest.1.inc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MyMysqli extends mysqli {}
3030
class YourMysqli extends \mysqli {}
3131

3232
class OurMysqli implements mysqli {}
33-
class TheirMysqli implements \mysqli {}
33+
class TheirMysqli implements \MYSQLI {}
3434

3535
$db5 = new PDO();
3636
$db6 = ( new PDO() )->exec();
@@ -115,3 +115,24 @@ $anon = new readonly class {
115115
$anon = new readonly class() extends PDOStatement {}; // Error.
116116

117117
$anon = new #[MyAttribute] readonly class {};
118+
119+
/*
120+
* Safeguard correct handling of namespaced class references (the namespace types not handled below are already handled
121+
* in other tests above).
122+
*/
123+
$obj = new MyNamespace\PDO();
124+
$obj = new \MyNamespace\PDOStatement();
125+
$obj = new namespace\Sub\mysqli();
126+
$obj = new namespace\PDO(); // Error.
127+
class MyClass1 extends MyNamespace\mysqli {}
128+
class MyClass2 extends \MyNamespace\PDO {}
129+
class MyClass3 extends namespace\Sub\PDOStatement {}
130+
class MyClass4 extends namespace\mysqli {} // Error.
131+
class MyClass5 implements MyNamespace\mysqli {}
132+
class MyClass6 implements \MyNamespace\PDO {}
133+
class MyClass7 implements namespace\Sub\PDOStatement {}
134+
class MyClass8 implements namespace\mysqli {} // Error.
135+
MyNamespace\mysqli::do_something();
136+
\MyNamespace\PDO::do_something();
137+
namespace\Sub\PDOStatement::do_something();
138+
namespace\MYSQLI::do_something(); // Error.

WordPress/Tests/DB/RestrictedClassesUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public function getErrorList( $testFile = '' ) {
102102
103 => 1,
103103
106 => 1,
104104
115 => 1,
105+
126 => 1,
106+
130 => 1,
107+
134 => 1,
108+
138 => 1,
105109
);
106110

107111
case 'RestrictedClassesUnitTest.2.inc':

WordPress/Tests/WP/ClassNameCaseUnitTest.inc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,29 @@ class NotYetDetected {
6565
public function paramTypeDeclaration( wp_role $role ) {}
6666
public function returnTypeDeclaration() : WP_TERM {}
6767
}
68+
69+
class ImplementsInterfaceCorrectCase implements SimplePie_Cache_Base {}
70+
class ImplementsInterfaceIncorrectCase implements simplepie_cache_base {}
71+
72+
/*
73+
* Safeguard correct handling of namespaced class references (the namespace types not handled below are already handled
74+
* in other tests above).
75+
*/
76+
$obj = new MyNamespace\atomEntry();
77+
$obj = new \MyNamespace\core_upgrader();
78+
$obj = new namespace\Sub\file_upload_upgrader();
79+
$obj = new namespace\pop3(); // Warning.
80+
class MyClass1 extends MyNamespace\twentytwenty_walker_page {}
81+
class MyClass2 extends \MyNamespace\TWENTY_ELEVEN_EPHEMERA_WIDGET {}
82+
class MyClass3 extends namespace\Sub\twenty_Twenty_One_SVG_icons {}
83+
class MyClass4 extends namespace\twentynineteen_SVG_icons {} // Warning.
84+
class MyClass5 implements \WPORG\REQUESTS\AUTH {} // Warning.
85+
class MyClass6 implements \MyNamespace\SIMPLEPIE\Cache\namefilter {}
86+
class MyClass7 implements MyNamespace\requests_auth {}
87+
class MyClass8 implements namespace\Sub\WpOrg\REQUESTS\proxy {}
88+
class MyClass9 implements namespace\simplepie\CACHE\base {} // Warning.
89+
\avifinfo\Box::prepare_query(); // Warning.
90+
MyNamespace\Avifinfo\CHAN_PROP::prepare_query();
91+
\MyNamespace\Avifinfo\features::prepare_query();
92+
namespace\Sub\AVIFINFO\parser::prepare_query();
93+
namespace\AVIFINFO\TILE::prepare_query(); // Warning.

WordPress/Tests/WP/ClassNameCaseUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public function getWarningList() {
4545
47 => 1,
4646
48 => 1,
4747
49 => 1,
48+
70 => 1,
49+
79 => 1,
50+
83 => 1,
51+
84 => 1,
52+
88 => 1,
53+
89 => 1,
54+
93 => 1,
4855
);
4956
}
5057
}
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* Safeguard correct handling of all types of namespaced class name references.
5+
*
6+
* Note: there will only be errors, no warnings, as the classes used in the code samples
7+
* were deprecated before the minimum supported WP version.
8+
*/
9+
$streams = new \WP_User_Search(); // Error.
10+
$streams = new MyNamespace\WP_HTTP_Fsockopen();
11+
$streams = new \MyNamespace\WP_Customize_New_Menu_Section();
12+
$streams = new namespace\Sub\WP_Customize_New_Menu_Control();
13+
$streams = new namespace\WP_Privacy_Data_Export_Requests_Table(); // Error.
14+
class MyClass1 extends \WP_Privacy_Data_Removal_Requests_Table {} // Error.
15+
class MyClass2 extends MyNamespace\Services_JSON {}
16+
class MyClass3 extends \MyNamespace\Services_JSON_Error {}
17+
class MyClass4 extends namespace\Sub\WP_Http_Curl {}
18+
class MyClass5 extends namespace\WP_Http_Streams {} // Error.
19+
\wp_user_search::do_something(); // Error.
20+
MyNamespace\WP_HTTP_Fsockopen::do_something();
21+
\MyNamespace\WP_Customize_New_Menu_Section::do_something();
22+
namespace\Sub\WP_Customize_New_Menu_Control::do_something();
23+
namespace\WP_PRIVACY_DATA_EXPORT_REQUESTS_TABLE::do_something(); // Error.

WordPress/Tests/WP/DeprecatedClassesUnitTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,35 @@ final class DeprecatedClassesUnitTest extends AbstractSniffUnitTest {
2424
/**
2525
* Returns the lines where errors should occur.
2626
*
27+
* @param string $testFile The test file to check for errors.
28+
*
2729
* @return array<int, int> Key is the line number, value is the number of expected errors.
2830
*/
29-
public function getErrorList() {
30-
$start_line = 9;
31-
$end_line = 28;
32-
$errors = array_fill( $start_line, ( ( $end_line - $start_line ) + 1 ), 1 );
31+
public function getErrorList( $testFile = '' ) {
32+
switch ( $testFile ) {
33+
case 'DeprecatedClassesUnitTest.1.inc':
34+
$start_line = 9;
35+
$end_line = 28;
36+
$errors = array_fill( $start_line, ( ( $end_line - $start_line ) + 1 ), 1 );
37+
38+
// Unset the lines related to version comments.
39+
unset( $errors[16], $errors[18], $errors[21], $errors[26] );
40+
41+
return $errors;
3342

34-
// Unset the lines related to version comments.
35-
unset( $errors[16], $errors[18], $errors[21], $errors[26] );
43+
case 'DeprecatedClassesUnitTest.2.inc':
44+
return array(
45+
9 => 1,
46+
13 => 1,
47+
14 => 1,
48+
18 => 1,
49+
19 => 1,
50+
23 => 1,
51+
);
3652

37-
return $errors;
53+
default:
54+
return array();
55+
}
3856
}
3957

4058
/**

0 commit comments

Comments
 (0)