1111
1212use PHP_CodeSniffer \Ruleset ;
1313use PHP_CodeSniffer \Files \DummyFile ;
14+ use PHP_CodeSniffer \Files \File ;
1415use PHP_CodeSniffer \Tests \ConfigDouble ;
1516use PHPUnit \Framework \TestCase ;
1617use ReflectionProperty ;
@@ -92,16 +93,36 @@ public static function initializeFile()
9293 */
9394 public function getTargetToken ($ commentString , $ tokenType , $ tokenContent =null )
9495 {
95- $ start = (self ::$ phpcsFile ->numTokens - 1 );
96- $ comment = self ::$ phpcsFile ->findPrevious (
96+ return self ::getTargetTokenFromFile (self ::$ phpcsFile , $ commentString , $ tokenType , $ tokenContent );
97+
98+ }//end getTargetToken()
99+
100+
101+ /**
102+ * Get the token pointer for a target token based on a specific comment found on the line before.
103+ *
104+ * Note: the test delimiter comment MUST start with "/* test" to allow this function to
105+ * distinguish between comments used *in* a test and test delimiters.
106+ *
107+ * @param \PHP_CodeSniffer\Files\File $phpcsFile The file to find the token in.
108+ * @param string $commentString The delimiter comment to look for.
109+ * @param int|string|array $tokenType The type of token(s) to look for.
110+ * @param string $tokenContent Optional. The token content for the target token.
111+ *
112+ * @return int
113+ */
114+ public static function getTargetTokenFromFile (File $ phpcsFile , $ commentString , $ tokenType , $ tokenContent =null )
115+ {
116+ $ start = ($ phpcsFile ->numTokens - 1 );
117+ $ comment = $ phpcsFile ->findPrevious (
97118 T_COMMENT ,
98119 $ start ,
99120 null ,
100121 false ,
101122 $ commentString
102123 );
103124
104- $ tokens = self :: $ phpcsFile ->getTokens ();
125+ $ tokens = $ phpcsFile ->getTokens ();
105126 $ end = ($ start + 1 );
106127
107128 // Limit the token finding to between this and the next delimiter comment.
@@ -116,7 +137,7 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null)
116137 }
117138 }
118139
119- $ target = self :: $ phpcsFile ->findNext (
140+ $ target = $ phpcsFile ->findNext (
120141 $ tokenType ,
121142 ($ comment + 1 ),
122143 $ end ,
@@ -130,12 +151,12 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null)
130151 $ msg .= ' With token content: ' .$ tokenContent ;
131152 }
132153
133- $ this -> assertFalse (true , $ msg );
154+ self :: assertFalse (true , $ msg );
134155 }
135156
136157 return $ target ;
137158
138- }//end getTargetToken ()
159+ }//end getTargetTokenFromFile ()
139160
140161
141162 /**
0 commit comments