Conditions | 11 |
Paths | 78 |
Total Lines | 51 |
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 |
||
44 | public function onKernelException(GetResponseForExceptionEvent $event) |
||
45 | { |
||
46 | $title = 'システムエラーが発生しました。'; |
||
47 | $message = '大変お手数ですが、サイト管理者までご連絡ください。'; |
||
48 | $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
||
49 | |||
50 | $exception = $event->getException(); |
||
51 | |||
52 | if ($exception instanceof HttpExceptionInterface) { |
||
53 | $statusCode = $exception->getStatusCode(); |
||
54 | switch ($statusCode) { |
||
55 | case 400: |
||
56 | case 401: |
||
57 | case 403: |
||
58 | case 405: |
||
59 | case 406: |
||
60 | $title = 'アクセスできません。'; |
||
61 | if ($exception->getMessage()) { |
||
62 | $message = $exception->getMessage(); |
||
63 | } else { |
||
64 | $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。'; |
||
65 | } |
||
66 | break; |
||
67 | case 404: |
||
68 | $title = 'ページがみつかりません。'; |
||
69 | $message = 'URLに間違いがないかご確認ください。'; |
||
70 | break; |
||
71 | default: |
||
72 | break; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | log_error('システムエラーが発生しました。', [ |
||
77 | $exception->getMessage(), |
||
78 | $exception->getFile(), |
||
79 | $exception->getLine(), |
||
80 | $exception->getTraceAsString() |
||
81 | ]); |
||
82 | |||
83 | try { |
||
84 | $file = $this->requestContext->isAdmin() ? '@admin/error.twig' : 'error.twig'; |
||
85 | $content = $this->twig->render($file, [ |
||
86 | 'error_title' => $title, |
||
87 | 'error_message' => $message, |
||
88 | ]); |
||
89 | } catch (\Exception $ignore) { |
||
90 | $content = $title; |
||
91 | } |
||
92 | |||
93 | $event->setResponse(Response::create($content, $statusCode)); |
||
94 | } |
||
95 | |||
121 |