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

Revision 1281, 6.9 kB (checked in by mikey, 5 months ago)

code nazi :)

Line 
1 <?php
2 /**
3  * Extended Reflection class for parameters.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  reflection
8  */
9 stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotatable',
10                       'net::stubbles::reflection::annotations::stubAnnotationFactory',
11                       'net::stubbles::reflection::stubReflectionFunction',
12                       'net::stubbles::reflection::stubReflectionClass'
13 );
14 /**
15  * Extended Reflection class for parameters.
16  *
17  * @package     stubbles
18  * @subpackage  reflection
19  */
20 class stubReflectionParameter extends ReflectionParameter implements stubAnnotatable
21 {
22     /**
23      * name of reflected routine
24      *
25      * @var  string
26      */
27     protected $routineName;
28     /**
29      * reflection instance of routine containing this parameter
30      *
31      * @var  stubReflectionRoutine
32      */
33     protected $refRoutine;
34     /**
35      * name of reflected parameter
36      *
37      * @var  string
38      */
39     protected $paramName;
40
41     /**
42      * constructor
43      *
44      * @param  string|array|stubReflectionRoutine  $routine    name or reflection instance of routine
45      * @param  string                              $paramName  name of parameter to reflect
46      */
47     public function __construct($routine, $paramName)
48     {
49         if ($routine instanceof stubReflectionMethod) {
50             $this->refRoutine  = $routine;
51             $this->routineName = array($routine->getDeclaringClass()->getName(), $routine->getName());
52         } elseif ($routine instanceof stubReflectionFunction) {
53             $this->refRoutine  = $routine;
54             $this->routineName = $routine->getName();
55         } else {
56             $this->routineName = $routine;
57         }
58         
59         $this->paramName = $paramName;
60         parent::__construct($this->routineName, $paramName);
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         $refRoutine = $this->getRefRoutine();
72         $targetName = ((is_array($this->routineName) === true) ? ($this->routineName[0] . '::' . $this->routineName[1] . '()') : ($this->routineName));
73         return stubAnnotationFactory::has($refRoutine->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $refRoutine->getFileName());
74     }
75
76     /**
77      * return the specified annotation
78      *
79      * @param   string          $annotationName
80      * @return  stubAnnotation
81      * @throws  ReflectionException
82      */
83     public function getAnnotation($annotationName)
84     {
85         $refRoutine = $this->getRefRoutine();
86         $targetName = ((is_array($this->routineName) === true) ? ($this->routineName[0] . '::' . $this->routineName[1] . '()') : ($this->routineName));
87         return stubAnnotationFactory::create($refRoutine->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $refRoutine->getFileName());
88     }
89
90     /**
91      * helper method to return the reflection routine defining this parameter
92      *
93      * @return  stubReflectionRoutine
94      * @todo    replace by getDeclaringFunction() as soon as Stubbles requires at least PHP 5.2.3
95      */
96     protected function getRefRoutine()
97     {
98         if (null === $this->refRoutine) {
99             if (is_array($this->routineName) === true) {
100                 $this->refRoutine = new stubReflectionMethod($this->routineName[0], $this->routineName[1]);
101             } else {
102                 $this->refRoutine = new stubReflectionFunction($this->routineName);
103             }
104         }
105         
106         return $this->refRoutine;
107     }
108
109     /**
110      * checks whether a value is equal to the class
111      *
112      * @param   mixed  $compare
113      * @return  bool
114      */
115     public function equals($compare)
116     {
117         if (($compare instanceof self) == false) {
118             return false;
119         }
120         
121         $class        = $this->getDeclaringClass();
122         $compareClass = $compare->getDeclaringClass();
123         if ((null == $class && null != $compareClass) || null != $class && null == $compareClass) {
124             return false;
125         }
126         
127         if (null == $class) {
128             return ($compare->routineName == $this->routineName && $compare->paramName == $this->paramName);
129         }
130         
131         return ($compareClass->getName() == $class->getName() && $compare->routineName == $this->routineName && $compare->paramName == $this->paramName);
132     }
133
134     /**
135      * returns a string representation of the class
136      *
137      * The result is a short but informative representation about the class and
138      * its values. Per default, this method returns:
139      * 'net::stubbles::reflection::stubReflectionParameter['[name-of-reflected-class]'::'[name-of-reflected-function]'(): Argument '[name-of-reflected-argument]']  {}'
140      * <code>
141      * net::stubbles::reflection::stubReflectionParameter[MyClass::myMethod(): Argument foo] {
142      * }
143      * net::stubbles::reflection::stubReflectionParameter[myFunction(): Argument bar] {
144      * }
145      * </code>
146      *
147      * @return  string
148      */
149     public function __toString()
150     {
151         if (is_array($this->routineName) == false) {
152             return 'net::stubbles::reflection::stubReflectionParameter[' . $this->routineName . '(): Argument ' . $this->paramName . "] {\n}\n";
153         }
154         
155         return 'net::stubbles::reflection::stubReflectionParameter[' . $this->routineName[0] . '::' . $this->routineName[1] . '(): Argument ' . $this->paramName . "] {\n}\n";
156     }
157
158     /**
159      * returns the function that declares this parameter
160      *
161      * @return  stubReflectionFunction
162      */
163     # well, manual says its there, its even in php cvs, but calling
164     # ReflectionParameter::getDeclaringFunction() results in a fatal error
165     # with message "Call to undefined method"
166     #public function getDeclaringFunction()
167     #{
168     #    $refFunction     = parent::getDeclaringFunction();
169     #    $stubRefFunction = new stubReflectionFunction($refFunction->getName());
170     #    return $stubRefFunction;
171     #}
172
173     /**
174      * returns the class that declares this parameter
175      *
176      * @return  stubReflectionClass
177      */
178     public function getDeclaringClass()
179     {
180         if (is_array($this->routineName) === false) {
181             return null;
182         }
183         
184         $refClass     = parent::getDeclaringClass();
185         $stubRefClass = new stubReflectionClass($refClass->getName());
186         return $stubRefClass;
187     }
188
189     /**
190      * returns the type (class) hint for this parameter
191      *
192      * @return  stubReflectionClass
193      */
194     public function getClass()
195     {
196         $refClass = parent::getClass();
197         if (null === $refClass) {
198             return null;
199         }
200         
201         $stubRefClass = new stubReflectionClass($refClass->getName());
202         return $stubRefClass;
203     }
204 }
205 ?>
Note: See TracBrowser for help on using the browser.