| Conditions | 12 |
| Paths | 17 |
| Total Lines | 29 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 34 | private function checkSecurity(Comment $comment, Operation $operation, array $context): void |
||
| 35 | { |
||
| 36 | $operationName = $operation->getName(); |
||
| 37 | |||
| 38 | // Vérifier l'authentification pour POST et PUT |
||
| 39 | if (in_array($operationName, ['post', 'put'])) { |
||
| 40 | if (!$this->security->isGranted('ROLE_USER')) { |
||
| 41 | throw new AccessDeniedException('Authentication required to manage comments'); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | // Pour PUT, vérifier que l'utilisateur peut modifier ce commentaire |
||
| 46 | if ($operationName === 'put') { |
||
| 47 | $currentUser = $this->security->getUser(); |
||
| 48 | |||
| 49 | // Si c'est une création depuis la base, récupérer l'entité existante |
||
| 50 | if (!$comment->getUser() && isset($context['previous_data'])) { |
||
| 51 | $previousComment = $context['previous_data']; |
||
| 52 | if ($previousComment instanceof Comment && $previousComment->getUser() !== $currentUser) { |
||
| 53 | throw new AccessDeniedException('You can only modify your own comments'); |
||
| 54 | } |
||
| 55 | } elseif ($comment->getUser() && $comment->getUser() !== $currentUser) { |
||
| 56 | throw new AccessDeniedException('You can only modify your own comments'); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | // Pour POST, s'assurer que l'utilisateur est défini |
||
| 61 | if ($operationName === 'post' && !$comment->getUser()) { |
||
| 62 | $comment->setUser($this->security->getUser()); |
||
| 63 | } |
||
| 66 |