Conditions | 7 |
Paths | 48 |
Total Lines | 61 |
Code Lines | 36 |
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 |
||
55 | { |
||
56 | $parameters = [ |
||
57 | 'title' => 'Add post', |
||
58 | 'action' => ['blog/add'], |
||
59 | 'errors' => [], |
||
60 | 'body' => $request->getParsedBody(), |
||
61 | ]; |
||
62 | |||
63 | if ($request->getMethod() === Method::POST) { |
||
64 | $form->load($parameters['body']); |
||
65 | if ($form->validate()) { |
||
66 | $this->postService->savePost($this->userService->getUser(), new Post(), $form); |
||
67 | return $this->webService->getRedirectResponse('blog/index'); |
||
68 | } |
||
69 | |||
70 | $parameters['errors'] = $form->firstErrors(); |
||
71 | } |
||
72 | |||
73 | return $this->viewRenderer->withCsrf()->render('__form', $parameters); |
||
74 | } |
||
75 | |||
76 | public function edit(Request $request, PostForm $form, PostRepository $postRepository): Response |
||
77 | { |
||
78 | $slug = $request->getAttribute('slug', null); |
||
79 | $post = $postRepository->fullPostPage($slug); |
||
80 | if ($post === null) { |
||
81 | return $this->webService->getNotFoundResponse(); |
||
82 | } |
||
83 | |||
84 | $parameters = [ |
||
85 | 'title' => 'Edit post', |
||
86 | 'action' => ['blog/edit', ['slug' => $slug]], |
||
87 | 'errors' => [], |
||
88 | 'body' => [ |
||
89 | 'title' => $post->getTitle(), |
||
90 | 'content' => $post->getContent(), |
||
91 | 'tags' => $this->postService->getPostTags($post) |
||
92 | ] |
||
93 | ]; |
||
94 | |||
95 | if ($request->getMethod() === Method::POST) { |
||
96 | $body = $request->getParsedBody(); |
||
97 | $form->load($body); |
||
98 | if ($form->validate()) { |
||
99 | $this->postService->savePost($this->userService->getUser(), $post, $form); |
||
100 | return $this->webService->getRedirectResponse('blog/index'); |
||
101 | } |
||
102 | |||
103 | $parameters['body'] = $body; |
||
104 | $parameters['errors'] = $form->firstErrors(); |
||
105 | } |
||
106 | |||
107 | return $this->viewRenderer->withCsrf()->render('__form', $parameters); |
||
108 | } |
||
109 | } |
||
110 |