PageRenderTime 25ms CodeModel.GetById 50ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/simpletest/reflection_php5.php

https://github.com/klando/pgpiwik
PHP | 366 lines | 181 code | 24 blank | 161 comment | 30 complexity | 88b80b6092d9fbed247c120a4f5f2af6 MD5 | raw file
Possible License(s): 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 abstract in parent or not.
  213. * @param string $name Method name.
  214. * @return bool true if method is abstract in parent, else false
  215. * @access private
  216. */
  217. function _isAbstractMethodInParent($name) {
  218. $interface = new ReflectionClass($this->_interface);
  219. if (! $parent = $interface->getParentClass()) {
  220. return false;
  221. }
  222. if (! $parent->hasMethod($name)) {
  223. return false;
  224. }
  225. return $parent->getMethod($name)->isAbstract();
  226. }
  227. /**
  228. * Checks whether a method is static or not.
  229. * @param string $name Method name
  230. * @return bool true if method is static, else false
  231. * @access private
  232. */
  233. function _isStaticMethod($name) {
  234. $interface = new ReflectionClass($this->_interface);
  235. if (! $interface->hasMethod($name)) {
  236. return false;
  237. }
  238. return $interface->getMethod($name)->isStatic();
  239. }
  240. /**
  241. * Gets the source code matching the declaration
  242. * of a method.
  243. * @param string $name Method name.
  244. * @return string Method signature up to last
  245. * bracket.
  246. * @access public
  247. */
  248. function getSignature($name) {
  249. if ($name == '__set') {
  250. return 'function __set($key, $value)';
  251. }
  252. if ($name == '__call') {
  253. return 'function __call($method, $arguments)';
  254. }
  255. if (version_compare(phpversion(), '5.1.0', '>=')) {
  256. if (in_array($name, array('__get', '__isset', $name == '__unset'))) {
  257. return "function {$name}(\$key)";
  258. }
  259. }
  260. if (! is_callable(array($this->_interface, $name)) && ! $this->_isAbstractMethod($name)) {
  261. return "function $name()";
  262. }
  263. if ($this->_isInterfaceMethod($name) ||
  264. $this->_isAbstractMethod($name) ||
  265. $this->_isAbstractMethodInParent($name) ||
  266. $this->_isStaticMethod($name)) {
  267. return $this->_getFullSignature($name);
  268. }
  269. return "function $name()";
  270. }
  271. /**
  272. * For a signature specified in an interface, full
  273. * details must be replicated to be a valid implementation.
  274. * @param string $name Method name.
  275. * @return string Method signature up to last
  276. * bracket.
  277. * @access private
  278. */
  279. function _getFullSignature($name) {
  280. $interface = new ReflectionClass($this->_interface);
  281. $method = $interface->getMethod($name);
  282. $reference = $method->returnsReference() ? '&' : '';
  283. $static = $method->isStatic() ? 'static ' : '';
  284. return "{$static}function $reference$name(" .
  285. implode(', ', $this->_getParameterSignatures($method)) .
  286. ")";
  287. }
  288. /**
  289. * Gets the source code for each parameter.
  290. * @param ReflectionMethod $method Method object from
  291. * reflection API
  292. * @return array List of strings, each
  293. * a snippet of code.
  294. * @access private
  295. */
  296. function _getParameterSignatures($method) {
  297. $signatures = array();
  298. foreach ($method->getParameters() as $parameter) {
  299. $signature = '';
  300. $type = $parameter->getClass();
  301. if (is_null($type) && version_compare(phpversion(), '5.1.0', '>=') && $parameter->isArray()) {
  302. $signature .= 'array ';
  303. } elseif (!is_null($type)) {
  304. $signature .= $type->getName() . ' ';
  305. }
  306. if ($parameter->isPassedByReference()) {
  307. $signature .= '&';
  308. }
  309. $signature .= '$' . $this->_suppressSpurious($parameter->getName());
  310. if ($this->_isOptional($parameter)) {
  311. $signature .= ' = null';
  312. }
  313. $signatures[] = $signature;
  314. }
  315. return $signatures;
  316. }
  317. /**
  318. * The SPL library has problems with the
  319. * Reflection library. In particular, you can
  320. * get extra characters in parameter names :(.
  321. * @param string $name Parameter name.
  322. * @return string Cleaner name.
  323. * @access private
  324. */
  325. function _suppressSpurious($name) {
  326. return str_replace(array('[', ']', ' '), '', $name);
  327. }
  328. /**
  329. * Test of a reflection parameter being optional
  330. * that works with early versions of PHP5.
  331. * @param reflectionParameter $parameter Is this optional.
  332. * @return boolean True if optional.
  333. * @access private
  334. */
  335. function _isOptional($parameter) {
  336. if (method_exists($parameter, 'isOptional')) {
  337. return $parameter->isOptional();
  338. }
  339. return false;
  340. }
  341. }
  342. ?>