PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 3ms app.codeStats 0ms

/lib/limb/tests_runner/lib/simpletest/reflection_php5.php

https://github.com/limb-php-framework/limb-app-buildman
PHP | 275 lines | 123 code | 19 blank | 133 comment | 15 complexity | 33c779b43208a1befdb75a018e666d56 MD5 | raw file
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: reflection_php5.php 4378 2006-10-27 10:04:53Z pachanga $
  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. foreach ($interfaces as $interface) {
  160. foreach($interfaces as $possible_parent) {
  161. if ($interface->getName() == $possible_parent->getName()) {
  162. continue;
  163. }
  164. if ($interface->isSubClassOf($possible_parent)) {
  165. break;
  166. }
  167. }
  168. $parents[] = $interface->getName();
  169. }
  170. return $parents;
  171. }
  172. /**
  173. * Gets the source code matching the declaration
  174. * of a method.
  175. * @param string $name Method name.
  176. * @return string Method signature up to last
  177. * bracket.
  178. * @access public
  179. */
  180. function getSignature($name) {
  181. if ($name == '__get') {
  182. return 'function __get($key)';
  183. }
  184. if ($name == '__set') {
  185. return 'function __set($key, $value)';
  186. }
  187. if (! is_callable(array($this->_interface, $name))) {
  188. return "function $name()";
  189. }
  190. if ($this->_isInterfaceMethod($name)) {
  191. return $this->_getFullSignature($name);
  192. }
  193. return "function $name()";
  194. }
  195. /**
  196. * For a signature specified in an interface, full
  197. * details must be replicated to be a valid implementation.
  198. * @param string $name Method name.
  199. * @return string Method signature up to last
  200. * bracket.
  201. * @access private
  202. */
  203. function _getFullSignature($name) {
  204. $interface = new ReflectionClass($this->_interface);
  205. $method = $interface->getMethod($name);
  206. $reference = $method->returnsReference() ? '&' : '';
  207. return "function $reference$name(" .
  208. implode(', ', $this->_getParameterSignatures($method)) .
  209. ")";
  210. }
  211. /**
  212. * Gets the source code for each parameter.
  213. * @param ReflectionMethod $method Method object from
  214. * reflection API
  215. * @return array List of strings, each
  216. * a snippet of code.
  217. * @access private
  218. */
  219. function _getParameterSignatures($method) {
  220. $signatures = array();
  221. foreach ($method->getParameters() as $parameter) {
  222. $type = $parameter->getClass();
  223. $signatures[] =
  224. (! is_null($type) ? $type->getName() . ' ' : '') .
  225. ($parameter->isPassedByReference() ? '&' : '') .
  226. '$' . $this->_suppressSpurious($parameter->getName()) .
  227. ($this->_isOptional($parameter) ? ' = null' : '');
  228. }
  229. return $signatures;
  230. }
  231. /**
  232. * The SPL library has problems with the
  233. * Reflection library. In particular, you can
  234. * get extra characters in parameter names :(.
  235. * @param string $name Parameter name.
  236. * @return string Cleaner name.
  237. * @access private
  238. */
  239. function _suppressSpurious($name) {
  240. return str_replace(array('[', ']', ' '), '', $name);
  241. }
  242. /**
  243. * Test of a reflection parameter being optional
  244. * that works with early versions of PHP5.
  245. * @param reflectionParameter $parameter Is this optional.
  246. * @return boolean True if optional.
  247. * @access private
  248. */
  249. function _isOptional($parameter) {
  250. if (method_exists($parameter, 'isOptional')) {
  251. return $parameter->isOptional();
  252. }
  253. return false;
  254. }
  255. }
  256. ?>