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.
16 * @package Zend_Reflection
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Reflection_Class
25 require_once 'Zend/Reflection/Class.php';
28 * @see Zend_Reflection_Docblock
30 require_once 'Zend/Reflection/Docblock.php';
33 * @see Zend_Reflection_Parameter
35 require_once 'Zend/Reflection/Parameter.php';
39 * @package Zend_Reflection
40 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
41 * @license http://framework.zend.com/license/new-bsd New BSD License
43 class Zend_Reflection_Method
extends ReflectionMethod
46 * Retrieve method docblock reflection
48 * @return Zend_Reflection_Docblock
49 * @throws Zend_Reflection_Exception
51 public function getDocblock($reflectionClass = 'Zend_Reflection_Docblock')
53 if ('' == $this->getDocComment()) {
54 require_once 'Zend/Reflection/Exception.php';
55 throw new Zend_Reflection_Exception($this->getName() . ' does not have a docblock');
58 $instance = new $reflectionClass($this);
59 if (!$instance instanceof Zend_Reflection_Docblock
) {
60 require_once 'Zend/Reflection/Exception.php';
61 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Docblock');
67 * Get start line (position) of method
69 * @param bool $includeDocComment
72 public function getStartLine($includeDocComment = false)
74 if ($includeDocComment) {
75 if ($this->getDocComment() != '') {
76 return $this->getDocblock()->getStartLine();
80 return parent
::getStartLine();
84 * Get reflection of declaring class
86 * @param string $reflectionClass Name of reflection class to use
87 * @return Zend_Reflection_Class
89 public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
91 $phpReflection = parent
::getDeclaringClass();
92 $zendReflection = new $reflectionClass($phpReflection->getName());
93 if (!$zendReflection instanceof Zend_Reflection_Class
) {
94 require_once 'Zend/Reflection/Exception.php';
95 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
97 unset($phpReflection);
98 return $zendReflection;
102 * Get all method parameter reflection objects
104 * @param string $reflectionClass Name of reflection class to use
105 * @return array of Zend_Reflection_Parameter objects
107 public function getParameters($reflectionClass = 'Zend_Reflection_Parameter')
109 $phpReflections = parent
::getParameters();
110 $zendReflections = array();
111 while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
112 $instance = new $reflectionClass(array($this->getDeclaringClass()->getName(), $this->getName()), $phpReflection->getName());
113 if (!$instance instanceof Zend_Reflection_Parameter
) {
114 require_once 'Zend/Reflection/Exception.php';
115 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Parameter');
117 $zendReflections[] = $instance;
118 unset($phpReflection);
120 unset($phpReflections);
121 return $zendReflections;
125 * Get method contents
127 * @param bool $includeDocblock
130 public function getContents($includeDocblock = true)
132 $fileContents = file($this->getFileName());
133 $startNum = $this->getStartLine($includeDocblock);
134 $endNum = ($this->getEndLine() - $this->getStartLine());
136 return implode("\n", array_splice($fileContents, $startNum, $endNum, true));
144 public function getBody()
146 $lines = array_slice(
147 file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES
),
148 $this->getStartLine(),
149 ($this->getEndLine() - $this->getStartLine()),
153 $firstLine = array_shift($lines);
155 if (trim($firstLine) !== '{') {
156 array_unshift($lines, $firstLine);
159 $lastLine = array_pop($lines);
161 if (trim($lastLine) !== '}') {
162 array_push($lines, $lastLine);
165 // just in case we had code on the bracket lines
166 return rtrim(ltrim(implode("\n", $lines), '{'), '}');