PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/Reflection/Method.php

https://bitbucket.org/netglue/zf-1.12-release
PHP | 187 lines | 92 code | 18 blank | 77 comment | 19 complexity | c3d42677d8d9f00f8f1fbb2df8411064 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Reflection
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Reflection_Class
  23. */
  24. require_once 'Zend/Reflection/Class.php';
  25. /**
  26. * @see Zend_Reflection_Docblock
  27. */
  28. require_once 'Zend/Reflection/Docblock.php';
  29. /**
  30. * @see Zend_Reflection_Parameter
  31. */
  32. require_once 'Zend/Reflection/Parameter.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Reflection
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Reflection_Method extends ReflectionMethod
  40. {
  41. /**
  42. * Retrieve method docblock reflection
  43. *
  44. * @return Zend_Reflection_Docblock
  45. * @throws Zend_Reflection_Exception
  46. */
  47. public function getDocblock($reflectionClass = 'Zend_Reflection_Docblock')
  48. {
  49. if ('' == $this->getDocComment()) {
  50. require_once 'Zend/Reflection/Exception.php';
  51. throw new Zend_Reflection_Exception($this->getName() . ' does not have a docblock');
  52. }
  53. $instance = new $reflectionClass($this);
  54. if (!$instance instanceof Zend_Reflection_Docblock) {
  55. require_once 'Zend/Reflection/Exception.php';
  56. throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Docblock');
  57. }
  58. return $instance;
  59. }
  60. /**
  61. * Get start line (position) of method
  62. *
  63. * @param bool $includeDocComment
  64. * @return int
  65. */
  66. public function getStartLine($includeDocComment = false)
  67. {
  68. if ($includeDocComment) {
  69. if ($this->getDocComment() != '') {
  70. return $this->getDocblock()->getStartLine();
  71. }
  72. }
  73. return parent::getStartLine();
  74. }
  75. /**
  76. * Get reflection of declaring class
  77. *
  78. * @param string $reflectionClass Name of reflection class to use
  79. * @return Zend_Reflection_Class
  80. */
  81. public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
  82. {
  83. $phpReflection = parent::getDeclaringClass();
  84. $zendReflection = new $reflectionClass($phpReflection->getName());
  85. if (!$zendReflection instanceof Zend_Reflection_Class) {
  86. require_once 'Zend/Reflection/Exception.php';
  87. throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
  88. }
  89. unset($phpReflection);
  90. return $zendReflection;
  91. }
  92. /**
  93. * Get all method parameter reflection objects
  94. *
  95. * @param string $reflectionClass Name of reflection class to use
  96. * @return array of Zend_Reflection_Parameter objects
  97. */
  98. public function getParameters($reflectionClass = 'Zend_Reflection_Parameter')
  99. {
  100. $phpReflections = parent::getParameters();
  101. $zendReflections = array();
  102. while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
  103. $instance = new $reflectionClass(array($this->getDeclaringClass()->getName(), $this->getName()), $phpReflection->getName());
  104. if (!$instance instanceof Zend_Reflection_Parameter) {
  105. require_once 'Zend/Reflection/Exception.php';
  106. throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Parameter');
  107. }
  108. $zendReflections[] = $instance;
  109. unset($phpReflection);
  110. }
  111. unset($phpReflections);
  112. return $zendReflections;
  113. }
  114. /**
  115. * Get method contents
  116. *
  117. * @param bool $includeDocblock
  118. * @return string
  119. */
  120. public function getContents($includeDocblock = true)
  121. {
  122. $fileContents = file($this->getFileName());
  123. $startNum = $this->getStartLine($includeDocblock);
  124. $endNum = ($this->getEndLine() - $this->getStartLine());
  125. return implode("\n", array_splice($fileContents, $startNum, $endNum, true));
  126. }
  127. /**
  128. * Get method body
  129. *
  130. * @return string
  131. */
  132. public function getBody()
  133. {
  134. $lines = array_slice(
  135. file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES),
  136. $this->getStartLine()-1,
  137. ($this->getEndLine() - $this->getStartLine()) + 1,
  138. true
  139. );
  140. // Strip off lines until we come to a closing bracket
  141. do {
  142. if (count($lines) == 0) break;
  143. $firstLine = array_shift($lines);
  144. } while (strpos($firstLine, ')') === false);
  145. // If the opening brace isn't on the same line as method
  146. // signature, then we should pop off more lines until we find it
  147. if (strpos($firstLine,'{') === false) {
  148. do {
  149. if (count($lines) == 0) break;
  150. $firstLine = array_shift($lines);
  151. } while (strpos($firstLine, '{') === false);
  152. }
  153. // If there are more characters on the line after the opening brace,
  154. // push them back onto the lines stack as they are part of the body
  155. $restOfFirstLine = trim(substr($firstLine, strpos($firstLine, '{')+1));
  156. if (!empty($restOfFirstLine)) {
  157. array_unshift($lines, $restOfFirstLine);
  158. }
  159. $lastLine = array_pop($lines);
  160. // If there are more characters on the line before the closing brace,
  161. // push them back onto the lines stack as they are part of the body
  162. $restOfLastLine = trim(substr($lastLine, 0, strrpos($lastLine, '}')-1));
  163. if (!empty($restOfLastLine)) {
  164. array_push($lines, $restOfLastLine);
  165. }
  166. // just in case we had code on the bracket lines
  167. return implode("\n", $lines);
  168. }
  169. }