-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathConstantStringUnitTest.inc
More file actions
68 lines (52 loc) · 2.33 KB
/
ConstantStringUnitTest.inc
File metadata and controls
68 lines (52 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
if ( ! defined( 'WPCOM_VIP' ) ) { // Okay.
define( 'WPCOM_VIP', true ); // Okay.
}
if ( ! defined( WPCOM_VIP ) ) { // Error.
define( WPCOM_VIP, true ); // Error.
}
namespace Foo\Bar;
const REST_ALLOWED_META_PREFIXES = [ 'foo-', 'bar-', 'baz-' ];
if ( defined( __NAMESPACE__ . '\REST_ALLOWED_META_PREFIXES' ) && in_array( 'foo-', REST_ALLOWED_META_PREFIXES, true ) ) { // Ok.
define( __NAMESPACE__ . '\\' . REST_ALLOWED_META_PREFIXES[1], $value ); // OK.
}
define( __NAMESPACE__ . '\PLUGIN_URL', \plugins_url( '/', __FILE__ ) ); // OK.
if ( defined( __NAMESPACE__ . '\\LOADED' ) ) {} // OK.
if ( defined( $obj->constant_name_property ) === false ) { // OK.
define( $variable_containing_constant_name, $constant_value ); // OK.
}
if ( defined( MY_PREFIX . '_CONSTANT_NAME' ) === false ) { // OK.
define( 'PREFIX_' . $variable_part, $constant_value ); // OK.
}
if ( ! defined($generator->get()) { // OK.
define( $generator->getLast(), 'value'); // OK.
}
$defined = defined(); // OK, ignore.
$defined = defined( /*comment*/ ); // OK, ignore.
// Safeguard that calls to namespaced functions or methods are not confused with the global functions.
if ( ! Not\defined( WPCOM_VIP ) ) { // Okay.
$obj->define( WPCOM_VIP, true ); // Okay.
}
if ( ! \Fully\Qualified\defined( WPCOM_VIP ) ) { // Okay.
namespace\define( WPCOM_VIP, true ); // Okay.
}
// Safeguard that fully qualified function calls are handled correctly.
if ( ! \defined( 'WPCOM_VIP' ) ) { // Okay.
\define( 'WPCOM_VIP', true, ); // Okay.
}
if ( ! \defined( WPCOM_VIP, ) ) { // Error.
\define( WPCOM_VIP, true ); // Error.
}
// Safeguard correct handling of case-insensitivity of function names.
if ( ! DEFINED( WPCOM_VIP ) ) { // Error.
Define( WPCOM_VIP, true ); // Error.
}
// Safeguard correct handling of function calls using PHP 8.0+ named parameters.
\defined( constant_nam: WPCOM_VIP ); // Okay, well not really, typo in param name, but that's not the concern of the sniff.
define(value: true, constant_name: 'WPCOM_VIP', ); // Okay.
define(case_insensitive: true, constant_name: WPCOM_VIP ); // Error.
// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute.
#[Define('name', 'value')]
function foo() {}
define(...$params); // PHP 5.6 argument unpacking will be ignored.
add_action('my_action', define(...)); // PHP 8.1 first class callable will be ignored.