LimeLexerCodeLines.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * Processes a source file for the lines of code and returns the line numbers.
  13. *
  14. * The following rules apply for detecting LOC and are conformant with
  15. * xdebug_get_code_coverage():
  16. *
  17. * * identifier in function declaration == LOC
  18. * * class declaration != LOC
  19. * * method declaration != LOC
  20. * * property declaration != LOC
  21. * * } == LOC
  22. * * { != LOC
  23. * * { after class declaration == LOC
  24. *
  25. * @package Lime
  26. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  27. * @version SVN: $Id: LimeLexerCodeLines.php 23701 2009-11-08 21:23:40Z bschussek $
  28. */
  29. class LimeLexerCodeLines extends LimeLexer
  30. {
  31. private
  32. $lines = array();
  33. /**
  34. * (non-PHPdoc)
  35. * @see lexer/LimeLexer#parse($content)
  36. */
  37. public function parse($content)
  38. {
  39. $this->lines = array();
  40. return parent::parse($content);
  41. }
  42. /**
  43. * (non-PHPdoc)
  44. * @see lexer/LimeLexer#process($text, $id)
  45. */
  46. protected function process($text, $id)
  47. {
  48. // whitespace is ignored
  49. if ($id == T_WHITESPACE)
  50. {
  51. return;
  52. }
  53. // PHP tags are ignored
  54. else if ($id == T_OPEN_TAG || $id == T_CLOSE_TAG)
  55. {
  56. return;
  57. }
  58. // class declarations are ignored
  59. else if ($this->inClassDeclaration())
  60. {
  61. return;
  62. }
  63. // function declarations are ignored, except for the identifier
  64. else if ($this->inFunctionDeclaration() && $id != T_STRING)
  65. {
  66. return;
  67. }
  68. // method declarations are ignored
  69. else if ($this->inClass() && $this->inFunctionDeclaration())
  70. {
  71. return;
  72. }
  73. // everything in classes except function body, the { and the } of the class is ignored
  74. else if ($this->inClass() && !$this->inFunction() && $text != '{' && $text != '}')
  75. {
  76. return;
  77. }
  78. // { is ignored, except for after class declarations
  79. else if ($text == '{' && !($this->inClass() && !$this->inFunction()))
  80. {
  81. return;
  82. }
  83. $this->lines[$this->getCurrentLine()] = true;
  84. }
  85. /**
  86. * (non-PHPdoc)
  87. * @see lexer/LimeLexer#getResult()
  88. */
  89. protected function getResult()
  90. {
  91. return array_keys($this->lines);
  92. }
  93. }