1010namespace WordPressVIPMinimum \Sniffs \Performance ;
1111
1212use PHP_CodeSniffer \Util \Tokens ;
13- use WordPressVIPMinimum \Sniffs \Sniff ;
13+ use PHPCSUtils \Utils \PassedParameters ;
14+ use WordPressCS \WordPress \AbstractFunctionParameterSniff ;
1415
1516/**
1617 * Restricts the implementation of taxonomy term meta via options.
1718 */
18- class TaxonomyMetaInOptionsSniff extends Sniff {
19+ class TaxonomyMetaInOptionsSniff extends AbstractFunctionParameterSniff {
1920
2021 /**
2122 * List of options_ functions
2223 *
24+ * @deprecated 3.1.0 This property should never have been public.
25+ *
2326 * @var array<string>
2427 */
2528 public $ option_functions = [
@@ -45,34 +48,47 @@ class TaxonomyMetaInOptionsSniff extends Sniff {
4548 ];
4649
4750 /**
48- * Returns an array of tokens this test wants to listen for .
51+ * The group name for this group of functions .
4952 *
50- * @return array<int| string>
53+ * @var string
5154 */
52- public function register () {
53- return [ T_STRING ];
54- }
55+ protected $ group_name = 'option_functions ' ;
5556
5657 /**
57- * Process this test when one of its tokens is encountered
58+ * Functions this sniff is looking for.
5859 *
59- * @param int $stackPtr The position of the current token in the stack passed in $tokens.
60+ * @var array<string, true> Keys are the target functions, value irrelevant.
61+ */
62+ protected $ target_functions = [
63+ 'get_option ' => true ,
64+ 'add_option ' => true ,
65+ 'update_option ' => true ,
66+ 'delete_option ' => true ,
67+ ];
68+
69+
70+ /**
71+ * Process the parameters of a matched function.
72+ *
73+ * @param int $stackPtr The position of the current token in the stack.
74+ * @param string $group_name The name of the group which was matched.
75+ * @param string $matched_content The token content (function name) which was matched
76+ * in lowercase.
77+ * @param array $parameters Array with information about the parameters.
6078 *
6179 * @return void
6280 */
63- public function process_token ( $ stackPtr ) {
64-
65- if ( in_array ( $ this ->tokens [ $ stackPtr ]['content ' ], $ this ->option_functions , true ) === false ) {
81+ public function process_parameters ( $ stackPtr , $ group_name , $ matched_content , $ parameters ) {
82+ $ target_param = PassedParameters::getParameterFromStack ( $ parameters , 1 , 'option ' );
83+ if ( $ target_param === false ) {
84+ // Missing (required) target parameter. Probably live coding, nothing to examine (yet). Bow out.
6685 return ;
6786 }
6887
69- $ openBracket = $ this ->phpcsFile ->findNext ( Tokens::$ emptyTokens , $ stackPtr + 1 , null , true );
70-
71- if ( $ this ->tokens [ $ openBracket ]['code ' ] !== T_OPEN_PARENTHESIS ) {
72- return ;
73- }
88+ $ param_start = $ target_param ['start ' ];
89+ $ param_end = ( $ target_param ['end ' ] + 1 ); // Add one to include the last token in the parameter in findNext searches.
7490
75- $ param_ptr = $ this ->phpcsFile ->findNext ( Tokens::$ emptyTokens , $ openBracket + 1 , null , true );
91+ $ param_ptr = $ this ->phpcsFile ->findNext ( Tokens::$ emptyTokens , $ param_start , $ param_end , true );
7692
7793 if ( $ this ->tokens [ $ param_ptr ]['code ' ] === T_DOUBLE_QUOTED_STRING ) {
7894 foreach ( $ this ->taxonomy_term_patterns as $ taxonomy_term_pattern ) {
@@ -101,7 +117,9 @@ public function process_token( $stackPtr ) {
101117 }
102118
103119 $ object_operator = $ this ->phpcsFile ->findNext ( Tokens::$ emptyTokens , $ variable_name + 1 , null , true );
104- if ( $ this ->tokens [ $ object_operator ]['code ' ] !== T_OBJECT_OPERATOR ) {
120+ if ( $ this ->tokens [ $ object_operator ]['code ' ] !== T_OBJECT_OPERATOR
121+ && $ this ->tokens [ $ object_operator ]['code ' ] !== T_NULLSAFE_OBJECT_OPERATOR
122+ ) {
105123 return ;
106124 }
107125
0 commit comments