Conditions | 7 |
Paths | 1 |
Total Lines | 217 |
Code Lines | 141 |
Lines | 0 |
Ratio | 0 % |
Changes | 22 | ||
Bugs | 3 | Features | 3 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
51 | public function __construct(RemoteStorage $remoteStorage, ApprovalManagementStorage $approvalManagementStorage, TemplateManagerInterface $templateManager, ClientStorageInterface $clientStorage, ResourceServerStorageInterface $resourceServerStorage, ApprovalStorageInterface $approvalStorage, AuthorizationCodeStorageInterface $authorizationCodeStorage, AccessTokenStorageInterface $accessTokenStorage, array $options = array(), IO $io = null) |
||
52 | { |
||
53 | $this->remoteStorage = $remoteStorage; |
||
54 | $this->approvalManagementStorage = $approvalManagementStorage; |
||
55 | |||
56 | parent::__construct( |
||
57 | $templateManager, |
||
58 | $clientStorage, |
||
59 | $resourceServerStorage, |
||
60 | $approvalStorage, |
||
61 | $authorizationCodeStorage, |
||
62 | $accessTokenStorage, |
||
63 | $options, |
||
64 | $io |
||
65 | ); |
||
66 | |||
67 | $this->get( |
||
68 | '/_account', |
||
69 | function (Request $request, UserInfoInterface $userInfo) { |
||
70 | $approvalList = $this->approvalManagementStorage->getApprovalList($userInfo->getUserId()); |
||
71 | |||
72 | return $this->templateManager->render( |
||
73 | 'getAccountPage', |
||
74 | array( |
||
75 | 'approval_list' => $approvalList, |
||
76 | 'host' => $request->getHeader('Host'), |
||
77 | 'user_id' => $userInfo->getUserId(), |
||
78 | 'disk_usage' => $this->remoteStorage->getFolderSize(new Path('/'.$userInfo->getUserId().'/')), |
||
79 | 'request_url' => $request->getUrl()->toString(), |
||
80 | 'show_account_icon' => true, |
||
81 | ) |
||
82 | ); |
||
83 | }, |
||
84 | array( |
||
85 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
86 | 'activate' => array('user'), |
||
87 | ), |
||
88 | ) |
||
89 | ); |
||
90 | |||
91 | $this->delete( |
||
92 | '/_approvals', |
||
93 | function (Request $request, UserInfoInterface $userInfo) { |
||
94 | $deleteApprovalRequest = RequestValidation::validateDeleteApprovalRequest($request); |
||
95 | |||
96 | $approval = new Approval( |
||
97 | $userInfo->getUserId(), |
||
98 | $deleteApprovalRequest['client_id'], |
||
99 | $deleteApprovalRequest['response_type'], |
||
100 | $deleteApprovalRequest['scope'] |
||
101 | ); |
||
102 | $this->approvalManagementStorage->deleteApproval($approval); |
||
103 | |||
104 | return new RedirectResponse($request->getUrl()->getRootUrl().'_account', 302); |
||
105 | }, |
||
106 | array( |
||
107 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
108 | 'activate' => array('user'), |
||
109 | ), |
||
110 | ) |
||
111 | ); |
||
112 | |||
113 | $this->get( |
||
114 | '/.well-known/webfinger', |
||
115 | function (Request $request) { |
||
116 | $resource = $request->getUrl()->getQueryParameter('resource'); |
||
117 | if (null === $resource) { |
||
118 | throw new BadRequestException('resource parameter missing'); |
||
119 | } |
||
120 | if (0 !== strpos($resource, 'acct:')) { |
||
121 | throw new BadRequestException('unsupported resource type'); |
||
122 | } |
||
123 | $userAddress = substr($resource, 5); |
||
124 | $atPos = strpos($userAddress, '@'); |
||
125 | if (false === $atPos) { |
||
126 | throw new BadRequestException('invalid user address'); |
||
127 | } |
||
128 | $user = substr($userAddress, 0, $atPos); |
||
129 | |||
130 | $webFingerData = array( |
||
131 | 'links' => array( |
||
132 | array( |
||
133 | 'href' => sprintf('%s%s', $request->getUrl()->getRootUrl(), $user), |
||
134 | 'properties' => array( |
||
135 | 'http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-05', |
||
136 | 'http://remotestorage.io/spec/web-authoring' => null, |
||
137 | 'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getUrl()->getRootUrl(), $user), |
||
138 | 'http://tools.ietf.org/html/rfc6750#section-2.3' => null, |
||
139 | 'http://tools.ietf.org/html/rfc7233' => 'development' !== $this->options['server_mode'] ? 'GET' : null, |
||
140 | ), |
||
141 | 'rel' => 'http://tools.ietf.org/id/draft-dejong-remotestorage', |
||
142 | ), |
||
143 | // legacy -03 WebFinger response |
||
144 | array( |
||
145 | 'href' => sprintf('%s%s', $request->getUrl()->getRootUrl(), $user), |
||
146 | 'properties' => array( |
||
147 | 'http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-03', |
||
148 | 'http://tools.ietf.org/html/rfc2616#section-14.16' => 'development' !== $this->options['server_mode'] ? 'GET' : false, |
||
149 | 'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getUrl()->getRootUrl(), $user), |
||
150 | 'http://tools.ietf.org/html/rfc6750#section-2.3' => false, |
||
151 | ), |
||
152 | 'rel' => 'remotestorage', |
||
153 | ), |
||
154 | ), |
||
155 | ); |
||
156 | |||
157 | $response = new Response(200, 'application/jrd+json'); |
||
158 | $response->setHeader('Access-Control-Allow-Origin', '*'); |
||
159 | $response->setBody( |
||
160 | Json::encode($webFingerData) |
||
161 | ); |
||
162 | |||
163 | return $response; |
||
164 | }, |
||
165 | array( |
||
166 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
167 | 'enabled' => false, |
||
168 | ), |
||
169 | ) |
||
170 | ); |
||
171 | |||
172 | $this->get( |
||
173 | '/', |
||
174 | function (Request $request, UserInfoInterface $userInfo = null) { |
||
175 | return $this->templateManager->render( |
||
176 | 'indexPage', |
||
177 | array( |
||
178 | 'user_id' => null !== $userInfo ? $userInfo->getUserId() : null, |
||
179 | 'show_account_icon' => true, |
||
180 | ) |
||
181 | ); |
||
182 | }, |
||
183 | array( |
||
184 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
185 | 'activate' => array('user'), |
||
186 | 'require' => false, |
||
187 | ), |
||
188 | ) |
||
189 | ); |
||
190 | |||
191 | $this->addRoute( |
||
192 | ['GET', 'HEAD'], |
||
193 | '*', |
||
194 | function (Request $request, TokenInfo $tokenInfo = null) { |
||
195 | $response = $this->getObject($request, $tokenInfo); |
||
196 | $this->addNoCache($response); |
||
197 | $this->addCors($response); |
||
198 | |||
199 | return $response; |
||
200 | }, |
||
201 | array( |
||
202 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
203 | 'activate' => array('api'), |
||
204 | 'require' => false, |
||
205 | ), |
||
206 | ) |
||
207 | ); |
||
208 | |||
209 | // put a document |
||
210 | $this->put( |
||
211 | '*', |
||
212 | function (Request $request, TokenInfo $tokenInfo) { |
||
213 | $response = $this->putDocument($request, $tokenInfo); |
||
214 | $this->addCors($response); |
||
215 | |||
216 | return $response; |
||
217 | }, |
||
218 | array( |
||
219 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
220 | 'activate' => array('api'), |
||
221 | ), |
||
222 | 'fkooman\Rest\Plugin\ReferrerCheck\ReferrerCheckPlugin' => array( |
||
223 | 'enabled' => false, |
||
224 | ), |
||
225 | ) |
||
226 | ); |
||
227 | |||
228 | // delete a document |
||
229 | $this->delete( |
||
230 | '*', |
||
231 | function (Request $request, TokenInfo $tokenInfo) { |
||
232 | $response = $this->deleteDocument($request, $tokenInfo); |
||
233 | $this->addCors($response); |
||
234 | |||
235 | return $response; |
||
236 | }, |
||
237 | array( |
||
238 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array( |
||
239 | 'activate' => array('api'), |
||
240 | ), |
||
241 | 'fkooman\Rest\Plugin\ReferrerCheck\ReferrerCheckPlugin' => array( |
||
242 | 'enabled' => false, |
||
243 | ), |
||
244 | ) |
||
245 | ); |
||
246 | |||
247 | // options request |
||
248 | $this->options( |
||
249 | '*', |
||
250 | function (Request $request) { |
||
|
|||
251 | $response = new Response(); |
||
252 | $response->setHeader( |
||
253 | 'Access-Control-Allow-Methods', |
||
254 | 'GET, PUT, DELETE, HEAD, OPTIONS' |
||
255 | ); |
||
256 | $response->setHeader( |
||
257 | 'Access-Control-Allow-Headers', |
||
258 | 'Authorization, Content-Length, Content-Type, Origin, X-Requested-With, If-Match, If-None-Match' |
||
259 | ); |
||
260 | |||
261 | return $response; |
||
262 | }, |
||
263 | array( |
||
264 | 'fkooman\Rest\Plugin\Authentication\AuthenticationPlugin' => array('enabled' => false), |
||
265 | ) |
||
266 | ); |
||
267 | } |
||
268 | |||
604 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.