44
55use Exception ;
66use Illuminate \Database \Eloquent \Model ;
7+ use Illuminate \Database \Eloquent \Model as IlluminateModel ;
78use Illuminate \Routing \Route ;
89use Illuminate \Support \Arr ;
910use League \Fractal \Manager ;
@@ -34,7 +35,7 @@ class UseTransformerTags extends Strategy
3435 *
3536 * @return array|null
3637 */
37- public function __invoke (Route $ route , \ ReflectionClass $ controller , \ ReflectionMethod $ method , array $ rulesToApply , array $ context = [])
38+ public function __invoke (Route $ route , ReflectionClass $ controller , ReflectionMethod $ method , array $ rulesToApply , array $ context = [])
3839 {
3940 $ docBlocks = RouteDocBlocker::getDocBlocksFromRoute ($ route );
4041 /** @var DocBlock $methodDocBlock */
@@ -47,6 +48,7 @@ public function __invoke(Route $route, \ReflectionClass $controller, \Reflection
4748 * Get a response from the transformer tags.
4849 *
4950 * @param array $tags
51+ * @param Route $route
5052 *
5153 * @return array|null
5254 */
@@ -57,7 +59,7 @@ protected function getTransformerResponse(array $tags, Route $route)
5759 return null ;
5860 }
5961
60- list ( $ statusCode , $ transformer) = $ this ->getStatusCodeAndTransformerClass ($ transformerTag );
62+ [ $ statusCode , $ transformer] = $ this ->getStatusCodeAndTransformerClass ($ transformerTag );
6163 $ model = $ this ->getClassToBeTransformed ($ tags , (new ReflectionClass ($ transformer ))->getMethod ('transform ' ));
6264 $ modelInstance = $ this ->instantiateTransformerModel ($ model );
6365
@@ -81,7 +83,7 @@ protected function getTransformerResponse(array $tags, Route $route)
8183 'content ' => $ response ->getContent (),
8284 ],
8385 ];
84- } catch (\ Exception $ e ) {
86+ } catch (Exception $ e ) {
8587 echo 'Exception thrown when fetching transformer response for [ ' .implode (', ' , $ route ->methods )."] {$ route ->uri }. \n" ;
8688 if (Flags::$ shouldBeVerbose ) {
8789 Utils::dumpException ($ e );
@@ -112,6 +114,8 @@ private function getStatusCodeAndTransformerClass($tag): array
112114 * @param array $tags
113115 * @param ReflectionMethod $transformerMethod
114116 *
117+ * @throws Exception
118+ *
115119 * @return string
116120 */
117121 private function getClassToBeTransformed (array $ tags , ReflectionMethod $ transformerMethod ): string
@@ -125,9 +129,9 @@ private function getClassToBeTransformed(array $tags, ReflectionMethod $transfor
125129 $ type = $ modelTag ->getContent ();
126130 } else {
127131 $ parameter = Arr::first ($ transformerMethod ->getParameters ());
128- if ($ parameter ->hasType () && ! $ parameter ->getType ()->isBuiltin () && class_exists (( string ) $ parameter ->getType ())) {
132+ if ($ parameter ->hasType () && ! $ parameter ->getType ()->isBuiltin () && class_exists ($ parameter ->getType ()-> getName ())) {
129133 // Ladies and gentlemen, we have a type!
130- $ type = ( string ) $ parameter ->getType ();
134+ $ type = $ parameter ->getType ()-> getName ();
131135 }
132136 }
133137
@@ -153,20 +157,20 @@ protected function instantiateTransformerModel(string $type)
153157 $ type = ltrim ($ type , '\\' );
154158
155159 return factory ($ type )->make ();
156- } catch (\ Exception $ e ) {
160+ } catch (Exception $ e ) {
157161 if (Flags::$ shouldBeVerbose ) {
158162 echo "Eloquent model factory failed to instantiate {$ type }; trying to fetch from database. \n" ;
159163 }
160164
161165 $ instance = new $ type ;
162- if ($ instance instanceof \ Illuminate \ Database \ Eloquent \Model ) {
166+ if ($ instance instanceof IlluminateModel ) {
163167 try {
164168 // we can't use a factory but can try to get one from the database
165169 $ firstInstance = $ type ::first ();
166170 if ($ firstInstance ) {
167171 return $ firstInstance ;
168172 }
169- } catch (\ Exception $ e ) {
173+ } catch (Exception $ e ) {
170174 // okay, we'll stick with `new`
171175 if (Flags::$ shouldBeVerbose ) {
172176 echo "Failed to fetch first {$ type } from database; using `new` to instantiate. \n" ;
0 commit comments