PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/phing/tasks/ext/phpunit/PHPUnitUtil.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 144 lines | 70 code | 19 blank | 55 comment | 8 complexity | d63e7ca06eb0eecae4750deb519e4e32 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. * $Id: PHPUnitUtil.php 792 2010-06-24 12:56:45Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. /**
  22. * Various utility functions
  23. *
  24. * @author Michiel Rook <michiel.rook@gmail.com>
  25. * @version $Id: PHPUnitUtil.php 792 2010-06-24 12:56:45Z mrook $
  26. * @package phing.tasks.ext.phpunit
  27. * @since 2.1.0
  28. */
  29. class PHPUnitUtil
  30. {
  31. protected static $definedClasses = array();
  32. /**
  33. * Returns the package of a class as defined in the docblock of the class using @package
  34. *
  35. * @param string the name of the class
  36. * @return string the name of the package
  37. */
  38. static function getPackageName($classname)
  39. {
  40. $reflect = new ReflectionClass($classname);
  41. if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches))
  42. {
  43. return $matches[1];
  44. }
  45. else
  46. {
  47. return "default";
  48. }
  49. }
  50. /**
  51. * Returns the subpackage of a class as defined in the docblock of the class
  52. * using @subpackage
  53. *
  54. * @param string $classname the name of the class
  55. *
  56. * @author Benjamin Schultz <bschultz@proqrent.de>
  57. * @return string|null the name of the subpackage
  58. */
  59. public static function getSubpackageName($classname)
  60. {
  61. $reflect = new ReflectionClass($classname);
  62. if (preg_match('/@subpackage[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches))
  63. {
  64. return $matches[1];
  65. }
  66. else
  67. {
  68. return null;
  69. }
  70. }
  71. /**
  72. * Derives the classname from a filename.
  73. * Assumes that there is only one class defined in that particular file, and that
  74. * the naming follows the dot-path (Java) notation scheme.
  75. *
  76. * @param string the filename
  77. * @return string the name fo the class
  78. */
  79. public static function getClassFromFileName($filename)
  80. {
  81. $filename = basename($filename);
  82. $rpos = strrpos($filename, '.');
  83. if ($rpos != - 1)
  84. {
  85. $filename = substr($filename, 0, $rpos);
  86. }
  87. return $filename;
  88. }
  89. /**
  90. * @param string the filename
  91. * @param Path optional classpath
  92. * @return array list of classes defined in the file
  93. */
  94. public static function getDefinedClasses($filename, $classpath = NULL)
  95. {
  96. $filename = realpath($filename);
  97. if (! file_exists($filename))
  98. {
  99. throw new Exception("File '" . $filename . "' does not exist");
  100. }
  101. if (isset(self :: $definedClasses[$filename]))
  102. {
  103. return self :: $definedClasses[$filename];
  104. }
  105. Phing :: __import($filename, $classpath);
  106. $declaredClasses = get_declared_classes();
  107. foreach ($declaredClasses as $classname)
  108. {
  109. $reflect = new ReflectionClass($classname);
  110. self :: $definedClasses[$reflect->getFilename()][] = $classname;
  111. if (is_array(self :: $definedClasses[$reflect->getFilename()]))
  112. {
  113. self :: $definedClasses[$reflect->getFilename()] = array_unique(self :: $definedClasses[$reflect->getFilename()]);
  114. }
  115. }
  116. if (isset(self :: $definedClasses[$filename]))
  117. {
  118. return self :: $definedClasses[$filename];
  119. }
  120. else
  121. {
  122. return array();
  123. }
  124. }
  125. }