Conditions | 13 |
Paths | 6 |
Total Lines | 116 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | public function getRoutes(string ...$classes): array |
||
46 | { |
||
47 | $routes = []; |
||
48 | |||
49 | foreach ($classes as $class) { |
||
50 | /** @var Route[] $classAttributes */ |
||
51 | $classAttributes = $this->attributes->forClass($class, Route::class); |
||
52 | /** @var Route[] $memberAttributes */ |
||
53 | $memberAttributes = $this->attributes->forClassMembers($class, Route::class); |
||
54 | |||
55 | $finalAttributes = []; |
||
56 | |||
57 | // If this class has attributes |
||
58 | if (! empty($classAttributes)) { |
||
59 | // Iterate through all the class attributes |
||
60 | foreach ($classAttributes as $classAttribute) { |
||
61 | /** @var Parameter[] $mergedClassParameters */ |
||
62 | $mergedClassParameters = [ |
||
63 | ...$classAttribute->getParameters(), |
||
64 | ...$this->attributes->forClass($class, Parameter::class), |
||
65 | ]; |
||
66 | $mergedClassMiddleware = [ |
||
67 | ...($classAttribute->getMiddleware() ?? []), |
||
68 | ...array_column($this->attributes->forClass($class, Middleware::class), 'name'), |
||
69 | ]; |
||
70 | /** @var class-string<RoutingMessage>[] $mergedClassMessages */ |
||
71 | $mergedClassMessages = [ |
||
72 | ...($classAttribute->getMessages() ?? []), |
||
73 | ...array_column($this->attributes->forClass($class, Message::class), 'name'), |
||
74 | ]; |
||
75 | |||
76 | if (! empty($this->attributes->forClass($class, Secure::class))) { |
||
77 | $classAttribute->setSecure(); |
||
78 | } |
||
79 | |||
80 | if (! empty($to = array_column($this->attributes->forClass($class, Redirect::class), 'to'))) { |
||
81 | $classAttribute->setTo($to[0]); |
||
82 | } |
||
83 | |||
84 | $classAttribute->setParameters($mergedClassParameters); |
||
85 | $classAttribute->setMiddleware($mergedClassMiddleware); |
||
86 | $classAttribute->setMessages($mergedClassMessages); |
||
87 | |||
88 | // If the class' members' had attributes |
||
89 | if (! empty($memberAttributes)) { |
||
90 | // Iterate through all the members' attributes |
||
91 | foreach ($memberAttributes as $routeAttribute) { |
||
92 | $routeParameters = []; |
||
93 | $routeMiddleware = []; |
||
94 | $routeMessages = []; |
||
95 | $routeSecure = []; |
||
96 | $routeRedirect = []; |
||
97 | |||
98 | if ($property = $routeAttribute->getProperty()) { |
||
99 | $routeParameters = $this->attributes->forProperty($class, $property, Parameter::class); |
||
100 | $routeMiddleware = $this->attributes->forProperty($class, $property, Middleware::class); |
||
101 | $routeMessages = $this->attributes->forProperty($class, $property, Message::class); |
||
102 | $routeSecure = $this->attributes->forProperty($class, $property, Secure::class); |
||
103 | $routeRedirect = $this->attributes->forProperty($class, $property, Redirect::class); |
||
104 | } elseif ($method = $routeAttribute->getMethod()) { |
||
105 | $routeParameters = $this->attributes->forMethod($class, $method, Parameter::class); |
||
106 | $routeMiddleware = $this->attributes->forMethod($class, $method, Middleware::class); |
||
107 | $routeMessages = $this->attributes->forMethod($class, $method, Message::class); |
||
108 | $routeSecure = $this->attributes->forMethod($class, $method, Secure::class); |
||
109 | $routeRedirect = $this->attributes->forMethod($class, $method, Redirect::class); |
||
110 | } |
||
111 | |||
112 | /** @var Parameter[] $mergedPropertyParameters */ |
||
113 | $mergedPropertyParameters = [ |
||
114 | ...$routeAttribute->getParameters(), |
||
115 | ...$routeParameters, |
||
116 | ]; |
||
117 | $mergedPropertyMiddleware = [ |
||
118 | ...($routeAttribute->getMiddleware() ?? []), |
||
119 | ...array_column($routeMiddleware, 'name'), |
||
120 | ]; |
||
121 | /** @var class-string<RoutingMessage>[] $mergedPropertyMessages */ |
||
122 | $mergedPropertyMessages = [ |
||
123 | ...($routeAttribute->getMessages() ?? []), |
||
124 | ...array_column($routeMessages, 'name'), |
||
125 | ]; |
||
126 | |||
127 | if (! empty($routeSecure)) { |
||
128 | $routeAttribute->setSecure(); |
||
129 | } |
||
130 | |||
131 | if (! empty($to = array_column($routeRedirect, 'to'))) { |
||
132 | $routeAttribute->setTo($to[0]); |
||
133 | } |
||
134 | |||
135 | $routeAttribute->setParameters($mergedPropertyParameters); |
||
136 | $routeAttribute->setMiddleware($mergedPropertyMiddleware); |
||
137 | $routeAttribute->setMessages($mergedPropertyMessages); |
||
138 | |||
139 | // And set a new route with the controller defined annotation additions |
||
140 | $finalAttributes[] = $this->getControllerBuiltRoute($classAttribute, $routeAttribute); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // Figure out if there should be an else that automatically sets routes from the class attributes |
||
145 | } |
||
146 | } else { |
||
147 | $finalAttributes = $memberAttributes; |
||
148 | } |
||
149 | |||
150 | $routes = [ |
||
151 | ...$routes, |
||
152 | ...$finalAttributes, |
||
153 | ]; |
||
154 | } |
||
155 | |||
156 | foreach ($routes as $route) { |
||
157 | $this->setRouteProperties($route); |
||
158 | } |
||
159 | |||
160 | return $routes; |
||
161 | } |
||
300 |