root/trunk/src/main/php/net/stubbles/reflection/stubReflectionMethod.php

Revision 1227, 6.0 kB (checked in by mikey, 6 months ago)

continued refactoring #119: replaced package dots in net::stubbles::reflection

Line 
1 <?php
2 /**
3  * Extended Reflection class for class methods that allows usage of annotations.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @author      Stephan Schmidt <schst@stubbles.net>
7  * @package     stubbles
8  * @subpackage  reflection
9  */
10 stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotatable',
11                       'net::stubbles::reflection::annotations::stubAnnotationFactory',
12                       'net::stubbles::reflection::stubReflectionClass',
13                       'net::stubbles::reflection::stubReflectionParameter',
14                       'net::stubbles::reflection::stubReflectionPrimitive',
15                       'net::stubbles::reflection::stubReflectionRoutine'
16 );
17 /**
18  * Extended Reflection class for class methods that allows usage of annotations.
19  *
20  * @package     stubbles
21  * @subpackage  reflection
22  */
23 class stubReflectionMethod extends ReflectionMethod implements stubReflectionRoutine
24 {
25     /**
26      * name of the reflected class
27      *
28      * @var  string
29      */
30     protected $className;
31     /**
32      * declaring class
33      *
34      * @var  stubBaseReflectionClass
35      */
36     protected $refClass;
37     /**
38      * name of the reflected method
39      *
40      * @var  string
41      */
42     protected $methodName;
43
44     /**
45      * constructor
46      *
47      * @param  string|stubBaseReflectionClass  $class       name of class to reflect
48      * @param  string                          $methodName  name of method to reflect
49      */
50     public function __construct($class, $methodName)
51     {
52         if ($class instanceof stubBaseReflectionClass) {
53             $this->refClass   = $class;
54             $this->className  = $this->refClass->getName();
55         } else {
56             $this->className  = $class;
57         }
58         
59         $this->methodName = $methodName;
60         parent::__construct($this->className, $methodName);
61     }
62
63     /**
64      * check whether the class has the given annotation or not
65      *
66      * @param   string  $annotationName
67      * @return  bool
68      */
69     public function hasAnnotation($annotationName)
70     {
71         return stubAnnotationFactory::has($this->getDocComment(), $annotationName, stubAnnotation::TARGET_METHOD, $this->className . '::' . $this->methodName . '()', $this->getFileName());
72     }
73
74     /**
75      * return the specified annotation
76      *
77      * @param   string          $annotationName
78      * @return  stubAnnotation
79      * @throws  ReflectionException
80      */
81     public function getAnnotation($annotationName)
82     {
83         return stubAnnotationFactory::create($this->getDocComment(), $annotationName, stubAnnotation::TARGET_METHOD, $this->className . '::' . $this->methodName . '()', $this->getFileName());
84     }
85
86     /**
87      * checks whether a value is equal to the class
88      *
89      * @param   mixed  $compare
90      * @return  bool
91      */
92     public function equals($compare)
93     {
94         if ($compare instanceof self) {
95             return ($compare->className === $this->className && $compare->methodName === $this->methodName);
96         }
97         
98         return false;
99     }
100
101     /**
102      * returns a string representation of the class
103      *
104      * The result is a short but informative representation about the class and
105      * its values. Per default, this method returns:
106      * 'net::stubbles::reflection::stubReflectionMethod['[name-of-reflected-class]'::'[name-of-reflected-method]'()]  {}'
107      * <code>
108      * net::stubbles::reflection::stubReflectionMethod[MyClass::myMethod()] {
109      * }
110      * </code>
111      *
112      * @return  string
113      */
114     public function __toString()
115     {
116         return 'net::stubbles::reflection::stubReflectionMethod[' . $this->className . '::' . $this->methodName . "()] {\n}\n";
117     }
118
119     /**
120      * returns the class that declares this method
121      *
122      * @return  stubBaseReflectionClass
123      */
124     public function getDeclaringClass()
125     {
126         $refClass = parent::getDeclaringClass();
127         if ($refClass->getName() === $this->className) {
128             if (null === $this->refClass) {
129                 $this->refClass = new stubReflectionClass($this->className);
130             }
131             
132             return $this->refClass;
133         }
134         
135         $stubRefClass = new stubReflectionClass($refClass->getName());
136         return $stubRefClass;
137     }
138
139     /**
140      * returns a list of all parameters
141      *
142      * @return  array<stubReflectionParameter>
143      */
144     public function getParameters()
145     {
146         $parameters     = parent::getParameters();
147         $stubParameters = array();
148         foreach ($parameters as $parameter) {
149             $stubParameters[] = new stubReflectionParameter($this, $parameter->getName());
150         }
151         
152         return $stubParameters;
153     }
154
155     /**
156      * returns information about the return type of a method
157      *
158      * If the return type is a class the return value is an instance of
159      * stubReflectionClass (if the class is unknown a
160      * stubClassNotFoundException will be thrown), if it is a scalar type the
161      * return value is an instance of stubReflectionPrimitive, and if the
162      * method does not have a return value this method returns null.
163      * Please be aware that this is guessing from the doc block with which the
164      * method is documented. If the doc block is missing or incorrect the return
165      * value of this method may be wrong. This is due to missing type hints for
166      * return values in PHP itself.
167      *
168      * @return  stubReflectionType
169      */
170     public function getReturnType()
171     {
172         $returnPart = strstr($this->getDocComment(), '@return');
173         if (false === $returnPart) {
174             return null;
175         }
176         
177         $returnParts = explode(' ', trim(str_replace('@return', '', $returnPart)));
178         $returnType  = trim($returnParts[0]);
179         try {
180             $reflectionType = stubReflectionPrimitive::forName(new ReflectionClass('stubReflectionPrimitive'), $returnType);
181         } catch (stubIllegalArgumentException $iae) {
182             $reflectionType = new stubReflectionClass($returnType);
183         }
184         
185         return $reflectionType;
186     }
187 }
188 ?>
Note: See TracBrowser for help on using the browser.