LimeLexerTestVariable.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Lime test framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Extracts the first global variable containing a reference to an instance of
  13. * LimeTest or any subclass from a source file.
  14. *
  15. * @package Lime
  16. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  17. * @version SVN: $Id: LimeLexerTestVariable.php 23701 2009-11-08 21:23:40Z bschussek $
  18. */
  19. class LimeLexerTestVariable extends LimeLexer
  20. {
  21. const
  22. NORMAL = 0,
  23. VARIABLE = 1,
  24. ASSIGNMENT = 2,
  25. INSTANTIATION = 3;
  26. protected
  27. $lastVariable = null,
  28. $testVariable = null,
  29. $state = self::NORMAL;
  30. /**
  31. * This method implements a turing machine for variable assignments.
  32. *
  33. * Once a variable name is caught, the object is set to state VARIABLE.
  34. * When the variable is succeeded by an assignment operator "=", the state
  35. * is set to ASSIGNMENT. If the assignment operator is succeeded by the
  36. * keyword "new", the state is set to INSTANTIATION. If the assignment
  37. * operator is succeeded by a class name that inherits class LimeTest,
  38. * processing is stopped and the variable name is returned. Otherwise,
  39. * the state is reset and processing continues.
  40. *
  41. * @see LimeLexer#process($text, $id)
  42. */
  43. protected function process($text, $id)
  44. {
  45. if ($id == T_VARIABLE && !$this->inFunction())
  46. {
  47. $this->lastVariable = $text;
  48. $this->state = self::VARIABLE;
  49. }
  50. else if ($text == '=' && $this->state == self::VARIABLE)
  51. {
  52. $this->state = self::ASSIGNMENT;
  53. }
  54. else if ($id == T_NEW && $this->state == self::ASSIGNMENT)
  55. {
  56. $this->state = self::INSTANTIATION;
  57. }
  58. else if ($id == T_STRING && $this->state == self::INSTANTIATION)
  59. {
  60. if (class_exists($text))
  61. {
  62. $class = new ReflectionClass($text);
  63. if ($text == 'LimeTest' || $class->isSubclassOf('LimeTest'))
  64. {
  65. $this->testVariable = $this->lastVariable;
  66. $this->stop();
  67. }
  68. }
  69. $this->state = self::NORMAL;
  70. }
  71. else if ($id != T_WHITESPACE)
  72. {
  73. $this->state = self::NORMAL;
  74. }
  75. }
  76. /**
  77. * (non-PHPdoc)
  78. * @see LimeLexer#getResult()
  79. */
  80. protected function getResult()
  81. {
  82. return $this->testVariable;
  83. }
  84. }