PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP/Depend/Util/UuidBuilder.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 166 lines | 43 code | 10 blank | 113 comment | 1 complexity | c4a48c62510773a7b2ef1f6fd2b07c8f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2010, 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 Util
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2010 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. * @since 0.9.12
  48. */
  49. /**
  50. * This class provides methods to generate unique, but reproducable identifiers
  51. * for nodes generated during the parsing process.
  52. *
  53. * @category PHP
  54. * @package PHP_Depend
  55. * @subpackage Util
  56. * @author Manuel Pichler <mapi@pdepend.org>
  57. * @copyright 2008-2010 Manuel Pichler. All rights reserved.
  58. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  59. * @version Release: 0.9.19
  60. * @link http://www.pdepend.org/
  61. * @since 0.9.12
  62. */
  63. class PHP_Depend_Util_UuidBuilder
  64. {
  65. /**
  66. * Generates an identifier for the given file instance.
  67. *
  68. * @param PHP_Depend_Code_File $file The context source file instance.
  69. *
  70. * @return string
  71. */
  72. public function forFile(PHP_Depend_Code_File $file)
  73. {
  74. return $this->hash($file->getFileName());
  75. }
  76. /**
  77. * Generates an identifier for the given function instance.
  78. *
  79. * @param PHP_Depend_Code_Function $function The context function instance.
  80. *
  81. * @return string
  82. */
  83. public function forFunction(PHP_Depend_Code_Function $function)
  84. {
  85. return $this->forOffsetItem($function, 'function');
  86. }
  87. /**
  88. * Generates an identifier for the given class or interface instance.
  89. *
  90. * @param PHP_Depend_Code_AbstractClassOrInterface $class A class instance.
  91. *
  92. * @return string
  93. */
  94. public function forClassOrInterface(PHP_Depend_Code_AbstractClassOrInterface $class)
  95. {
  96. return $this->forOffsetItem($class, 'class');
  97. }
  98. /**
  99. * Generates an identifier for the given source item.
  100. *
  101. * @param PHP_Depend_Code_AbstractItem $item The context source item.
  102. * @param string $prefix The item type identifier.
  103. *
  104. * @return string
  105. */
  106. protected function forOffsetItem(PHP_Depend_Code_AbstractItem $item, $prefix)
  107. {
  108. $fileHash = $item->getSourceFile()->getUUID();
  109. $itemHash = $this->hash($prefix . ':' . $item->getName());
  110. $offset = $this->getOffsetInFile($fileHash, $itemHash);
  111. return sprintf('%s-%s-%s', $fileHash, $itemHash, $offset);
  112. }
  113. /**
  114. * Generates an identifier for the given method instance.
  115. *
  116. * @param PHP_Depend_Code_Method $method A method instance.
  117. *
  118. * @return string
  119. */
  120. public function forMethod(PHP_Depend_Code_Method $method)
  121. {
  122. return sprintf('%s-%s', $method->getParent()->getUUID(), $this->hash($method->getName()));
  123. }
  124. /**
  125. * Creates a base 36 hash for the given string.
  126. *
  127. * @param string $string The raw input identifier/string.
  128. *
  129. * @return string
  130. */
  131. protected function hash($string)
  132. {
  133. return substr(base_convert(md5($string), 16, 36), 0, 11);
  134. }
  135. /**
  136. * Returns the node offset/occurence of the given <b>$string</b> within a
  137. * file.
  138. *
  139. * @param string $file The file identifier.
  140. * @param string $string The node identifier.
  141. *
  142. * @return string
  143. */
  144. protected function getOffsetInFile($file, $string)
  145. {
  146. if (isset($this->_offsetInFile[$file][$string]))
  147. {
  148. $this->_offsetInFile[$file][$string] ++;
  149. }
  150. else
  151. {
  152. $this->_offsetInFile[$file][$string] = 0;
  153. }
  154. return sprintf('%02s', base_convert($this->_offsetInFile[$file][$string], 10, 36));
  155. }
  156. }