PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletestlib/reflection_php5.php

http://github.com/moodle/moodle
PHP | 380 lines | 188 code | 25 blank | 167 comment | 33 complexity | 7084a47eef733329709ab596d7e42927 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id$
  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.
  83. * @returns array List of method names.
  84. * @access public
  85. */
  86. function getMethods() {
  87. return array_unique(get_class_methods($this->_interface));
  88. }
  89. /**
  90. * Gets the list of interfaces from a class. If the
  91. * class name is actually an interface then just that
  92. * interface is returned.
  93. * @returns array List of interfaces.
  94. * @access public
  95. */
  96. function getInterfaces() {
  97. $reflection = new ReflectionClass($this->_interface);
  98. if ($reflection->isInterface()) {
  99. return array($this->_interface);
  100. }
  101. return $this->_onlyParents($reflection->getInterfaces());
  102. }
  103. /**
  104. * Gets the list of methods for the implemented
  105. * interfaces only.
  106. * @returns array List of enforced method signatures.
  107. * @access public
  108. */
  109. function getInterfaceMethods() {
  110. $methods = array();
  111. foreach ($this->getInterfaces() as $interface) {
  112. $methods = array_merge($methods, get_class_methods($interface));
  113. }
  114. return array_unique($methods);
  115. }
  116. /**
  117. * Checks to see if the method signature has to be tightly
  118. * specified.
  119. * @param string $method Method name.
  120. * @returns boolean True if enforced.
  121. * @access private
  122. */
  123. function _isInterfaceMethod($method) {
  124. return in_array($method, $this->getInterfaceMethods());
  125. }
  126. /**
  127. * Finds the parent class name.
  128. * @returns string Parent class name.
  129. * @access public
  130. */
  131. function getParent() {
  132. $reflection = new ReflectionClass($this->_interface);
  133. $parent = $reflection->getParentClass();
  134. if ($parent) {
  135. return $parent->getName();
  136. }
  137. return false;
  138. }
  139. /**
  140. * Trivially determines if the class is abstract.
  141. * @returns boolean True if abstract.
  142. * @access public
  143. */
  144. function isAbstract() {
  145. $reflection = new ReflectionClass($this->_interface);
  146. return $reflection->isAbstract();
  147. }
  148. /**
  149. * Trivially determines if the class is an interface.
  150. * @returns boolean True if interface.
  151. * @access public
  152. */
  153. function isInterface() {
  154. $reflection = new ReflectionClass($this->_interface);
  155. return $reflection->isInterface();
  156. }
  157. /**
  158. * Scans for final methods, as they screw up inherited
  159. * mocks by not allowing you to override them.
  160. * @returns boolean True if the class has a final method.
  161. * @access public
  162. */
  163. function hasFinal() {
  164. $reflection = new ReflectionClass($this->_interface);
  165. foreach ($reflection->getMethods() as $method) {
  166. if ($method->isFinal()) {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. /**
  173. * Whittles a list of interfaces down to only the
  174. * necessary top level parents.
  175. * @param array $interfaces Reflection API interfaces
  176. * to reduce.
  177. * @returns array List of parent interface names.
  178. * @access private
  179. */
  180. function _onlyParents($interfaces) {
  181. $parents = array();
  182. $blacklist = array();
  183. foreach ($interfaces as $interface) {
  184. foreach($interfaces as $possible_parent) {
  185. if ($interface->getName() == $possible_parent->getName()) {
  186. continue;
  187. }
  188. if ($interface->isSubClassOf($possible_parent)) {
  189. $blacklist[$possible_parent->getName()] = true;
  190. }
  191. }
  192. if (!isset($blacklist[$interface->getName()])) {
  193. $parents[] = $interface->getName();
  194. }
  195. }
  196. return $parents;
  197. }
  198. /**
  199. * Checks whether a method is abstract or not.
  200. * @param string $name Method name.
  201. * @return bool true if method is abstract, else false
  202. * @access private
  203. */
  204. function _isAbstractMethod($name) {
  205. $interface = new ReflectionClass($this->_interface);
  206. if (! $interface->hasMethod($name)) {
  207. return false;
  208. }
  209. return $interface->getMethod($name)->isAbstract();
  210. }
  211. /**
  212. * Checks whether a method is the constructor.
  213. * @param string $name Method name.
  214. * @return bool true if method is the constructor
  215. * @access private
  216. */
  217. function _isConstructor($name) {
  218. return ($name == '__construct') || ($name == $this->_interface);
  219. }
  220. /**
  221. * Checks whether a method is abstract in all parents or not.
  222. * @param string $name Method name.
  223. * @return bool true if method is abstract in parent, else false
  224. * @access private
  225. */
  226. function _isAbstractMethodInParents($name) {
  227. $interface = new ReflectionClass($this->_interface);
  228. $parent = $interface->getParentClass();
  229. while($parent) {
  230. if (! $parent->hasMethod($name)) {
  231. return false;
  232. }
  233. if ($parent->getMethod($name)->isAbstract()) {
  234. return true;
  235. }
  236. $parent = $parent->getParentClass();
  237. }
  238. return false;
  239. }
  240. /**
  241. * Checks whether a method is static or not.
  242. * @param string $name Method name
  243. * @return bool true if method is static, else false
  244. * @access private
  245. */
  246. function _isStaticMethod($name) {
  247. $interface = new ReflectionClass($this->_interface);
  248. if (! $interface->hasMethod($name)) {
  249. return false;
  250. }
  251. return $interface->getMethod($name)->isStatic();
  252. }
  253. /**
  254. * Writes the source code matching the declaration
  255. * of a method.
  256. * @param string $name Method name.
  257. * @return string Method signature up to last
  258. * bracket.
  259. * @access public
  260. */
  261. function getSignature($name) {
  262. if ($name == '__set') {
  263. return 'function __set($key, $value)';
  264. }
  265. if ($name == '__call') {
  266. return 'function __call($method, $arguments)';
  267. }
  268. if (version_compare(phpversion(), '5.1.0', '>=')) {
  269. if (in_array($name, array('__get', '__isset', $name == '__unset'))) {
  270. return "function {$name}(\$key)";
  271. }
  272. }
  273. if ($name == '__toString') {
  274. return "function $name()";
  275. }
  276. if ($this->_isInterfaceMethod($name) ||
  277. $this->_isAbstractMethod($name) ||
  278. $this->_isAbstractMethodInParents($name) ||
  279. $this->_isStaticMethod($name)) {
  280. return $this->_getFullSignature($name);
  281. }
  282. return "function $name()";
  283. }
  284. /**
  285. * For a signature specified in an interface, full
  286. * details must be replicated to be a valid implementation.
  287. * @param string $name Method name.
  288. * @return string Method signature up to last
  289. * bracket.
  290. * @access private
  291. */
  292. function _getFullSignature($name) {
  293. $interface = new ReflectionClass($this->_interface);
  294. $method = $interface->getMethod($name);
  295. $reference = $method->returnsReference() ? '&' : '';
  296. $static = $method->isStatic() ? 'static ' : '';
  297. return "{$static}function $reference$name(" .
  298. implode(', ', $this->_getParameterSignatures($method)) .
  299. ")";
  300. }
  301. /**
  302. * Gets the source code for each parameter.
  303. * @param ReflectionMethod $method Method object from
  304. * reflection API
  305. * @return array List of strings, each
  306. * a snippet of code.
  307. * @access private
  308. */
  309. function _getParameterSignatures($method) {
  310. $signatures = array();
  311. foreach ($method->getParameters() as $parameter) {
  312. $signature = '';
  313. $type = $parameter->getClass();
  314. if (is_null($type) && version_compare(phpversion(), '5.1.0', '>=') && $parameter->isArray()) {
  315. $signature .= 'array ';
  316. } elseif (!is_null($type)) {
  317. $signature .= $type->getName() . ' ';
  318. }
  319. if ($parameter->isPassedByReference()) {
  320. $signature .= '&';
  321. }
  322. $signature .= '$' . $this->_suppressSpurious($parameter->getName());
  323. if ($this->_isOptional($parameter)) {
  324. $signature .= ' = null';
  325. }
  326. $signatures[] = $signature;
  327. }
  328. return $signatures;
  329. }
  330. /**
  331. * The SPL library has problems with the
  332. * Reflection library. In particular, you can
  333. * get extra characters in parameter names :(.
  334. * @param string $name Parameter name.
  335. * @return string Cleaner name.
  336. * @access private
  337. */
  338. function _suppressSpurious($name) {
  339. return str_replace(array('[', ']', ' '), '', $name);
  340. }
  341. /**
  342. * Test of a reflection parameter being optional
  343. * that works with early versions of PHP5.
  344. * @param reflectionParameter $parameter Is this optional.
  345. * @return boolean True if optional.
  346. * @access private
  347. */
  348. function _isOptional($parameter) {
  349. if (method_exists($parameter, 'isOptional')) {
  350. return $parameter->isOptional();
  351. }
  352. return false;
  353. }
  354. }
  355. ?>