Completed
Push — master ( 749edd...721fe7 )
by Park Jong-Hun
10:14
created

Comment   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 39.47 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 5
dl 15
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A showEditForm() 0 6 1
A edit() 0 12 1
A fillUserInfomanionByLoginAccount() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Controller;
4
5
use Core\ViewModel;
6
use Core\Application;
7
use App\Entity\Comment as CommentModel;
8
use App\Entity\User as UserModel;
9
use App\Auth\LoginManagerInterface;
10
use Doctrine\ORM\EntityManagerInterface;
11
use \DateTime;
12
13
class Comment
14
{
15
    public function showEditForm($id, EntityManagerInterface $entityManager, ViewModel $viewModel)
16
    {
17
        $viewModel->set('comment', $entityManager->getRepository(CommentModel::class)->find($id));
18
19
        return 'default/commentForm';
20
    }
21
22
    public function edit($id, $parsedBody, EntityManagerInterface $entityManager, LoginManagerInterface $loginManager, ViewModel $viewModel)
0 ignored issues
show
Unused Code introduced by
The parameter $viewModel is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
    {
24
        /** @var CommentModel */
25
        $comment = $entityManager->getRepository(CommentModel::class)->find($id);
26
        $comment->setContent($parsedBody['content']);
27
        $comment->setUpdatedAt(new DateTime());
28
        $this->fillUserInfomanionByLoginAccount($comment, $parsedBody, $loginManager, $entityManager);
29
30
        $entityManager->flush();
31
32
        return 'redirect: ' . Application::getUrl('/post/' . $comment->getPost()->getId());
33
    }
34
35 View Code Duplication
    private function fillUserInfomanionByLoginAccount($userContent, $parsedBody, LoginManagerInterface $loginManager, EntityManagerInterface $entityManager)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        if ($loginManager->getLoggedAccountId()) {
38
            /** @var UserModel */
39
            $user = $entityManager->getRepository(UserModel::class)
40
                        ->findOneBy(['accountId' => $loginManager->getLoggedAccountId()]);
41
42
            $userContent->setUser($user);
43
            $userContent->setAuthor($user->getNickname());
44
            $userContent->setPassword($user->getPassword());
45
        } else {
46
            $userContent->setAuthor($parsedBody['author']);
47
            $userContent->setPassword($parsedBody['password']);
48
        }
49
    }
50
}
51