3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Reflection
;
12 use ReflectionMethod
as PhpReflectionMethod
;
13 use Zend\Code\Annotation\AnnotationManager
;
14 use Zend\Code\Scanner\AnnotationScanner
;
15 use Zend\Code\Scanner\CachingFileScanner
;
17 class MethodReflection
extends PhpReflectionMethod
implements ReflectionInterface
20 * @var AnnotationScanner
22 protected $annotations = null;
25 * Retrieve method DocBlock reflection
27 * @return DocBlockReflection|false
29 public function getDocBlock()
31 if ('' == $this->getDocComment()) {
35 $instance = new DocBlockReflection($this);
41 * @param AnnotationManager $annotationManager
42 * @return AnnotationScanner
44 public function getAnnotations(AnnotationManager
$annotationManager)
46 if (($docComment = $this->getDocComment()) == '') {
50 if ($this->annotations
) {
51 return $this->annotations
;
54 $cachingFileScanner = $this->createFileScanner($this->getFileName());
55 $nameInformation = $cachingFileScanner->getClassNameInformation($this->getDeclaringClass()->getName());
57 if (!$nameInformation) {
61 $this->annotations
= new AnnotationScanner($annotationManager, $docComment, $nameInformation);
63 return $this->annotations
;
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 * @return ClassReflection
88 public function getDeclaringClass()
90 $phpReflection = parent
::getDeclaringClass();
91 $zendReflection = new ClassReflection($phpReflection->getName());
92 unset($phpReflection);
94 return $zendReflection;
98 * Get all method parameter reflection objects
100 * @return ParameterReflection[]
102 public function getParameters()
104 $phpReflections = parent
::getParameters();
105 $zendReflections = array();
106 while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
107 $instance = new ParameterReflection(array(
108 $this->getDeclaringClass()->getName(),
110 $phpReflection->getName()
112 $zendReflections[] = $instance;
113 unset($phpReflection);
115 unset($phpReflections);
117 return $zendReflections;
121 * Get method contents
123 * @param bool $includeDocBlock
126 public function getContents($includeDocBlock = true)
128 $fileContents = file($this->getFileName());
129 $startNum = $this->getStartLine($includeDocBlock);
130 $endNum = ($this->getEndLine() - $this->getStartLine());
132 return implode("\n", array_splice($fileContents, $startNum, $endNum, true));
140 public function getBody()
142 $lines = array_slice(
143 file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES
),
144 $this->getStartLine(),
145 ($this->getEndLine() - $this->getStartLine()),
149 $firstLine = array_shift($lines);
151 if (trim($firstLine) !== '{') {
152 array_unshift($lines, $firstLine);
155 $lastLine = array_pop($lines);
157 if (trim($lastLine) !== '}') {
158 array_push($lines, $lastLine);
161 // just in case we had code on the bracket lines
162 return rtrim(ltrim(implode("\n", $lines), '{'), '}');
165 public function toString()
167 return parent
::__toString();
170 public function __toString()
172 return parent
::__toString();
176 * Creates a new FileScanner instance.
178 * By having this as a seperate method it allows the method to be overridden
179 * if a different FileScanner is needed.
181 * @param string $filename
183 * @return FileScanner
185 protected function createFileScanner($filename)
187 return new CachingFileScanner($filename);