PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DocBlox/Reflection/Constant.php

http://github.com/mvriel/Docblox
PHP | 131 lines | 63 code | 13 blank | 55 comment | 13 complexity | 4dde4eaa711a99e7f78a5f3050b1eb9e MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Reflection
  9. * @author Mike van Riel <mike.vanriel@naenius.com>
  10. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT
  12. * @link http://docblox-project.org
  13. */
  14. /**
  15. * Parses a constant definition.
  16. *
  17. * @category DocBlox
  18. * @package Reflection
  19. * @author Mike van Riel <mike.vanriel@naenius.com>
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT
  21. * @link http://docblox-project.org
  22. */
  23. class DocBlox_Reflection_Constant extends DocBlox_Reflection_DocBlockedAbstract
  24. {
  25. /** @var string Contains the value contained in the constant */
  26. protected $value = '';
  27. /**
  28. * Retrieves the generic information.
  29. *
  30. * Finds out what the name and value is of this constant on top of the
  31. * information found using the DocBlox_Reflection_DocBlockedAbstract parent
  32. * method.
  33. *
  34. * @param DocBlox_Reflection_TokenIterator $tokens Tokens to interpret with
  35. * the pointer at the token to be processed.
  36. *
  37. * @see DocBlox_Reflection_DocBlockedAbstract::processGenericInformation
  38. *
  39. * @return void
  40. */
  41. protected function processGenericInformation(
  42. DocBlox_Reflection_TokenIterator $tokens
  43. ) {
  44. parent::processGenericInformation($tokens);
  45. if ($tokens->current()->content == 'define') {
  46. // find the first encapsed string and strip the opening and closing
  47. // apostrophe
  48. $name_token = $tokens->gotoNextByType(
  49. T_CONSTANT_ENCAPSED_STRING, 5, array(',')
  50. );
  51. if (!$name_token) {
  52. $this->log(
  53. 'Unable to process constant in file ' . $tokens->getFilename()
  54. . ' at line ' . $tokens->current()->getLineNumber(),
  55. DocBlox_Core_Log::CRIT
  56. );
  57. return;
  58. }
  59. $this->setName(substr($name_token->content, 1, -1));
  60. // skip to after the comma
  61. while ($tokens->current()->content != ',') {
  62. if ($tokens->next() === false) {
  63. break;
  64. }
  65. }
  66. // get everything until the closing brace and use that for value,
  67. // take child parenthesis under consideration
  68. $value = '';
  69. $level = 0;
  70. while (!(($tokens->current()->content == ')') && ($level == -1))) {
  71. if ($tokens->next() === false) {
  72. break;
  73. }
  74. switch ($tokens->current()->content) {
  75. case '(':
  76. $level++;
  77. break;
  78. case ')':
  79. $level--;
  80. break;
  81. }
  82. $value .= $tokens->current()->content;
  83. }
  84. $this->setValue(trim(substr($value, 0, -1)));
  85. } else {
  86. // Added T_NAMESPACE in case anyone uses a constant name NAMESPACE
  87. // in PHP 5.2.x and tries to parse the code in 5.3.x
  88. $this->setName(
  89. $tokens->gotoNextByType(
  90. array(T_STRING, T_NAMESPACE),
  91. 10,
  92. array('=')
  93. )->content
  94. );
  95. $this->setValue($this->findDefault($tokens));
  96. }
  97. }
  98. /**
  99. * Stores the value contained in this constant.
  100. *
  101. * @param string $value String representation of the constant value.
  102. *
  103. * @return void
  104. */
  105. public function setValue($value)
  106. {
  107. $this->value = $value;
  108. }
  109. /**
  110. * Returns the value contained in this Constant.
  111. *
  112. * @return string
  113. */
  114. public function getValue()
  115. {
  116. return $this->value;
  117. }
  118. }