reflection_php5.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: reflection_php5.php 1786 2008-04-26 17:32:20Z pp11 $
  7. */
  8. /**
  9. * Version specific reflection API.
  10. * @package SimpleTest
  11. * @subpackage UnitTester
  12. */
  13. class SimpleReflection {
  14. private $interface;
  15. /**
  16. * Stashes the class/interface.
  17. * @param string $interface Class or interface
  18. * to inspect.
  19. */
  20. function __construct($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. protected 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. protected 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. protected 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. protected 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. protected 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. protected 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. protected 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. // This wonky try-catch is a work around for a faulty method_exists()
  277. // in early versions of PHP 5 which would return false for static
  278. // methods. The Reflection classes work fine, but hasMethod()
  279. // doesn't exist prior to PHP 5.1.0, so we need to use a more crude
  280. // detection method.
  281. try {
  282. $interface = new ReflectionClass($this->interface);
  283. $interface->getMethod($name);
  284. } catch (ReflectionException $e) {
  285. return "function $name()";
  286. }
  287. return $this->getFullSignature($name);
  288. }
  289. /**
  290. * For a signature specified in an interface, full
  291. * details must be replicated to be a valid implementation.
  292. * @param string $name Method name.
  293. * @return string Method signature up to last
  294. * bracket.
  295. * @access private
  296. */
  297. protected function getFullSignature($name) {
  298. $interface = new ReflectionClass($this->interface);
  299. $method = $interface->getMethod($name);
  300. $reference = $method->returnsReference() ? '&' : '';
  301. $static = $method->isStatic() ? 'static ' : '';
  302. return "{$static}function $reference$name(" .
  303. implode(', ', $this->getParameterSignatures($method)) .
  304. ")";
  305. }
  306. /**
  307. * Gets the source code for each parameter.
  308. * @param ReflectionMethod $method Method object from
  309. * reflection API
  310. * @return array List of strings, each
  311. * a snippet of code.
  312. * @access private
  313. */
  314. protected function getParameterSignatures($method) {
  315. $signatures = array();
  316. foreach ($method->getParameters() as $parameter) {
  317. $signature = '';
  318. $type = $parameter->getClass();
  319. if (is_null($type) && version_compare(phpversion(), '5.1.0', '>=') && $parameter->isArray()) {
  320. $signature .= 'array ';
  321. } elseif (!is_null($type)) {
  322. $signature .= $type->getName() . ' ';
  323. }
  324. if ($parameter->isPassedByReference()) {
  325. $signature .= '&';
  326. }
  327. $signature .= '$' . $this->suppressSpurious($parameter->getName());
  328. if ($this->isOptional($parameter)) {
  329. $signature .= ' = null';
  330. }
  331. $signatures[] = $signature;
  332. }
  333. return $signatures;
  334. }
  335. /**
  336. * The SPL library has problems with the
  337. * Reflection library. In particular, you can
  338. * get extra characters in parameter names :(.
  339. * @param string $name Parameter name.
  340. * @return string Cleaner name.
  341. * @access private
  342. */
  343. protected function suppressSpurious($name) {
  344. return str_replace(array('[', ']', ' '), '', $name);
  345. }
  346. /**
  347. * Test of a reflection parameter being optional
  348. * that works with early versions of PHP5.
  349. * @param reflectionParameter $parameter Is this optional.
  350. * @return boolean True if optional.
  351. * @access private
  352. */
  353. protected function isOptional($parameter) {
  354. if (method_exists($parameter, 'isOptional')) {
  355. return $parameter->isOptional();
  356. }
  357. return false;
  358. }
  359. }
  360. ?>