/tests/lib/PHP/Depend/Parser/SymbolTable.php

https://github.com/timglabisch/pimcore · PHP · 152 lines · 40 code · 9 blank · 103 comment · 4 complexity · 5da69dcbcea09b2e8bb25b143dc04aa4 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2012, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category PHP
  40. * @package PHP_Depend
  41. * @subpackage Parser
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2012 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://www.pdepend.org/
  47. */
  48. /**
  49. * This class provides a simple hashmap for name mappings done by the parser.
  50. *
  51. * @category PHP
  52. * @package PHP_Depend
  53. * @subpackage Parser
  54. * @author Manuel Pichler <mapi@pdepend.org>
  55. * @copyright 2008-2012 Manuel Pichler. All rights reserved.
  56. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  57. * @version Release: 1.1.0
  58. * @link http://www.pdepend.org/
  59. */
  60. class PHP_Depend_Parser_SymbolTable
  61. {
  62. /**
  63. * Stack with all active scopes.
  64. *
  65. * @var array(array) $_scopeStack
  66. */
  67. private $scopeStack = array();
  68. /**
  69. * The currently active scope.
  70. *
  71. * @var array(string=>string) $_scope
  72. */
  73. private $scope = array();
  74. /**
  75. * This method creates a new scope.
  76. *
  77. * @return void
  78. */
  79. public function createScope()
  80. {
  81. // Add copy of last scope as new scope
  82. array_push($this->scopeStack, $this->scope);
  83. }
  84. /**
  85. * This method destorys the top most scope.
  86. *
  87. * @return void
  88. */
  89. public function destroyScope()
  90. {
  91. // Remove scope from stack
  92. array_pop($this->scopeStack);
  93. // Update current scope to latest in stack
  94. $this->scope = end($this->scopeStack);
  95. }
  96. /**
  97. * Adds a new value to the top most scope.
  98. *
  99. * @param string $key The key of this scope value.
  100. * @param mixed $value A new scope value.
  101. *
  102. * @return void
  103. */
  104. public function add($key, $value)
  105. {
  106. if (is_array($this->scope) === false) {
  107. throw new UnderflowException('No active scope.');
  108. }
  109. $this->scope[strtolower($key)] = $value;
  110. }
  111. /**
  112. * Resets the current scope
  113. *
  114. * @return void
  115. */
  116. public function resetScope()
  117. {
  118. if (is_array($this->scope) === false) {
  119. throw new UnderflowException('No active scope.');
  120. }
  121. $this->scope = array();
  122. }
  123. /**
  124. * This method will return the registered value for the given key, when it
  125. * exists in the current scope. The returned value will <b>null</b> if no
  126. * value exists for the given key.
  127. *
  128. * @param string $key The key for a searched scope value.
  129. *
  130. * @return mixed
  131. */
  132. public function lookup($key)
  133. {
  134. if (is_array($this->scope) === false) {
  135. throw new UnderflowException('No active scope.');
  136. }
  137. $key = strtolower($key);
  138. if (isset($this->scope[$key])) {
  139. return $this->scope[$key];
  140. }
  141. return null;
  142. }
  143. }