PageRenderTime 37ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/MyVenture-BlackHawk/Library/UnitTest/Include/Util/Class.php

https://github.com/samar-tmr/MyVenture
PHP | 399 lines | 254 code | 43 blank | 102 comment | 37 complexity | b79a3f7818dd646263aed8348dbff911 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Util
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.1.0
  44. */
  45. if (!defined('T_NAMESPACE')) {
  46. define('T_NAMESPACE', 377);
  47. }
  48. /**
  49. * Class helpers.
  50. *
  51. * @package PHPUnit
  52. * @subpackage Util
  53. * @author Sebastian Bergmann <sebastian@phpunit.de>
  54. * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: @package_version@
  57. * @link http://www.phpunit.de/
  58. * @since Class available since Release 3.1.0
  59. */
  60. class PHPUnit_Util_Class
  61. {
  62. protected static $buffer = array();
  63. /**
  64. * Starts the collection of loaded classes.
  65. *
  66. */
  67. public static function collectStart()
  68. {
  69. self::$buffer = get_declared_classes();
  70. }
  71. /**
  72. * Stops the collection of loaded classes and
  73. * returns the names of the loaded classes.
  74. *
  75. * @return array
  76. */
  77. public static function collectEnd()
  78. {
  79. return array_values(
  80. array_diff(get_declared_classes(), self::$buffer)
  81. );
  82. }
  83. /**
  84. * Returns the class hierarchy for a given class.
  85. *
  86. * @param string $className
  87. * @param boolean $asReflectionObjects
  88. * @return array
  89. */
  90. public static function getHierarchy($className, $asReflectionObjects = FALSE)
  91. {
  92. if ($asReflectionObjects) {
  93. $classes = array(new ReflectionClass($className));
  94. } else {
  95. $classes = array($className);
  96. }
  97. $done = FALSE;
  98. while (!$done) {
  99. if ($asReflectionObjects) {
  100. $class = new ReflectionClass(
  101. $classes[count($classes)-1]->getName()
  102. );
  103. } else {
  104. $class = new ReflectionClass($classes[count($classes)-1]);
  105. }
  106. $parent = $class->getParentClass();
  107. if ($parent !== FALSE) {
  108. if ($asReflectionObjects) {
  109. $classes[] = $parent;
  110. } else {
  111. $classes[] = $parent->getName();
  112. }
  113. } else {
  114. $done = TRUE;
  115. }
  116. }
  117. return $classes;
  118. }
  119. /**
  120. * Returns the parameters of a function or method.
  121. *
  122. * @param ReflectionFunction|ReflectionMethod $method
  123. * @param boolean $forCall
  124. * @return string
  125. * @since Method available since Release 3.2.0
  126. */
  127. public static function getMethodParameters($method, $forCall = FALSE)
  128. {
  129. $parameters = array();
  130. foreach ($method->getParameters() as $i => $parameter) {
  131. $name = '$' . $parameter->getName();
  132. if ($name === '$') {
  133. $name .= 'arg' . $i;
  134. }
  135. $default = '';
  136. $reference = '';
  137. $typeHint = '';
  138. if (!$forCall) {
  139. if ($parameter->isArray()) {
  140. $typeHint = 'array ';
  141. } else {
  142. try {
  143. $class = $parameter->getClass();
  144. }
  145. catch (ReflectionException $e) {
  146. $class = FALSE;
  147. }
  148. if ($class) {
  149. $typeHint = $class->getName() . ' ';
  150. }
  151. }
  152. if ($parameter->isDefaultValueAvailable()) {
  153. $value = $parameter->getDefaultValue();
  154. $default = ' = ' . var_export($value, TRUE);
  155. }
  156. else if ($parameter->isOptional()) {
  157. $default = ' = null';
  158. }
  159. if ($parameter->isPassedByReference()) {
  160. $reference = '&';
  161. }
  162. }
  163. $parameters[] = $typeHint . $reference . $name . $default;
  164. }
  165. return join(', ', $parameters);
  166. }
  167. /**
  168. * Returns the package information of a user-defined class.
  169. *
  170. * @param string $className
  171. * @param string $docComment
  172. * @return array
  173. */
  174. public static function getPackageInformation($className, $docComment)
  175. {
  176. $result = array(
  177. 'namespace' => '',
  178. 'fullPackage' => '',
  179. 'category' => '',
  180. 'package' => '',
  181. 'subpackage' => ''
  182. );
  183. if (strpos($className, '\\') !== FALSE) {
  184. $result['namespace'] = self::arrayToName(
  185. explode('\\', $className)
  186. );
  187. }
  188. if (preg_match('/@category[\s]+([\.\w]+)/', $docComment, $matches)) {
  189. $result['category'] = $matches[1];
  190. }
  191. if (preg_match('/@package[\s]+([\.\w]+)/', $docComment, $matches)) {
  192. $result['package'] = $matches[1];
  193. $result['fullPackage'] = $matches[1];
  194. }
  195. if (preg_match('/@subpackage[\s]+([\.\w]+)/', $docComment, $matches)) {
  196. $result['subpackage'] = $matches[1];
  197. $result['fullPackage'] .= '.' . $matches[1];
  198. }
  199. if (empty($result['fullPackage'])) {
  200. $result['fullPackage'] = self::arrayToName(
  201. explode('_', str_replace('\\', '_', $className)), '.'
  202. );
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Returns the value of a static attribute.
  208. * This also works for attributes that are declared protected or private.
  209. *
  210. * @param string $className
  211. * @param string $attributeName
  212. * @return mixed
  213. * @throws InvalidArgumentException
  214. * @since Method available since Release 3.4.0
  215. */
  216. public static function getStaticAttribute($className, $attributeName)
  217. {
  218. if (!is_string($className)) {
  219. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
  220. }
  221. if (!class_exists($className)) {
  222. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class name');
  223. }
  224. if (!is_string($attributeName)) {
  225. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  226. }
  227. $class = new ReflectionClass($className);
  228. while ($class) {
  229. $attributes = $class->getStaticProperties();
  230. if (array_key_exists($attributeName, $attributes)) {
  231. return $attributes[$attributeName];
  232. }
  233. $class = $class->getParentClass();
  234. }
  235. throw new PHPUnit_Framework_Exception(
  236. sprintf(
  237. 'Attribute "%s" not found in class.',
  238. $attributeName
  239. )
  240. );
  241. }
  242. /**
  243. * Returns the value of an object's attribute.
  244. * This also works for attributes that are declared protected or private.
  245. *
  246. * @param object $object
  247. * @param string $attributeName
  248. * @return mixed
  249. * @throws InvalidArgumentException
  250. * @since Method available since Release 3.4.0
  251. */
  252. public static function getObjectAttribute($object, $attributeName)
  253. {
  254. if (!is_object($object)) {
  255. throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'object');
  256. }
  257. if (!is_string($attributeName)) {
  258. throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
  259. }
  260. try {
  261. $attribute = new ReflectionProperty($object, $attributeName);
  262. }
  263. catch (ReflectionException $e) {
  264. $reflector = new ReflectionObject($object);
  265. while ($reflector = $reflector->getParentClass()) {
  266. try {
  267. $attribute = $reflector->getProperty($attributeName);
  268. break;
  269. }
  270. catch(ReflectionException $e) {
  271. }
  272. }
  273. }
  274. if (isset($attribute)) {
  275. if ($attribute == NULL || $attribute->isPublic()) {
  276. return $object->$attributeName;
  277. } else {
  278. $array = (array)$object;
  279. $protectedName = "\0*\0" . $attributeName;
  280. if (array_key_exists($protectedName, $array)) {
  281. return $array[$protectedName];
  282. } else {
  283. $classes = self::getHierarchy(get_class($object));
  284. foreach ($classes as $class) {
  285. $privateName = sprintf(
  286. "\0%s\0%s",
  287. $class,
  288. $attributeName
  289. );
  290. if (array_key_exists($privateName, $array)) {
  291. return $array[$privateName];
  292. }
  293. }
  294. }
  295. }
  296. }
  297. throw new PHPUnit_Framework_Exception(
  298. sprintf(
  299. 'Attribute "%s" not found in object.',
  300. $attributeName
  301. )
  302. );
  303. }
  304. /**
  305. *
  306. *
  307. * @param string $className
  308. * @return array
  309. * @since Method available since Release 3.4.0
  310. */
  311. public static function parseFullyQualifiedClassName($className)
  312. {
  313. $result = array(
  314. 'namespace' => '',
  315. 'className' => $className,
  316. 'fullyQualifiedClassName' => $className
  317. );
  318. if (strpos($className, '\\') !== FALSE) {
  319. $tmp = explode('\\', $className);
  320. $result['className'] = $tmp[count($tmp)-1];
  321. $result['namespace'] = self::arrayToName($tmp);
  322. }
  323. return $result;
  324. }
  325. /**
  326. * Returns the package information of a user-defined class.
  327. *
  328. * @param array $parts
  329. * @param string $join
  330. * @return string
  331. * @since Method available since Release 3.2.12
  332. */
  333. protected static function arrayToName(array $parts, $join = '\\')
  334. {
  335. $result = '';
  336. if (count($parts) > 1) {
  337. array_pop($parts);
  338. $result = join($join, $parts);
  339. }
  340. return $result;
  341. }
  342. }