Conditions | 9 |
Paths | 13 |
Total Lines | 65 |
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 |
||
107 | public function __invoke(Request $request, $id) |
||
108 | { |
||
109 | $post = $this->postManager->findOneBy([ |
||
110 | 'id' => $id, |
||
111 | ]); |
||
112 | |||
113 | if (!$post instanceof PostInterface) { |
||
114 | throw new NotFoundHttpException(sprintf('Post (%d) not found', $id)); |
||
115 | } |
||
116 | |||
117 | if (!$post->isCommentable()) { |
||
118 | // todo : add notice in event listener |
||
119 | return new RedirectResponse($this->router->generate('sonata_news_view', [ |
||
120 | 'permalink' => $this->blog->getPermalinkGenerator()->generate($post), |
||
121 | ])); |
||
122 | } |
||
123 | |||
124 | $comment = $this->createComment($post); |
||
125 | |||
126 | // NEXT_MAJOR: Remove the if code |
||
127 | if (null !== $this->eventDispatcher) { |
||
128 | $event = new GetResponseCommentEvent($comment, $request); |
||
129 | $this->eventDispatcher->dispatch($event, SonataNewsEvents::COMMENT_INITIALIZE); |
||
130 | |||
131 | if (null !== $event->getResponse()) { |
||
132 | return $event->getResponse(); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | $form = $this->getCommentForm($comment); |
||
137 | $form->handleRequest($request); |
||
138 | |||
139 | if ($form->isSubmitted() && $form->isValid()) { |
||
140 | // NEXT_MAJOR: Remove the if code |
||
141 | if (null !== $this->eventDispatcher) { |
||
142 | $event = new FormEvent($form, $request); |
||
143 | $this->eventDispatcher->dispatch($event, SonataNewsEvents::COMMENT_SUCCESS); |
||
144 | } |
||
145 | |||
146 | $comment = $form->getData(); |
||
147 | |||
148 | $this->commentManager->save($comment); |
||
149 | $this->mailer->sendCommentNotification($comment); |
||
150 | |||
151 | // todo : add notice in event listener |
||
152 | $response = new RedirectResponse($this->router->generate('sonata_news_view', [ |
||
153 | 'permalink' => $this->blog->getPermalinkGenerator()->generate($post), |
||
154 | ])); |
||
155 | |||
156 | // NEXT_MAJOR: Remove the if code |
||
157 | if (null !== $this->eventDispatcher) { |
||
158 | $this->eventDispatcher->dispatch( |
||
159 | new FilterCommentResponseEvent($comment, $request, $response), |
||
160 | SonataNewsEvents::COMMENT_COMPLETED |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | return $response; |
||
165 | } |
||
166 | |||
167 | return $this->render('@SonataNews/Post/view.html.twig', [ |
||
168 | 'post' => $post, |
||
169 | 'form' => $form, |
||
170 | ]); |
||
171 | } |
||
172 | |||
192 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.