PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/nette-dev/Reflection/MethodReflection.php

https://github.com/bazo/Mokuji
PHP | 229 lines | 100 code | 67 blank | 62 comment | 4 complexity | 8f13bbbf3913a29c365184f47ccaa7c4 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Reflection
  10. */
  11. /**
  12. * Reports information about a method.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Reflection
  16. */
  17. class MethodReflection extends ReflectionMethod
  18. {
  19. /**
  20. * @param string|object
  21. * @param string
  22. * @return MethodReflection
  23. */
  24. public static function from($class, $method)
  25. {
  26. return new self(is_object($class) ? get_class($class) : $class, $method);
  27. }
  28. /**
  29. * @return array
  30. */
  31. public function getDefaultParameters()
  32. {
  33. $res = array();
  34. foreach (parent::getParameters() as $param) {
  35. $res[$param->getName()] = $param->isDefaultValueAvailable()
  36. ? $param->getDefaultValue()
  37. : NULL;
  38. if ($param->isArray()) {
  39. settype($res[$param->getName()], 'array');
  40. }
  41. }
  42. return $res;
  43. }
  44. /**
  45. * Invokes method using named parameters.
  46. * @param object
  47. * @param array
  48. * @return mixed
  49. */
  50. public function invokeNamedArgs($object, $args)
  51. {
  52. $res = array();
  53. $i = 0;
  54. foreach ($this->getDefaultParameters() as $name => $def) {
  55. if (isset($args[$name])) { // NULL treats as none value
  56. $val = $args[$name];
  57. if ($def !== NULL) {
  58. settype($val, gettype($def));
  59. }
  60. $res[$i++] = $val;
  61. } else {
  62. $res[$i++] = $def;
  63. }
  64. }
  65. return $this->invokeArgs($object, $res);
  66. }
  67. /**
  68. * @return Callback
  69. */
  70. public function getCallback()
  71. {
  72. return new Callback(parent::getDeclaringClass()->getName(), $this->getName());
  73. }
  74. public function __toString()
  75. {
  76. return 'Method ' . parent::getDeclaringClass()->getName() . '::' . $this->getName() . '()';
  77. }
  78. /********************* Reflection layer ****************d*g**/
  79. /**
  80. * @return MethodReflection
  81. * @ignore internal
  82. */
  83. public static function import(ReflectionMethod $ref)
  84. {
  85. return new self($ref->getDeclaringClass()->getName(), $ref->getName());
  86. }
  87. /**
  88. * @return ClassReflection
  89. */
  90. public function getDeclaringClass()
  91. {
  92. return ClassReflection::import(parent::getDeclaringClass());
  93. }
  94. /**
  95. * @return ExtensionReflection
  96. */
  97. public function getExtension()
  98. {
  99. return ($ref = parent::getExtension()) ? ExtensionReflection::import($ref) : NULL;
  100. }
  101. public function getParameters()
  102. {
  103. return array_map(array('MethodParameterReflection', 'import'), parent::getParameters());
  104. }
  105. /********************* Nette\Annotations support ****************d*g**/
  106. /**
  107. * Has method specified annotation?
  108. * @param string
  109. * @return bool
  110. */
  111. public function hasAnnotation($name)
  112. {
  113. $res = AnnotationsParser::getAll($this);
  114. return !empty($res[$name]);
  115. }
  116. /**
  117. * Returns an annotation value.
  118. * @param string
  119. * @return IAnnotation
  120. */
  121. public function getAnnotation($name)
  122. {
  123. $res = AnnotationsParser::getAll($this);
  124. return isset($res[$name]) ? end($res[$name]) : NULL;
  125. }
  126. /**
  127. * Returns all annotations.
  128. * @return array
  129. */
  130. public function getAnnotations()
  131. {
  132. return AnnotationsParser::getAll($this);
  133. }
  134. /********************* Nette\Object behaviour ****************d*g**/
  135. /**
  136. * @return ClassReflection
  137. */
  138. public function getReflection()
  139. {
  140. return new ClassReflection($this);
  141. }
  142. public function __call($name, $args)
  143. {
  144. return ObjectMixin::call($this, $name, $args);
  145. }
  146. public function &__get($name)
  147. {
  148. return ObjectMixin::get($this, $name);
  149. }
  150. public function __set($name, $value)
  151. {
  152. return ObjectMixin::set($this, $name, $value);
  153. }
  154. public function __isset($name)
  155. {
  156. return ObjectMixin::has($this, $name);
  157. }
  158. public function __unset($name)
  159. {
  160. throw new MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
  161. }
  162. }