]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/File.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / File.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @package    PHPUnit
38  * @subpackage Util
39  * @author     Sebastian Bergmann <sebastian@phpunit.de>
40  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://www.phpunit.de/
43  * @since      File available since Release 3.4.0
44  */
45
46 if (!defined('T_NAMESPACE')) {
47     define('T_NAMESPACE', 377);
48 }
49
50 /**
51  * File helpers.
52  *
53  * @package    PHPUnit
54  * @subpackage Util
55  * @author     Sebastian Bergmann <sebastian@phpunit.de>
56  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
57  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58  * @version    Release: 3.5.13
59  * @link       http://www.phpunit.de/
60  * @since      Class available since Release 3.4.0
61  */
62 class PHPUnit_Util_File
63 {
64     /**
65      * @var array
66      */
67     protected static $cache = array();
68
69     /**
70      * Returns information on the classes declared in a sourcefile.
71      *
72      * @param  string $filename
73      * @return array
74      */
75     public static function getClassesInFile($filename)
76     {
77         if (!isset(self::$cache[$filename])) {
78             self::$cache[$filename] = self::parseFile($filename);
79         }
80
81         return self::$cache[$filename];
82     }
83
84     /**
85      * Parses a file for class and method information.
86      *
87      * @param  string $filename
88      * @return array
89      */
90     protected static function parseFile($filename)
91     {
92         $result = array();
93
94         $tokens                     = token_get_all(
95                                         file_get_contents($filename)
96                                       );
97         $numTokens                  = count($tokens);
98         $blocks                     = array();
99         $line                       = 1;
100         $currentBlock               = FALSE;
101         $currentNamespace           = FALSE;
102         $currentClass               = FALSE;
103         $currentFunction            = FALSE;
104         $currentFunctionStartLine   = FALSE;
105         $currentFunctionTokens      = array();
106         $currentDocComment          = FALSE;
107         $currentSignature           = FALSE;
108         $currentSignatureStartToken = FALSE;
109
110         for ($i = 0; $i < $numTokens; $i++) {
111             if ($currentFunction !== FALSE) {
112                 $currentFunctionTokens[] = $tokens[$i];
113             }
114
115             if (is_string($tokens[$i])) {
116                 if ($tokens[$i] == '{') {
117                     if ($currentBlock == T_CLASS) {
118                         $block = $currentClass;
119                     }
120
121                     else if ($currentBlock == T_FUNCTION) {
122                         $currentSignature = '';
123
124                         for ($j = $currentSignatureStartToken; $j < $i; $j++) {
125                             if (is_string($tokens[$j])) {
126                                 $currentSignature .= $tokens[$j];
127                             } else {
128                                 $currentSignature .= $tokens[$j][1];
129                             }
130                         }
131
132                         $currentSignature = trim($currentSignature);
133
134                         $block                      = $currentFunction;
135                         $currentSignatureStartToken = FALSE;
136                     }
137
138                     else {
139                         $block = FALSE;
140                     }
141
142                     array_push($blocks, $block);
143
144                     $currentBlock = FALSE;
145                 }
146
147                 else if ($tokens[$i] == '}') {
148                     $block = array_pop($blocks);
149
150                     if ($block !== FALSE && $block !== NULL) {
151                         if ($block == $currentFunction) {
152                             if ($currentDocComment !== FALSE) {
153                                 $docComment        = $currentDocComment;
154                                 $currentDocComment = FALSE;
155                             } else {
156                                 $docComment = '';
157                             }
158
159                             $tmp = array(
160                               'docComment' => $docComment,
161                               'signature'  => $currentSignature,
162                               'startLine'  => $currentFunctionStartLine,
163                               'endLine'    => $line,
164                               'tokens'     => $currentFunctionTokens
165                             );
166
167                             if ($currentClass !== FALSE) {
168                                 $result[$currentClass]['methods'][$currentFunction] = $tmp;
169                             }
170
171                             $currentFunction          = FALSE;
172                             $currentFunctionStartLine = FALSE;
173                             $currentFunctionTokens    = array();
174                             $currentSignature         = FALSE;
175                         }
176
177                         else if ($block == $currentClass) {
178                             $result[$currentClass]['endLine'] = $line;
179
180                             $currentClass          = FALSE;
181                             $currentClassStartLine = FALSE;
182                         }
183                     }
184                 }
185
186                 continue;
187             }
188
189             switch ($tokens[$i][0]) {
190                 case T_HALT_COMPILER: {
191                     return;
192                 }
193                 break;
194
195                 case T_NAMESPACE: {
196                     $currentNamespace = $tokens[$i+2][1];
197
198                     for ($j = $i+3; $j < $numTokens; $j += 2) {
199                         if ($tokens[$j][0] == T_NS_SEPARATOR) {
200                             $currentNamespace .= '\\' . $tokens[$j+1][1];
201                         } else {
202                             break;
203                         }
204                     }
205                 }
206                 break;
207
208                 case T_CURLY_OPEN: {
209                     $currentBlock = T_CURLY_OPEN;
210                     array_push($blocks, $currentBlock);
211                 }
212                 break;
213
214                 case T_DOLLAR_OPEN_CURLY_BRACES: {
215                     $currentBlock = T_DOLLAR_OPEN_CURLY_BRACES;
216                     array_push($blocks, $currentBlock);
217                 }
218                 break;
219
220                 case T_CLASS: {
221                     $currentBlock = T_CLASS;
222
223                     if ($currentNamespace === FALSE) {
224                         $currentClass = $tokens[$i+2][1];
225                     } else {
226                         $currentClass = $currentNamespace . '\\' .
227                                         $tokens[$i+2][1];
228                     }
229
230                     if ($currentDocComment !== FALSE) {
231                         $docComment        = $currentDocComment;
232                         $currentDocComment = FALSE;
233                     } else {
234                         $docComment = '';
235                     }
236
237                     $result[$currentClass] = array(
238                       'methods'    => array(),
239                       'docComment' => $docComment,
240                       'startLine'  => $line
241                     );
242                 }
243                 break;
244
245                 case T_FUNCTION: {
246                     if (!((is_array($tokens[$i+2]) &&
247                           $tokens[$i+2][0] == T_STRING) ||
248                          (is_string($tokens[$i+2]) &&
249                           $tokens[$i+2] == '&' &&
250                           is_array($tokens[$i+3]) &&
251                           $tokens[$i+3][0] == T_STRING))) {
252                         continue;
253                     }
254
255                     $currentBlock             = T_FUNCTION;
256                     $currentFunctionStartLine = $line;
257
258                     $done                       = FALSE;
259                     $currentSignatureStartToken = $i - 1;
260
261                     do {
262                         switch ($tokens[$currentSignatureStartToken][0]) {
263                             case T_ABSTRACT:
264                             case T_FINAL:
265                             case T_PRIVATE:
266                             case T_PUBLIC:
267                             case T_PROTECTED:
268                             case T_STATIC:
269                             case T_WHITESPACE: {
270                                 $currentSignatureStartToken--;
271                             }
272                             break;
273
274                             default: {
275                                 $currentSignatureStartToken++;
276                                 $done = TRUE;
277                             }
278                         }
279                     }
280                     while (!$done);
281
282                     if (isset($tokens[$i+2][1])) {
283                         $functionName = $tokens[$i+2][1];
284                     }
285
286                     else if (isset($tokens[$i+3][1])) {
287                         $functionName = $tokens[$i+3][1];
288                     }
289
290                     if ($currentNamespace === FALSE) {
291                         $currentFunction = $functionName;
292                     } else {
293                         $currentFunction = $currentNamespace . '\\' .
294                                            $functionName;
295                     }
296                 }
297                 break;
298
299                 case T_DOC_COMMENT: {
300                     $currentDocComment = $tokens[$i][1];
301                 }
302                 break;
303             }
304
305             $line += substr_count($tokens[$i][1], "\n");
306         }
307
308         return $result;
309     }
310 }