PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/pdepend/pdepend/src/main/php/PDepend/Util/IdBuilder.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 162 lines | 56 code | 12 blank | 94 comment | 2 complexity | 05e66e52a3ca57847247d13453d209fc MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PDepend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2015, 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. * @copyright 2008-2015 Manuel Pichler. All rights reserved.
  40. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  41. * @since 0.9.12
  42. */
  43. namespace PDepend\Util;
  44. use PDepend\Source\AST\AbstractASTArtifact;
  45. use PDepend\Source\AST\AbstractASTType;
  46. use PDepend\Source\AST\ASTCompilationUnit;
  47. use PDepend\Source\AST\ASTFunction;
  48. use PDepend\Source\AST\ASTMethod;
  49. /**
  50. * This class provides methods to generate unique, but reproducable identifiers
  51. * for nodes generated during the parsing process.
  52. *
  53. * @copyright 2008-2015 Manuel Pichler. All rights reserved.
  54. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  55. * @since 0.9.12
  56. */
  57. class IdBuilder
  58. {
  59. /**
  60. * Generates an identifier for the given file instance.
  61. *
  62. * @param \PDepend\Source\AST\ASTCompilationUnit $compilationUnit
  63. * @return string
  64. */
  65. public function forFile(ASTCompilationUnit $compilationUnit)
  66. {
  67. return $this->hash($compilationUnit->getFileName());
  68. }
  69. /**
  70. * Generates an identifier for the given function instance.
  71. *
  72. * @param \PDepend\Source\AST\ASTFunction $function
  73. * @return string
  74. */
  75. public function forFunction(ASTFunction $function)
  76. {
  77. return $this->forOffsetItem($function, 'function');
  78. }
  79. /**
  80. * Generates an identifier for the given class, interface or trait instance.
  81. *
  82. * @param \PDepend\Source\AST\AbstractASTType $type
  83. * @return string
  84. */
  85. public function forClassOrInterface(AbstractASTType $type)
  86. {
  87. return $this->forOffsetItem(
  88. $type,
  89. ltrim(strrchr(strtolower(get_class($type)), '_'), '_')
  90. );
  91. }
  92. /**
  93. * Generates an identifier for the given source item.
  94. *
  95. * @param \PDepend\Source\AST\AbstractASTArtifact $artifact
  96. * @param string $prefix The item type identifier.
  97. * @return string
  98. */
  99. protected function forOffsetItem(AbstractASTArtifact $artifact, $prefix)
  100. {
  101. $fileHash = $artifact->getCompilationUnit()->getId();
  102. $itemHash = $this->hash($prefix . ':' . strtolower($artifact->getName()));
  103. $offset = $this->getOffsetInFile($fileHash, $itemHash);
  104. return sprintf('%s-%s-%s', $fileHash, $itemHash, $offset);
  105. }
  106. /**
  107. * Generates an identifier for the given method instance.
  108. *
  109. * @param \PDepend\Source\AST\ASTMethod $method
  110. * @return string
  111. */
  112. public function forMethod(ASTMethod $method)
  113. {
  114. return sprintf(
  115. '%s-%s',
  116. $method->getParent()->getId(),
  117. $this->hash(strtolower($method->getName()))
  118. );
  119. }
  120. /**
  121. * Creates a base 36 hash for the given string.
  122. *
  123. * @param string $string The raw input identifier/string.
  124. * @return string
  125. */
  126. protected function hash($string)
  127. {
  128. return substr(base_convert(md5($string), 16, 36), 0, 11);
  129. }
  130. /**
  131. * Returns the node offset/occurence of the given <b>$string</b> within a
  132. * file.
  133. *
  134. * @param string $file The file identifier.
  135. * @param string $string The node identifier.
  136. * @return string
  137. */
  138. protected function getOffsetInFile($file, $string)
  139. {
  140. if (isset($this->offsetInFile[$file][$string])) {
  141. $this->offsetInFile[$file][$string]++;
  142. } else {
  143. $this->offsetInFile[$file][$string] = 0;
  144. }
  145. return sprintf(
  146. '%02s',
  147. base_convert($this->offsetInFile[$file][$string], 10, 36)
  148. );
  149. }
  150. }