| Conditions | 217 |
| Paths | 19536 |
| Total Lines | 557 |
| Lines | 44 |
| Ratio | 7.9 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 155 | public function parse() { |
||
| 156 | ## remove empty lines |
||
| 157 | if (trim($this->code) == '') return 1; |
||
| 158 | |||
| 159 | $t = token_get_all('<?php '.$this->code.' ?>'); |
||
| 160 | |||
| 161 | $need_semicolon = 1; /* do we need a semicolon to complete the statement ? */ |
||
| 162 | $need_return = 1; /* can we prepend a return to the eval-string ? */ |
||
| 163 | $open_comment = 0; /* a open multi-line comment */ |
||
| 164 | $eval = ''; /* code to be eval()'ed later */ |
||
| 165 | $braces = array(); /* to track if we need more closing braces */ |
||
| 166 | |||
| 167 | $methods = array(); /* to track duplicate methods in a class declaration */ |
||
| 168 | $ts = array(); /* tokens without whitespaces */ |
||
| 169 | |||
| 170 | foreach ($t as $ndx => $token) { |
||
| 171 | if (is_array($token)) { |
||
| 172 | $ignore = 0; |
||
| 173 | |||
| 174 | switch($token[0]) { |
||
| 175 | case T_WHITESPACE: |
||
| 176 | case T_OPEN_TAG: |
||
| 177 | case T_CLOSE_TAG: |
||
| 178 | $ignore = 1; |
||
| 179 | break; |
||
| 180 | case T_FOREACH: |
||
| 181 | case T_DO: |
||
| 182 | case T_WHILE: |
||
| 183 | case T_FOR: |
||
| 184 | |||
| 185 | case T_IF: |
||
| 186 | case T_RETURN: |
||
| 187 | |||
| 188 | case T_CLASS: |
||
| 189 | case T_FUNCTION: |
||
| 190 | case T_INTERFACE: |
||
| 191 | |||
| 192 | case T_PRINT: |
||
| 193 | case T_ECHO: |
||
| 194 | |||
| 195 | case T_COMMENT: |
||
| 196 | case T_UNSET: |
||
| 197 | |||
| 198 | case T_INCLUDE: |
||
| 199 | case T_REQUIRE: |
||
| 200 | case T_INCLUDE_ONCE: |
||
| 201 | case T_REQUIRE_ONCE: |
||
| 202 | case T_TRY: |
||
| 203 | case T_SWITCH: |
||
| 204 | case T_DEFAULT: |
||
| 205 | case T_CASE: |
||
| 206 | case T_BREAK: |
||
| 207 | case T_DOC_COMMENT: |
||
| 208 | $need_return = 0; |
||
| 209 | break; |
||
| 210 | case T_EMPTY: |
||
| 211 | case T_ISSET: |
||
| 212 | case T_EVAL: |
||
| 213 | case T_EXIT: |
||
| 214 | |||
| 215 | case T_VARIABLE: |
||
| 216 | case T_STRING: |
||
| 217 | case T_NEW: |
||
| 218 | case T_EXTENDS: |
||
| 219 | case T_IMPLEMENTS: |
||
| 220 | case T_OBJECT_OPERATOR: |
||
| 221 | case T_DOUBLE_COLON: |
||
| 222 | case T_INSTANCEOF: |
||
| 223 | |||
| 224 | case T_CATCH: |
||
| 225 | case T_THROW: |
||
| 226 | |||
| 227 | case T_ELSE: |
||
| 228 | case T_AS: |
||
| 229 | case T_LNUMBER: |
||
| 230 | case T_DNUMBER: |
||
| 231 | case T_CONSTANT_ENCAPSED_STRING: |
||
| 232 | case T_ENCAPSED_AND_WHITESPACE: |
||
| 233 | case T_CHARACTER: |
||
| 234 | case T_ARRAY: |
||
| 235 | case T_DOUBLE_ARROW: |
||
| 236 | |||
| 237 | case T_CONST: |
||
| 238 | case T_PUBLIC: |
||
| 239 | case T_PROTECTED: |
||
| 240 | case T_PRIVATE: |
||
| 241 | case T_ABSTRACT: |
||
| 242 | case T_STATIC: |
||
| 243 | case T_VAR: |
||
| 244 | |||
| 245 | case T_INC: |
||
| 246 | case T_DEC: |
||
| 247 | case T_SL: |
||
| 248 | case T_SL_EQUAL: |
||
| 249 | case T_SR: |
||
| 250 | case T_SR_EQUAL: |
||
| 251 | |||
| 252 | case T_IS_EQUAL: |
||
| 253 | case T_IS_IDENTICAL: |
||
| 254 | case T_IS_GREATER_OR_EQUAL: |
||
| 255 | case T_IS_SMALLER_OR_EQUAL: |
||
| 256 | |||
| 257 | case T_BOOLEAN_OR: |
||
| 258 | case T_LOGICAL_OR: |
||
| 259 | case T_BOOLEAN_AND: |
||
| 260 | case T_LOGICAL_AND: |
||
| 261 | case T_LOGICAL_XOR: |
||
| 262 | case T_MINUS_EQUAL: |
||
| 263 | case T_PLUS_EQUAL: |
||
| 264 | case T_MUL_EQUAL: |
||
| 265 | case T_DIV_EQUAL: |
||
| 266 | case T_MOD_EQUAL: |
||
| 267 | case T_XOR_EQUAL: |
||
| 268 | case T_AND_EQUAL: |
||
| 269 | case T_OR_EQUAL: |
||
| 270 | |||
| 271 | case T_FUNC_C: |
||
| 272 | case T_CLASS_C: |
||
| 273 | case T_LINE: |
||
| 274 | case T_FILE: |
||
| 275 | |||
| 276 | case T_BOOL_CAST: |
||
| 277 | case T_INT_CAST: |
||
| 278 | case T_STRING_CAST: |
||
| 279 | |||
| 280 | /* just go on */ |
||
| 281 | break; |
||
| 282 | default: |
||
| 283 | /* debug unknown tags*/ |
||
| 284 | error_log(sprintf("unknown tag: %d (%s): %s".PHP_EOL, $token[0], token_name($token[0]), $token[1])); |
||
| 285 | |||
| 286 | break; |
||
| 287 | } |
||
| 288 | if (!$ignore) { |
||
| 289 | $eval .= $token[1]." "; |
||
| 290 | $ts[] = array("token" => $token[0], "value" => $token[1]); |
||
| 291 | } |
||
| 292 | } else { |
||
| 293 | $ts[] = array("token" => $token, "value" => ''); |
||
| 294 | |||
| 295 | $last = count($ts) - 1; |
||
| 296 | |||
| 297 | switch ($token) { |
||
| 298 | case '(': |
||
| 299 | /* walk backwards through the tokens */ |
||
| 300 | |||
| 301 | if ($last >= 4 && |
||
| 302 | $ts[$last - 1]['token'] == T_STRING && |
||
| 303 | $ts[$last - 2]['token'] == T_OBJECT_OPERATOR && |
||
| 304 | $ts[$last - 3]['token'] == ')' ) { |
||
| 305 | /* func()->method() |
||
| 306 | * |
||
| 307 | * we can't know what func() is return, so we can't |
||
| 308 | * say if the method() exists or not |
||
| 309 | * |
||
| 310 | */ |
||
| 311 | } else if ($last >= 3 && |
||
| 312 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 313 | $ts[0]['token'] != T_ABSTRACT && /* if we are not in a class definition */ |
||
| 314 | $ts[1]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 315 | $ts[$last - 1]['token'] == T_STRING && |
||
| 316 | $ts[$last - 2]['token'] == T_OBJECT_OPERATOR && |
||
| 317 | $ts[$last - 3]['token'] == T_VARIABLE ) { |
||
| 318 | |||
| 319 | /* $object->method( */ |
||
| 320 | |||
| 321 | /* catch (Exception $e) does not set $e in $GLOBALS[] */ |
||
| 322 | $in_catch = 0; |
||
| 323 | |||
| 324 | foreach ($ts as $v) { |
||
| 325 | if ($v['token'] == T_CATCH) { |
||
| 326 | $in_catch = 1; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | if (!$in_catch) { |
||
| 331 | /* $object has to exist and has to be a object */ |
||
| 332 | $objname = $ts[$last - 3]['value']; |
||
| 333 | |||
| 334 | View Code Duplication | if (!isset($GLOBALS[ltrim($objname, '$')])) { |
|
| 335 | throw new Exception(sprintf('Variable \'%s\' is not set', $objname)); |
||
| 336 | } |
||
| 337 | $object = $GLOBALS[ltrim($objname, '$')]; |
||
| 338 | |||
| 339 | if (!is_object($object)) { |
||
| 340 | throw new Exception(sprintf('Variable \'%s\' is not a class', $objname)); |
||
| 341 | } |
||
| 342 | |||
| 343 | $method = $ts[$last - 1]['value']; |
||
| 344 | |||
| 345 | /* obj */ |
||
| 346 | |||
| 347 | View Code Duplication | if (!method_exists($object, $method)) { |
|
| 348 | throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'", |
||
| 349 | $objname, get_class($object), $method)); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } else if ($last >= 3 && |
||
| 353 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 354 | $ts[$last - 1]['token'] == T_VARIABLE && |
||
| 355 | $ts[$last - 2]['token'] == T_OBJECT_OPERATOR && |
||
| 356 | $ts[$last - 3]['token'] == T_VARIABLE ) { |
||
| 357 | |||
| 358 | /* $object->$method( */ |
||
| 359 | |||
| 360 | /* $object has to exist and has to be a object */ |
||
| 361 | $objname = $ts[$last - 3]['value']; |
||
| 362 | |||
| 363 | View Code Duplication | if (!isset($GLOBALS[ltrim($objname, '$')])) { |
|
| 364 | throw new Exception(sprintf('Variable \'%s\' is not set', $objname)); |
||
| 365 | } |
||
| 366 | $object = $GLOBALS[ltrim($objname, '$')]; |
||
| 367 | |||
| 368 | if (!is_object($object)) { |
||
| 369 | throw new Exception(sprintf('Variable \'%s\' is not a class', $objname)); |
||
| 370 | } |
||
| 371 | |||
| 372 | $methodname = $ts[$last - 1]['value']; |
||
| 373 | |||
| 374 | View Code Duplication | if (!isset($GLOBALS[ltrim($methodname, '$')])) { |
|
| 375 | throw new Exception(sprintf('Variable \'%s\' is not set', $methodname)); |
||
| 376 | } |
||
| 377 | $method = $GLOBALS[ltrim($methodname, '$')]; |
||
| 378 | |||
| 379 | /* obj */ |
||
| 380 | |||
| 381 | View Code Duplication | if (!method_exists($object, $method)) { |
|
| 382 | throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'", |
||
| 383 | $objname, get_class($object), $method)); |
||
| 384 | } |
||
| 385 | |||
| 386 | } else if ($last >= 6 && |
||
| 387 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 388 | $ts[$last - 1]['token'] == T_STRING && |
||
| 389 | $ts[$last - 2]['token'] == T_OBJECT_OPERATOR && |
||
| 390 | $ts[$last - 3]['token'] == ']' && |
||
| 391 | /* might be anything as index */ |
||
| 392 | $ts[$last - 5]['token'] == '[' && |
||
| 393 | $ts[$last - 6]['token'] == T_VARIABLE ) { |
||
| 394 | |||
| 395 | /* $object[...]->method( */ |
||
| 396 | |||
| 397 | /* $object has to exist and has to be a object */ |
||
| 398 | $objname = $ts[$last - 6]['value']; |
||
| 399 | |||
| 400 | View Code Duplication | if (!isset($GLOBALS[ltrim($objname, '$')])) { |
|
| 401 | throw new Exception(sprintf('Variable \'%s\' is not set', $objname)); |
||
| 402 | } |
||
| 403 | $array = $GLOBALS[ltrim($objname, '$')]; |
||
| 404 | |||
| 405 | if (!is_array($array)) { |
||
| 406 | throw new Exception(sprintf('Variable \'%s\' is not a array', $objname)); |
||
| 407 | } |
||
| 408 | |||
| 409 | $andx = $ts[$last - 4]['value']; |
||
| 410 | |||
| 411 | if (!isset($array[$andx])) { |
||
| 412 | throw new Exception(sprintf('%s[\'%s\'] is not set', $objname, $andx)); |
||
| 413 | } |
||
| 414 | |||
| 415 | $object = $array[$andx]; |
||
| 416 | |||
| 417 | if (!is_object($object)) { |
||
| 418 | throw new Exception(sprintf('Variable \'%s\' is not a class', $objname)); |
||
| 419 | } |
||
| 420 | |||
| 421 | $method = $ts[$last - 1]['value']; |
||
| 422 | |||
| 423 | /* obj */ |
||
| 424 | |||
| 425 | View Code Duplication | if (!method_exists($object, $method)) { |
|
| 426 | throw new Exception(sprintf("Variable %s (Class '%s') doesn't have a method named '%s'", |
||
| 427 | $objname, get_class($object), $method)); |
||
| 428 | } |
||
| 429 | |||
| 430 | } else if ($last >= 3 && |
||
| 431 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 432 | $ts[$last - 1]['token'] == T_STRING && |
||
| 433 | $ts[$last - 2]['token'] == T_DOUBLE_COLON && |
||
| 434 | $ts[$last - 3]['token'] == T_STRING ) { |
||
| 435 | |||
| 436 | /* Class::method() */ |
||
| 437 | |||
| 438 | /* $object has to exist and has to be a object */ |
||
| 439 | $classname = $ts[$last - 3]['value']; |
||
| 440 | |||
| 441 | if (!class_exists($classname)) { |
||
| 442 | throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname)); |
||
| 443 | } |
||
| 444 | |||
| 445 | $method = $ts[$last - 1]['value']; |
||
| 446 | |||
| 447 | View Code Duplication | if (!in_array($method, get_class_methods($classname))) { |
|
| 448 | throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'", |
||
| 449 | $classname, $method)); |
||
| 450 | } |
||
| 451 | } else if ($last >= 3 && |
||
| 452 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 453 | $ts[$last - 1]['token'] == T_VARIABLE && |
||
| 454 | $ts[$last - 2]['token'] == T_DOUBLE_COLON && |
||
| 455 | $ts[$last - 3]['token'] == T_STRING ) { |
||
| 456 | |||
| 457 | /* $var::method() */ |
||
| 458 | |||
| 459 | /* $object has to exist and has to be a object */ |
||
| 460 | $classname = $ts[$last - 3]['value']; |
||
| 461 | |||
| 462 | if (!class_exists($classname)) { |
||
| 463 | throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname)); |
||
| 464 | } |
||
| 465 | |||
| 466 | $methodname = $ts[$last - 1]['value']; |
||
| 467 | |||
| 468 | View Code Duplication | if (!isset($GLOBALS[ltrim($methodname, '$')])) { |
|
| 469 | throw new Exception(sprintf('Variable \'%s\' is not set', $methodname)); |
||
| 470 | } |
||
| 471 | $method = $GLOBALS[ltrim($methodname, '$')]; |
||
| 472 | |||
| 473 | View Code Duplication | if (!in_array($method, get_class_methods($classname))) { |
|
| 474 | throw new Exception(sprintf("Class '%s' doesn't have a method named '%s'", |
||
| 475 | $classname, $method)); |
||
| 476 | } |
||
| 477 | |||
| 478 | } else if ($last >= 2 && |
||
| 479 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 480 | $ts[$last - 1]['token'] == T_STRING && |
||
| 481 | $ts[$last - 2]['token'] == T_NEW ) { |
||
| 482 | |||
| 483 | /* new Class() */ |
||
| 484 | |||
| 485 | /* don't care about this in a class ... { ... } */ |
||
| 486 | |||
| 487 | $classname = $ts[$last - 1]['value']; |
||
| 488 | |||
| 489 | if (!class_exists($classname)) { |
||
| 490 | throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname)); |
||
| 491 | } |
||
| 492 | |||
| 493 | $r = new ReflectionClass($classname); |
||
| 494 | |||
| 495 | if ($r->isAbstract()) { |
||
| 496 | throw new Exception(sprintf("Can't instantiate abstract Class '%s'", $classname)); |
||
| 497 | } |
||
| 498 | |||
| 499 | if (!$r->isInstantiable()) { |
||
| 500 | throw new Exception(sprintf('Class \'%s\' can\'t be instantiated. Is the class abstract ?', $classname)); |
||
| 501 | } |
||
| 502 | |||
| 503 | } else if ($last >= 2 && |
||
| 504 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 505 | $ts[$last - 1]['token'] == T_STRING && |
||
| 506 | $ts[$last - 2]['token'] == T_FUNCTION ) { |
||
| 507 | |||
| 508 | /* make sure we are not a in class definition */ |
||
| 509 | |||
| 510 | /* function a() */ |
||
| 511 | |||
| 512 | $func = $ts[$last - 1]['value']; |
||
| 513 | |||
| 514 | if (function_exists($func)) { |
||
| 515 | throw new Exception(sprintf('Function \'%s\' is already defined', $func)); |
||
| 516 | } |
||
| 517 | } else if ($last >= 4 && |
||
| 518 | $ts[0]['token'] == T_CLASS && |
||
| 519 | $ts[1]['token'] == T_STRING && |
||
| 520 | $ts[$last - 1]['token'] == T_STRING && |
||
| 521 | $ts[$last - 2]['token'] == T_FUNCTION ) { |
||
| 522 | |||
| 523 | /* make sure we are not a in class definition */ |
||
| 524 | |||
| 525 | /* class a { .. function a() ... } */ |
||
| 526 | |||
| 527 | $func = $ts[$last - 1]['value']; |
||
| 528 | $classname = $ts[1]['value']; |
||
| 529 | |||
| 530 | if (isset($methods[$func])) { |
||
| 531 | throw new Exception(sprintf("Can't redeclare method '%s' in Class '%s'", $func, $classname)); |
||
| 532 | } |
||
| 533 | |||
| 534 | $methods[$func] = 1; |
||
| 535 | |||
| 536 | } else if ($last >= 1 && |
||
| 537 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 538 | $ts[0]['token'] != T_ABSTRACT && /* if we are not in a class definition */ |
||
| 539 | $ts[1]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 540 | $ts[$last - 1]['token'] == T_STRING ) { |
||
| 541 | /* func() */ |
||
| 542 | $funcname = $ts[$last - 1]['value']; |
||
| 543 | |||
| 544 | if (!function_exists($funcname)) { |
||
| 545 | throw new Exception(sprintf("Function %s() doesn't exist", $funcname)); |
||
| 546 | } |
||
| 547 | } else if ($last >= 1 && |
||
| 548 | $ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 549 | $ts[$last - 1]['token'] == T_VARIABLE ) { |
||
| 550 | |||
| 551 | /* $object has to exist and has to be a object */ |
||
| 552 | $funcname = $ts[$last - 1]['value']; |
||
| 553 | |||
| 554 | View Code Duplication | if (!isset($GLOBALS[ltrim($funcname, '$')])) { |
|
| 555 | throw new Exception(sprintf('Variable \'%s\' is not set', $funcname)); |
||
| 556 | } |
||
| 557 | $func = $GLOBALS[ltrim($funcname, '$')]; |
||
| 558 | |||
| 559 | if (!function_exists($func)) { |
||
| 560 | throw new Exception(sprintf("Function %s() doesn't exist", $func)); |
||
| 561 | } |
||
| 562 | |||
| 563 | } |
||
| 564 | |||
| 565 | array_push($braces, $token); |
||
| 566 | break; |
||
| 567 | case '{': |
||
| 568 | $need_return = 0; |
||
| 569 | |||
| 570 | if ($last >= 2 && |
||
| 571 | $ts[$last - 1]['token'] == T_STRING && |
||
| 572 | $ts[$last - 2]['token'] == T_CLASS ) { |
||
| 573 | |||
| 574 | /* class name { */ |
||
| 575 | |||
| 576 | $classname = $ts[$last - 1]['value']; |
||
| 577 | |||
| 578 | if (class_exists($classname, false)) { |
||
| 579 | throw new Exception(sprintf("Class '%s' can't be redeclared", $classname)); |
||
| 580 | } |
||
| 581 | } else if ($last >= 4 && |
||
| 582 | $ts[$last - 1]['token'] == T_STRING && |
||
| 583 | $ts[$last - 2]['token'] == T_EXTENDS && |
||
| 584 | $ts[$last - 3]['token'] == T_STRING && |
||
| 585 | $ts[$last - 4]['token'] == T_CLASS ) { |
||
| 586 | |||
| 587 | /* class classname extends classname { */ |
||
| 588 | |||
| 589 | $classname = $ts[$last - 3]['value']; |
||
| 590 | $extendsname = $ts[$last - 1]['value']; |
||
| 591 | |||
| 592 | if (class_exists($classname, false)) { |
||
| 593 | throw new Exception(sprintf("Class '%s' can't be redeclared", |
||
| 594 | $classname)); |
||
| 595 | } |
||
| 596 | if (!class_exists($extendsname, true)) { |
||
| 597 | throw new Exception(sprintf("Can't extend '%s' ... from not existing Class '%s'", |
||
| 598 | $classname, $extendsname)); |
||
| 599 | } |
||
| 600 | } else if ($last >= 4 && |
||
| 601 | $ts[$last - 1]['token'] == T_STRING && |
||
| 602 | $ts[$last - 2]['token'] == T_IMPLEMENTS && |
||
| 603 | $ts[$last - 3]['token'] == T_STRING && |
||
| 604 | $ts[$last - 4]['token'] == T_CLASS ) { |
||
| 605 | |||
| 606 | /* class name implements interface { */ |
||
| 607 | |||
| 608 | $classname = $ts[$last - 3]['value']; |
||
| 609 | $implements = $ts[$last - 1]['value']; |
||
| 610 | |||
| 611 | if (class_exists($classname, false)) { |
||
| 612 | throw new Exception(sprintf("Class '%s' can't be redeclared", |
||
| 613 | $classname)); |
||
| 614 | } |
||
| 615 | if (!interface_exists($implements, false)) { |
||
| 616 | throw new Exception(sprintf("Can't implement not existing Interface '%s' for Class '%s'", |
||
| 617 | $implements, $classname)); |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | array_push($braces, $token); |
||
| 622 | break; |
||
| 623 | case '}': |
||
| 624 | $need_return = 0; |
||
| 625 | case ')': |
||
| 626 | array_pop($braces); |
||
| 627 | break; |
||
| 628 | case '[': |
||
| 629 | if ($ts[0]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 630 | $ts[0]['token'] != T_ABSTRACT && /* if we are not in a class definition */ |
||
| 631 | $ts[1]['token'] != T_CLASS && /* if we are not in a class definition */ |
||
| 632 | $ts[$last - 1]['token'] == T_VARIABLE) { |
||
| 633 | /* $a[] only works on array and string */ |
||
| 634 | |||
| 635 | /* $object has to exist and has to be a object */ |
||
| 636 | $objname = $ts[$last - 1]['value']; |
||
| 637 | |||
| 638 | View Code Duplication | if (!isset($GLOBALS[ltrim($objname, '$')])) { |
|
| 639 | throw new Exception(sprintf('Variable \'%s\' is not set', $objname)); |
||
| 640 | } |
||
| 641 | $obj = $GLOBALS[ltrim($objname, '$')]; |
||
| 642 | |||
| 643 | if (is_object($obj)) { |
||
| 644 | throw new Exception(sprintf('Objects (%s) don\'t support array access operators', $objname)); |
||
| 645 | } |
||
| 646 | } |
||
| 647 | break; |
||
| 648 | } |
||
| 649 | |||
| 650 | $eval .= $token; |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | $last = count($ts) - 1; |
||
| 655 | if ($last >= 2 && |
||
| 656 | $ts[$last - 0]['token'] == T_STRING && |
||
| 657 | $ts[$last - 1]['token'] == T_DOUBLE_COLON && |
||
| 658 | $ts[$last - 2]['token'] == T_STRING ) { |
||
| 659 | |||
| 660 | /* Class::constant */ |
||
| 661 | |||
| 662 | /* $object has to exist and has to be a object */ |
||
| 663 | $classname = $ts[$last - 2]['value']; |
||
| 664 | |||
| 665 | if (!class_exists($classname)) { |
||
| 666 | throw new Exception(sprintf('Class \'%s\' doesn\'t exist', $classname)); |
||
| 667 | } |
||
| 668 | |||
| 669 | $constname = $ts[$last - 0]['value']; |
||
| 670 | |||
| 671 | $c = new ReflectionClass($classname); |
||
| 672 | if (!$c->hasConstant($constname)) { |
||
| 673 | throw new Exception(sprintf("Class '%s' doesn't have a constant named '%s'", |
||
| 674 | $classname, $constname)); |
||
| 675 | } |
||
| 676 | } else if ($last == 0 && |
||
| 677 | $ts[$last - 0]['token'] == T_VARIABLE ) { |
||
| 678 | |||
| 679 | /* $var */ |
||
| 680 | |||
| 681 | $varname = $ts[$last - 0]['value']; |
||
| 682 | |||
| 683 | View Code Duplication | if (!isset($GLOBALS[ltrim($varname, '$')])) { |
|
| 684 | throw new Exception(sprintf('Variable \'%s\' is not set', $varname)); |
||
| 685 | } |
||
| 686 | } |
||
| 687 | |||
| 688 | |||
| 689 | $need_more = (count($braces) > 0) || $open_comment; |
||
| 690 | |||
| 691 | if ($need_more || ';' === $token) { |
||
| 692 | $need_semicolon = 0; |
||
| 693 | } |
||
| 694 | |||
| 695 | if ($need_return) { |
||
| 696 | $eval = "return ".$eval; |
||
| 697 | } |
||
| 698 | |||
| 699 | /* add a traling ; if necessary */ |
||
| 700 | if ($need_semicolon) |
||
| 701 | { |
||
| 702 | $this->has_semicolon = preg_match('/;\s*$/', $eval); |
||
| 703 | $eval .= ';'; |
||
| 704 | } |
||
| 705 | |||
| 706 | if (!$need_more) { |
||
| 707 | $this->code = $eval; |
||
| 708 | } |
||
| 709 | |||
| 710 | return $need_more; |
||
| 711 | } |
||
| 712 | |||
| 1092 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: