11
11
12
12
use PHP_CodeSniffer \Ruleset ;
13
13
use PHP_CodeSniffer \Files \DummyFile ;
14
+ use PHP_CodeSniffer \Files \File ;
14
15
use PHP_CodeSniffer \Tests \ConfigDouble ;
15
16
use PHPUnit \Framework \TestCase ;
16
17
use ReflectionProperty ;
@@ -92,16 +93,36 @@ public static function initializeFile()
92
93
*/
93
94
public function getTargetToken ($ commentString , $ tokenType , $ tokenContent =null )
94
95
{
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 (
97
118
T_COMMENT ,
98
119
$ start ,
99
120
null ,
100
121
false ,
101
122
$ commentString
102
123
);
103
124
104
- $ tokens = self :: $ phpcsFile ->getTokens ();
125
+ $ tokens = $ phpcsFile ->getTokens ();
105
126
$ end = ($ start + 1 );
106
127
107
128
// Limit the token finding to between this and the next delimiter comment.
@@ -116,7 +137,7 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null)
116
137
}
117
138
}
118
139
119
- $ target = self :: $ phpcsFile ->findNext (
140
+ $ target = $ phpcsFile ->findNext (
120
141
$ tokenType ,
121
142
($ comment + 1 ),
122
143
$ end ,
@@ -130,12 +151,12 @@ public function getTargetToken($commentString, $tokenType, $tokenContent=null)
130
151
$ msg .= ' With token content: ' .$ tokenContent ;
131
152
}
132
153
133
- $ this -> assertFalse (true , $ msg );
154
+ self :: assertFalse (true , $ msg );
134
155
}
135
156
136
157
return $ target ;
137
158
138
- }//end getTargetToken ()
159
+ }//end getTargetTokenFromFile ()
139
160
140
161
141
162
/**
0 commit comments