1212
1313namespace phpDocumentor \Reflection \Types ;
1414
15+ use phpDocumentor \Reflection \DocBlock \Context ;
16+ use UnexpectedValueException ;
17+
1518/**
1619 * Convenience class to create a Context for DocBlocks when not using the Reflection Component of phpDocumentor.
1720 *
@@ -33,31 +36,53 @@ final class ContextFactory
3336 * Build a Context given a Class Reflection.
3437 *
3538 * @see Context for more information on Contexts.
36- * @return Context
3739 */
38- public function createFromReflector (\Reflector $ reflector )
40+ public function createFromReflector (\Reflector $ reflector ): Context
3941 {
42+ if ($ reflector instanceof \ReflectionClass) {
43+ return $ this ->createFromReflectionClass ($ reflector );
44+ }
45+
46+ if ($ reflector instanceof \ReflectionParameter) {
47+ return $ this ->createFromReflectionParameter ($ reflector );
48+ }
49+
4050 if ($ reflector instanceof \ReflectionMethod) {
4151 return $ this ->createFromReflectionMethod ($ reflector );
4252 }
4353
44- if ($ reflector instanceof \ReflectionClass ) {
45- return $ this ->createFromReflectionClass ($ reflector );
54+ if ($ reflector instanceof \ReflectionProperty ) {
55+ return $ this ->createFromReflectionProperty ($ reflector );
4656 }
57+
58+ if ($ reflector instanceof \ReflectionClassConstant) {
59+ return $ this ->createFromReflectionClassConstant ($ reflector );
60+ }
61+
62+ throw new UnexpectedValueException ('Unhandled \Reflector instance given: ' . get_class ($ reflector ));
4763 }
4864
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
5371 {
5472 return $ this ->createFromReflectionClass ($ method ->getDeclaringClass ());
5573 }
5674
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
6186 {
6287 $ fileName = $ class ->getFileName ();
6388 $ namespace = $ class ->getNamespaceName ();
0 commit comments