12
12
13
13
namespace phpDocumentor \Reflection \Types ;
14
14
15
+ use phpDocumentor \Reflection \DocBlock \Context ;
16
+ use UnexpectedValueException ;
17
+
15
18
/**
16
19
* Convenience class to create a Context for DocBlocks when not using the Reflection Component of phpDocumentor.
17
20
*
@@ -33,31 +36,53 @@ final class ContextFactory
33
36
* Build a Context given a Class Reflection.
34
37
*
35
38
* @see Context for more information on Contexts.
36
- * @return Context
37
39
*/
38
- public function createFromReflector (\Reflector $ reflector )
40
+ public function createFromReflector (\Reflector $ reflector ): Context
39
41
{
42
+ if ($ reflector instanceof \ReflectionClass) {
43
+ return $ this ->createFromReflectionClass ($ reflector );
44
+ }
45
+
46
+ if ($ reflector instanceof \ReflectionParameter) {
47
+ return $ this ->createFromReflectionParameter ($ reflector );
48
+ }
49
+
40
50
if ($ reflector instanceof \ReflectionMethod) {
41
51
return $ this ->createFromReflectionMethod ($ reflector );
42
52
}
43
53
44
- if ($ reflector instanceof \ReflectionClass ) {
45
- return $ this ->createFromReflectionClass ($ reflector );
54
+ if ($ reflector instanceof \ReflectionProperty ) {
55
+ return $ this ->createFromReflectionProperty ($ reflector );
46
56
}
57
+
58
+ if ($ reflector instanceof \ReflectionClassConstant) {
59
+ return $ this ->createFromReflectionClassConstant ($ reflector );
60
+ }
61
+
62
+ throw new UnexpectedValueException ('Unhandled \Reflector instance given: ' . get_class ($ reflector ));
47
63
}
48
64
49
- /**
50
- * @return Context
51
- */
52
- private function createFromReflectionMethod (\ReflectionMethod $ method )
65
+ private function createFromReflectionParameter (\ReflectionParameter $ parameter ): Context
66
+ {
67
+ return $ this ->createFromReflectionClass ($ parameter ->getDeclaringClass ());
68
+ }
69
+
70
+ private function createFromReflectionMethod (\ReflectionMethod $ method ): Context
53
71
{
54
72
return $ this ->createFromReflectionClass ($ method ->getDeclaringClass ());
55
73
}
56
74
57
- /**
58
- * @return Context
59
- */
60
- private function createFromReflectionClass (\ReflectionClass $ class )
75
+ private function createFromReflectionProperty (\ReflectionProperty $ property ): Context
76
+ {
77
+ return $ this ->createFromReflectionClass ($ property ->getDeclaringClass ());
78
+ }
79
+
80
+ private function createFromReflectionClassConstant (\ReflectionClassConstant $ constant ): Context
81
+ {
82
+ return $ this ->createFromReflectionClass ($ constant ->getDeclaringClass ());
83
+ }
84
+
85
+ private function createFromReflectionClass (\ReflectionClass $ class ): Context
61
86
{
62
87
$ fileName = $ class ->getFileName ();
63
88
$ namespace = $ class ->getNamespaceName ();
0 commit comments