PageRenderTime 24ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/pdepend/PHP/Depend/Metrics/AnalyzerClassFileSystemLocator.php

https://github.com/JohnMurray/VulnScan
PHP | 200 lines | 67 code | 18 blank | 115 comment | 7 complexity | 6f80030b0f27a469cfa54a1893b41935 MD5 | raw file
  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 QualityAssurance
  40. * @package PHP_Depend
  41. * @subpackage Metrics
  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://pdepend.org/
  47. * @since 0.9.10
  48. */
  49. require_once 'PHP/Depend/Metrics/AnalyzerClassLocator.php';
  50. /**
  51. * Locator that searches for PHP_Depend analyzers that follow the PHP_Depend
  52. * convention and are present the PHP_Depend source tree.
  53. *
  54. * @category QualityAssurance
  55. * @package PHP_Depend
  56. * @subpackage Metrics
  57. * @author Manuel Pichler <mapi@pdepend.org>
  58. * @copyright 2008-2010 Manuel Pichler. All rights reserved.
  59. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  60. * @version Release: @package_version@
  61. * @link http://pdepend.org/
  62. * @since 0.9.10
  63. */
  64. class PHP_Depend_Metrics_AnalyzerClassFileSystemLocator
  65. implements PHP_Depend_Metrics_AnalyzerClassLocator
  66. {
  67. /**
  68. * The root search directory.
  69. *
  70. * @var string
  71. */
  72. private $_classPath = null;
  73. /**
  74. * Array containing reflection classes for all found analyzer implementations.
  75. *
  76. * @var array(ReflectionClass)
  77. */
  78. private $_analyzers = null;
  79. /**
  80. * Regular expression that matches possible analyzer source files.
  81. *
  82. * @var string
  83. */
  84. private $_classRegexp = '(Metrics[/\\\\][a-zA-Z0-9_]+[/\\\\]Analyzer\.php$)';
  85. /**
  86. * Constructs a new locator instance.
  87. *
  88. * @param string $classPath The root search directory.
  89. */
  90. public function __construct($classPath = null)
  91. {
  92. if ($classPath === null) {
  93. $classPath = dirname(__FILE__) . '/../../../';
  94. }
  95. $this->_classPath = realpath($classPath) . DIRECTORY_SEPARATOR;
  96. }
  97. /**
  98. * Returns an array with reflection instances for all analyzer classes.
  99. *
  100. * @return array(ReflectionClass)
  101. */
  102. public function findAll()
  103. {
  104. if ($this->_analyzers === null) {
  105. $this->_analyzers = $this->_find();
  106. }
  107. return $this->_analyzers;
  108. }
  109. /**
  110. * Performs a recursive search for analyzers in the configured class path
  111. * directory.
  112. *
  113. * @return array(ReflectionClass)
  114. */
  115. private function _find()
  116. {
  117. $result = array();
  118. $paths = explode(PATH_SEPARATOR, get_include_path());
  119. foreach ($paths as $path) {
  120. $dir = $path.'/PHP/Depend/Metrics/';
  121. if (!is_dir($dir)) {
  122. continue;
  123. }
  124. $this->_classPath = $dir;
  125. $iterator = new RecursiveIteratorIterator(
  126. new RecursiveDirectoryIterator($dir)
  127. );
  128. foreach ($iterator as $file) {
  129. if ($file->getFilename() === 'Analyzer.php') {
  130. $className = $this->_createClassNameFromPath(
  131. $dir, $file->getPathname()
  132. );
  133. if (!class_exists($className)) {
  134. include_once $file->getPathname();
  135. }
  136. if ($this->_isAnalyzerClass($className)) {
  137. $result[$className] = new ReflectionClass($className);
  138. }
  139. }
  140. }
  141. }
  142. ksort($result);
  143. return array_values($result);
  144. }
  145. /**
  146. * Creates a possible analyzer class name from a given absolute file path
  147. * name.
  148. *
  149. * @param string $classPath The currently processed class path.
  150. * @param string $path Path of a possible analyzer class.
  151. *
  152. * @return string
  153. */
  154. private function _createClassNameFromPath($classPath, $path)
  155. {
  156. $localPath = substr($path, strlen($classPath), -4);
  157. return 'PHP_Depend_Metrics_' . strtr($localPath, DIRECTORY_SEPARATOR, '_');
  158. }
  159. /**
  160. * Checks if the given class name represents a valid analyzer implementation.
  161. *
  162. * @param string $className Class name of a possible analyzer implementation.
  163. *
  164. * @return boolean
  165. */
  166. private function _isAnalyzerClass($className)
  167. {
  168. return class_exists($className) && $this->_implementsInterface($className);
  169. }
  170. /**
  171. * Checks if the given class implements the analyzer interface.
  172. *
  173. * @param string $className Class name of a possible analyzer implementation.
  174. *
  175. * @return boolean
  176. */
  177. private function _implementsInterface($className)
  178. {
  179. $expectedType = 'PHP_Depend_Metrics_AnalyzerI';
  180. return in_array($expectedType, class_implements($className));
  181. }
  182. }