/branches/kaste/framework/vendor/simpletest/reflection_php5.php

https://github.com/akelos/v1 · PHP · 278 lines · 126 code · 19 blank · 133 comment · 16 complexity · 578ab7ef9f045d6d4befe605a6c2b202 MD5 · raw file

  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: reflection_php5.php,v 1.20 2006/02/05 21:39:07 lastcraft Exp $
  7. */
  8. /**
  9. * Version specific reflection API.
  10. * @package SimpleTest
  11. * @subpackage UnitTester
  12. */
  13. class SimpleReflection {
  14. var $_interface;
  15. /**
  16. * Stashes the class/interface.
  17. * @param string $interface Class or interface
  18. * to inspect.
  19. */
  20. function SimpleReflection($interface) {
  21. $this->_interface = $interface;
  22. }
  23. /**
  24. * Checks that a class has been declared. Versions
  25. * before PHP5.0.2 need a check that it's not really
  26. * an interface.
  27. * @return boolean True if defined.
  28. * @access public
  29. */
  30. function classExists() {
  31. if (! class_exists($this->_interface)) {
  32. return false;
  33. }
  34. $reflection = new ReflectionClass($this->_interface);
  35. return ! $reflection->isInterface();
  36. }
  37. /**
  38. * Needed to kill the autoload feature in PHP5
  39. * for classes created dynamically.
  40. * @return boolean True if defined.
  41. * @access public
  42. */
  43. function classExistsSansAutoload() {
  44. return class_exists($this->_interface, false);
  45. }
  46. /**
  47. * Checks that a class or interface has been
  48. * declared.
  49. * @return boolean True if defined.
  50. * @access public
  51. */
  52. function classOrInterfaceExists() {
  53. return $this->_classOrInterfaceExistsWithAutoload($this->_interface, true);
  54. }
  55. /**
  56. * Needed to kill the autoload feature in PHP5
  57. * for classes created dynamically.
  58. * @return boolean True if defined.
  59. * @access public
  60. */
  61. function classOrInterfaceExistsSansAutoload() {
  62. return $this->_classOrInterfaceExistsWithAutoload($this->_interface, false);
  63. }
  64. /**
  65. * Needed to select the autoload feature in PHP5
  66. * for classes created dynamically.
  67. * @param string $interface Class or interface name.
  68. * @param boolean $autoload True totriggerautoload.
  69. * @return boolean True if interface defined.
  70. * @access private
  71. */
  72. function _classOrInterfaceExistsWithAutoload($interface, $autoload) {
  73. if (function_exists('interface_exists')) {
  74. if (interface_exists($this->_interface, $autoload)) {
  75. return true;
  76. }
  77. }
  78. return class_exists($this->_interface, $autoload);
  79. }
  80. /**
  81. * Gets the list of methods on a class or
  82. * interface. Needs to recursively look at all of
  83. * the interfaces included.
  84. * @returns array List of method names.
  85. * @access public
  86. */
  87. function getMethods() {
  88. return array_unique(get_class_methods($this->_interface));
  89. }
  90. /**
  91. * Gets the list of interfaces from a class. If the
  92. * class name is actually an interface then just that
  93. * interface is returned.
  94. * @returns array List of interfaces.
  95. * @access public
  96. */
  97. function getInterfaces() {
  98. $reflection = new ReflectionClass($this->_interface);
  99. if ($reflection->isInterface()) {
  100. return array($this->_interface);
  101. }
  102. return $this->_onlyParents($reflection->getInterfaces());
  103. }
  104. /**
  105. * Gets the list of methods for the implemented
  106. * interfaces only.
  107. * @returns array List of enforced method signatures.
  108. * @access public
  109. */
  110. function getInterfaceMethods() {
  111. $methods = array();
  112. foreach ($this->getInterfaces() as $interface) {
  113. $methods = array_merge($methods, get_class_methods($interface));
  114. }
  115. return array_unique($methods);
  116. }
  117. /**
  118. * Checks to see if the method signature has to be tightly
  119. * specified.
  120. * @param string $method Method name.
  121. * @returns boolean True if enforced.
  122. * @access private
  123. */
  124. function _isInterfaceMethod($method) {
  125. return in_array($method, $this->getInterfaceMethods());
  126. }
  127. /**
  128. * Finds the parent class name.
  129. * @returns string Parent class name.
  130. * @access public
  131. */
  132. function getParent() {
  133. $reflection = new ReflectionClass($this->_interface);
  134. $parent = $reflection->getParentClass();
  135. if ($parent) {
  136. return $parent->getName();
  137. }
  138. return false;
  139. }
  140. /**
  141. * Determines if the class is abstract.
  142. * @returns boolean True if abstract.
  143. * @access public
  144. */
  145. function isAbstract() {
  146. $reflection = new ReflectionClass($this->_interface);
  147. return $reflection->isAbstract();
  148. }
  149. /**
  150. * Wittles a list of interfaces down to only the top
  151. * level parents.
  152. * @param array $interfaces Reflection API interfaces
  153. * to reduce.
  154. * @returns array List of parent interface names.
  155. * @access private
  156. */
  157. function _onlyParents($interfaces) {
  158. $parents = array();
  159. $blacklist = array();
  160. foreach ($interfaces as $interface) {
  161. foreach($interfaces as $possible_parent) {
  162. if ($interface->getName() == $possible_parent->getName()) {
  163. continue;
  164. }
  165. if ($interface->isSubClassOf($possible_parent)) {
  166. $blacklist[$possible_parent->getName()] = true;
  167. }
  168. }
  169. if (!isset($blacklist[$interface->getName()])) {
  170. $parents[] = $interface->getName();
  171. }
  172. }
  173. return $parents;
  174. }
  175. /**
  176. * Gets the source code matching the declaration
  177. * of a method.
  178. * @param string $name Method name.
  179. * @return string Method signature up to last
  180. * bracket.
  181. * @access public
  182. */
  183. function getSignature($name) {
  184. if ($name == '__get') {
  185. return 'function __get($key)';
  186. }
  187. if ($name == '__set') {
  188. return 'function __set($key, $value)';
  189. }
  190. if (! is_callable(array($this->_interface, $name))) {
  191. return "function $name()";
  192. }
  193. if ($this->_isInterfaceMethod($name)) {
  194. return $this->_getFullSignature($name);
  195. }
  196. return "function $name()";
  197. }
  198. /**
  199. * For a signature specified in an interface, full
  200. * details must be replicated to be a valid implementation.
  201. * @param string $name Method name.
  202. * @return string Method signature up to last
  203. * bracket.
  204. * @access private
  205. */
  206. function _getFullSignature($name) {
  207. $interface = new ReflectionClass($this->_interface);
  208. $method = $interface->getMethod($name);
  209. $reference = $method->returnsReference() ? '&' : '';
  210. return "function $reference$name(" .
  211. implode(', ', $this->_getParameterSignatures($method)) .
  212. ")";
  213. }
  214. /**
  215. * Gets the source code for each parameter.
  216. * @param ReflectionMethod $method Method object from
  217. * reflection API
  218. * @return array List of strings, each
  219. * a snippet of code.
  220. * @access private
  221. */
  222. function _getParameterSignatures($method) {
  223. $signatures = array();
  224. foreach ($method->getParameters() as $parameter) {
  225. $type = $parameter->getClass();
  226. $signatures[] =
  227. (! is_null($type) ? $type->getName() . ' ' : '') .
  228. ($parameter->isPassedByReference() ? '&' : '') .
  229. '$' . $this->_suppressSpurious($parameter->getName()) .
  230. ($this->_isOptional($parameter) ? ' = null' : '');
  231. }
  232. return $signatures;
  233. }
  234. /**
  235. * The SPL library has problems with the
  236. * Reflection library. In particular, you can
  237. * get extra characters in parameter names :(.
  238. * @param string $name Parameter name.
  239. * @return string Cleaner name.
  240. * @access private
  241. */
  242. function _suppressSpurious($name) {
  243. return str_replace(array('[', ']', ' '), '', $name);
  244. }
  245. /**
  246. * Test of a reflection parameter being optional
  247. * that works with early versions of PHP5.
  248. * @param reflectionParameter $parameter Is this optional.
  249. * @return boolean True if optional.
  250. * @access private
  251. */
  252. function _isOptional($parameter) {
  253. if (method_exists($parameter, 'isOptional')) {
  254. return $parameter->isOptional();
  255. }
  256. return false;
  257. }
  258. }
  259. ?>