1212
1313namespace phpDocumentor \Reflection \Types ;
1414
15+ use UnexpectedValueException ;
16+
1517/**
1618 * Convenience class to create a Context for DocBlocks when not using the Reflection Component of phpDocumentor.
1719 *
@@ -33,31 +35,53 @@ final class ContextFactory
3335 * Build a Context given a Class Reflection.
3436 *
3537 * @see Context for more information on Contexts.
36- * @return Context
3738 */
38- public function createFromReflector (\Reflector $ reflector )
39+ public function createFromReflector (\Reflector $ reflector ): Context
3940 {
41+ if ($ reflector instanceof \ReflectionClass) {
42+ return $ this ->createFromReflectionClass ($ reflector );
43+ }
44+
45+ if ($ reflector instanceof \ReflectionParameter) {
46+ return $ this ->createFromReflectionParameter ($ reflector );
47+ }
48+
4049 if ($ reflector instanceof \ReflectionMethod) {
4150 return $ this ->createFromReflectionMethod ($ reflector );
4251 }
4352
44- if ($ reflector instanceof \ReflectionClass ) {
45- return $ this ->createFromReflectionClass ($ reflector );
53+ if ($ reflector instanceof \ReflectionProperty ) {
54+ return $ this ->createFromReflectionProperty ($ reflector );
4655 }
56+
57+ if ($ reflector instanceof \ReflectionClassConstant) {
58+ return $ this ->createFromReflectionClassConstant ($ reflector );
59+ }
60+
61+ throw new UnexpectedValueException ('Unhandled \Reflector instance given: ' . get_class ($ reflector ));
4762 }
4863
49- /**
50- * @return Context
51- */
52- private function createFromReflectionMethod (\ReflectionMethod $ method )
64+ private function createFromReflectionParameter (\ReflectionParameter $ parameter ): Context
65+ {
66+ return $ this ->createFromReflectionClass ($ parameter ->getDeclaringClass ());
67+ }
68+
69+ private function createFromReflectionMethod (\ReflectionMethod $ method ): Context
5370 {
5471 return $ this ->createFromReflectionClass ($ method ->getDeclaringClass ());
5572 }
5673
57- /**
58- * @return Context
59- */
60- private function createFromReflectionClass (\ReflectionClass $ class )
74+ private function createFromReflectionProperty (\ReflectionProperty $ property ): Context
75+ {
76+ return $ this ->createFromReflectionClass ($ property ->getDeclaringClass ());
77+ }
78+
79+ private function createFromReflectionClassConstant (\ReflectionClassConstant $ constant ): Context
80+ {
81+ return $ this ->createFromReflectionClass ($ constant ->getDeclaringClass ());
82+ }
83+
84+ private function createFromReflectionClass (\ReflectionClass $ class ): Context
6185 {
6286 $ fileName = $ class ->getFileName ();
6387 $ namespace = $ class ->getNamespaceName ();
0 commit comments