[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /** 3 * A class for performing code analysis for php scripts 4 * It is designed to be the heart of a code limiting script 5 * to use with Savant {@link http://phpsavant.com} 6 * 7 * This code should be php4 compatiable but i've only run it in php5 and some of the Tokenizer constants have changed 8 * 9 * @author Joshua Eichorn <josh@bluga.net> 10 * @copyright Joshua Eichorn 2004 11 * @package PHPCodeAnalyzer 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU Lesser General Public License as 15 * published by the Free Software Foundation; either version 2.1 of the 16 * License, or (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, but 19 * WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 * Lesser General Public License for more details. 22 * 23 * @license http://www.gnu.org/copyleft/lesser.html LGPL 24 */ 25 26 /**#@+ 27 * compat tokeniezer defines 28 */ 29 if (! defined('T_OLD_FUNCTION')) { 30 define('T_OLD_FUNCTION', T_FUNCTION); 31 } 32 if (!defined('T_ML_COMMENT')) { 33 define('T_ML_COMMENT', T_COMMENT); 34 } else { 35 define('T_DOC_COMMENT', T_ML_COMMENT); 36 } 37 /**#@-*/ 38 39 /** 40 * Code Analysis class 41 * 42 * Example Usage: 43 * <code> 44 * $analyzer = new PHPCodeAnalyzer(); 45 * $analyzer->source = file_get_contents(__FILE__); 46 * $analyzer->analyze(); 47 * print_r($analyzer->calledMethods); 48 * </code> 49 * 50 * @todo is it important to grab the details from creating new functions defines classes? 51 * @todo support php5 only stuff like interface 52 * 53 * @version 0.4 54 * @license http://www.gnu.org/copyleft/lesser.html LGPL 55 * @copyright Joshua Eichorn 2004 56 * @package PHPCodeAnalyzer 57 * @author Joshua Eichorn <josh@bluga.net> 58 */ 59 class PHPCodeAnalyzer 60 { 61 /** 62 * Source code to analyze 63 */ 64 var $source = ""; 65 66 /** 67 * functions called 68 */ 69 var $calledFunctions = array(); 70 71 /** 72 * Called constructs 73 */ 74 var $calledConstructs = array(); 75 76 /** 77 * methods called 78 */ 79 var $calledMethods = array(); 80 81 /** 82 * static methods called 83 */ 84 var $calledStaticMethods = array(); 85 86 /** 87 * new classes instantiated 88 */ 89 var $classesInstantiated = array(); 90 91 /** 92 * variables used 93 */ 94 var $usedVariables = array(); 95 96 /** 97 * member variables used 98 */ 99 var $usedMemberVariables = array(); 100 101 /** 102 * classes created 103 */ 104 var $createdClasses = array(); 105 106 /** 107 * functions created 108 */ 109 var $createdFunctions = array(); 110 111 /** 112 * Files includes or requried 113 */ 114 var $filesIncluded = array(); 115 116 // private variables 117 /**#@+ 118 * @access private 119 */ 120 var $currentString = null; 121 var $currentStrings = null; 122 var $currentVar = false; 123 var $staticClass = false; 124 var $inNew = false; 125 var $inInclude = false; 126 var $lineNumber = 1; 127 /**#@-*/ 128 129 /** 130 * parse source filling informational arrays 131 */ 132 function analyze() 133 { 134 $tokens = token_get_all($this->source); 135 136 // mapping of token to method to call 137 $handleMap = array( 138 T_STRING => 'handleString', 139 T_CONSTANT_ENCAPSED_STRING => 'handleString', 140 T_ENCAPSED_AND_WHITESPACE => 'handleString', 141 T_CHARACTER => 'handleString', 142 T_NUM_STRING => 'handleString', 143 T_DNUMBER => 'handleString', 144 T_FUNC_C => 'handleString', 145 T_CLASS_C => 'handleString', 146 T_FILE => 'handleString', 147 T_LINE => 'handleString', 148 T_DOUBLE_ARROW => 'handleString', 149 150 T_DOUBLE_COLON => 'handleDoubleColon', 151 T_NEW => 'handleNew', 152 T_OBJECT_OPERATOR => 'handleObjectOperator', 153 T_VARIABLE => 'handleVariable', 154 T_FUNCTION => 'handleFunction', 155 T_OLD_FUNCTION => 'handleFunction', 156 T_CLASS => 'handleClass', 157 T_WHITESPACE => 'handleWhitespace', 158 T_INLINE_HTML => 'handleWhitespace', 159 T_OPEN_TAG => 'handleWhitespace', 160 T_CLOSE_TAG => 'handleWhitespace', 161 162 T_AS => 'handleAs', 163 164 T_ECHO => 'handleConstruct', 165 T_EVAL => 'handleConstruct', 166 T_UNSET => 'handleConstruct', 167 T_ISSET => 'handleConstruct', 168 T_PRINT => 'handleConstruct', 169 T_FOR => 'handleConstruct', 170 T_FOREACH=> 'handleConstruct', 171 T_EMPTY => 'handleConstruct', 172 T_EXIT => 'handleConstruct', 173 T_CASE => 'handleConstruct', 174 T_GLOBAL=> 'handleConstruct', 175 T_UNSET => 'handleConstruct', 176 T_WHILE => 'handleConstruct', 177 T_DO => 'handleConstruct', 178 T_IF => 'handleConstruct', 179 T_LIST => 'handleConstruct', 180 T_RETURN=> 'handleConstruct', 181 T_STATIC=> 'handleConstruct', 182 T_ENDFOR=> 'handleConstruct', 183 T_ENDFOREACH=> 'handleConstruct', 184 T_ENDIF=> 'handleConstruct', 185 T_ENDSWITCH=> 'handleConstruct', 186 T_ENDWHILE=> 'handleConstruct', 187 188 T_INCLUDE => 'handleInclude', 189 T_INCLUDE_ONCE => 'handleInclude', 190 T_REQUIRE => 'handleInclude', 191 T_REQUIRE_ONCE => 'handleInclude', 192 ); 193 194 foreach($tokens as $token) 195 { 196 if (is_string($token)) 197 { 198 // we have a simple 1-character token 199 $this->handleSimpleToken($token); 200 } 201 else 202 { 203 list($id, $text) = $token; 204 if (isseT($handleMap[$id])) 205 { 206 $call = $handleMap[$id]; 207 $this->$call($id,$text); 208 } 209 /*else 210 { 211 echo token_name($id).": $text<br>\n"; 212 }*/ 213 } 214 } 215 } 216 217 /** 218 * Handle a 1 char token 219 * @access private 220 */ 221 function handleSimpleToken($token) 222 { 223 if ($token !== ";") 224 { 225 $this->currentStrings .= $token; 226 } 227 switch($token) 228 { 229 case "(": 230 // method is called 231 if ($this->staticClass !== false) 232 { 233 if (!isset($this->calledStaticMethods[$this->staticClass][$this->currentString])) 234 { 235 $this->calledStaticMethods[$this->staticClass][$this->currentString] 236 = array(); 237 } 238 $this->calledStaticMethods[$this->staticClass][$this->currentString][] 239 = $this->lineNumber; 240 $this->staticClass = false; 241 } 242 else if ($this->currentVar !== false) 243 { 244 if (!isset($this->calledMethods[$this->currentVar][$this->currentString])) 245 { 246 $this->calledMethods[$this->currentVar][$this->currentString] = array(); 247 } 248 $this->calledMethods[$this->currentVar][$this->currentString][] = $this->lineNumber; 249 $this->currentVar = false; 250 } 251 else if ($this->inNew !== false) 252 { 253 $this->classInstantiated(); 254 } 255 else if ($this->currentString !== null) 256 { 257 $this->functionCalled(); 258 } 259 //$this->currentString = null; 260 break; 261 case "=": 262 case ";": 263 if ($this->inNew !== false) 264 { 265 $this->classInstantiated(); 266 } 267 else if ($this->inInclude !== false) 268 { 269 $this->fileIncluded(); 270 } 271 else if ($this->currentVar !== false) 272 { 273 $this->useMemberVar(); 274 } 275 $this->currentString = null; 276 $this->currentStrings = null; 277 break; 278 } 279 } 280 281 /** 282 * handle includes and requires 283 * @access private 284 */ 285 function handleInclude($id,$text) 286 { 287 $this->inInclude = true; 288 $this->handleConstruct($id,$text); 289 } 290 291 /** 292 * handle String tokens 293 * @access private 294 */ 295 function handleString($id,$text) 296 { 297 $this->currentString = $text; 298 $this->currentStrings .= $text; 299 } 300 301 /** 302 * handle variables 303 * @access private 304 */ 305 function handleVariable($id,$text) 306 { 307 $this->currentString = $text; 308 $this->currentStrings .= $text; 309 $this->useVariable(); 310 } 311 312 313 /** 314 * handle Double Colon tokens 315 * @access private 316 */ 317 function handleDoubleColon($id,$text) 318 { 319 $this->staticClass = $this->currentString; 320 $this->currentString = null; 321 } 322 323 /** 324 * handle new keyword 325 * @access private 326 */ 327 function handleNew($id,$text) 328 { 329 $this->inNew = true; 330 } 331 332 /** 333 * handle function 334 * @access private 335 */ 336 function handleFunction($id,$text) 337 { 338 $this->createdFunctions[] = $this->lineNumber; 339 } 340 341 /** 342 * handle class 343 * @access private 344 */ 345 function handleClass($id,$text) 346 { 347 $this->createdClasses[] = $this->lineNumber; 348 } 349 350 /** 351 * Handle -> 352 * @access private 353 */ 354 function handleObjectOperator($id,$text) 355 { 356 $this->currentVar = $this->currentString; 357 $this->currentString = null; 358 $this->currentStrings .= $text; 359 } 360 361 /** 362 * handle whitespace to figure out line counts 363 * @access private 364 */ 365 function handleWhitespace($id,$text) 366 { 367 $this->lineNumber+=substr_count($text,"\n"); 368 if ($id == T_CLOSE_TAG) 369 { 370 $this->handleSimpleToken(";"); 371 } 372 } 373 374 375 /** 376 * as has been used we must have a var before it 377 * 378 * @access private 379 */ 380 function handleAs($id,$text) 381 { 382 $this->handleSimpleToken(";"); 383 } 384 385 /** 386 * a language construct has been called record it 387 * @access private 388 */ 389 function handleConstruct($id,$construct) 390 { 391 if (!isset($this->calledConstructs[$construct])) 392 { 393 $this->calledConstructs[$construct] = array(); 394 } 395 $this->calledConstructs[$construct][] = $this->lineNumber; 396 $this->currentString = null; 397 } 398 399 /** 400 * a class was Instantiated record it 401 * @access private 402 */ 403 function classInstantiated() 404 { 405 if (!isset($this->classesInstantiated[$this->currentString])) 406 { 407 $this->classesInstantiated[$this->currentString] = array(); 408 } 409 $this->classesInstantiated[$this->currentString][] = $this->lineNumber; 410 $this->inNew = false; 411 } 412 413 /** 414 * a file was included record it 415 * @access private 416 */ 417 function fileIncluded() 418 { 419 if (!isset($this->filesIncluded[$this->currentStrings])) 420 { 421 $this->filesIncluded[$this->currentStrings] = array(); 422 } 423 $this->filesIncluded[$this->currentStrings][] = $this->lineNumber; 424 $this->inInclude = false; 425 $this->currentString = null; 426 $this->currentStrings = ""; 427 } 428 429 /** 430 * a function was called record it 431 * @access private 432 */ 433 function functionCalled($id = false) 434 { 435 if (!isset($this->calledFunctions[$this->currentString])) 436 { 437 $this->calledFunctions[$this->currentString] = array(); 438 } 439 $this->calledFunctions[$this->currentString][] = $this->lineNumber; 440 $this->currentString = null; 441 } 442 443 /** 444 * we used a member variable record it 445 * @access private 446 */ 447 function useMemberVar() 448 { 449 if (!isset($this->usedMemberVariables[$this->currentVar][$this->currentString])) 450 { 451 $this->usedMemberVariables[$this->currentVar][$this->currentString] = array(); 452 } 453 $this->usedMemberVariables[$this->currentVar][$this->currentString][] = $this->lineNumber; 454 $this->currentVar = false; 455 $this->currentString = null; 456 } 457 458 /** 459 * we used a variable record it 460 * @access private 461 */ 462 function useVariable() 463 { 464 if (!isset($this->usedVariables[$this->currentString])) 465 { 466 $this->usedVariables[$this->currentString] = array(); 467 } 468 $this->usedVariables[$this->currentString][] = $this->lineNumber; 469 } 470 } 471 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |