Test Setup Failed
Push — graphql_api ( 4ff753 )
by Herberto
07:48 queued 01:35
created

CommentAuthorsResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 42
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A getCommentAuthorsConnection() 16 16 2
A countEdges() 4 4 1

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Presentation\Api\GraphQl\Node\Comment\Connection\Author;
16
17
use Acme\App\Core\Component\Blog\Application\Query\FindAuthorIdByCommentIdQueryInterface;
18
use Acme\App\Core\Component\User\Application\Repository\UserRepositoryInterface;
19
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
20
use Acme\App\Core\SharedKernel\Component\Blog\Domain\Post\Comment\CommentId;
21
use Acme\App\Presentation\Api\GraphQl\Node\User\AbstractUserViewModel;
22
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
23
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;
24
use function count;
25
26 View Code Duplication
final class CommentAuthorsResolver
0 ignored issues
show
Duplication introduced by
This class 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...
27
{
28
    /**
29
     * @var UserRepositoryInterface
30
     */
31
    private $userRepository;
32
33
    /**
34
     * @var FindAuthorIdByCommentIdQueryInterface
35
     */
36
    private $findAuthorIdByCommentIdQuery;
37
38
    public function __construct(
39
        UserRepositoryInterface $userRepository,
40
        FindAuthorIdByCommentIdQueryInterface $findAuthorIdByCommentIdQuery
41
    ) {
42
        $this->userRepository = $userRepository;
43
        $this->findAuthorIdByCommentIdQuery = $findAuthorIdByCommentIdQuery;
44
    }
45
46
    public function getCommentAuthorsConnection(CommentId $commentId): Connection
47
    {
48
        try {
49
            $author = $this->userRepository->findOneById(
50
                $this->findAuthorIdByCommentIdQuery->execute($commentId)
51
            );
52
        } catch (EmptyQueryResultException $e) {
53
            return ConnectionBuilder::connectionFromArray([]);
54
        }
55
56
        return ConnectionBuilder::connectionFromArray(
57
            [
58
                AbstractUserViewModel::constructFromEntity($author),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Acme\App\Presentation\Ap...r\AbstractUserViewModel as the method constructFromEntity() does only exist in the following sub-classes of Acme\App\Presentation\Ap...r\AbstractUserViewModel: Acme\App\Presentation\Ap...er\Admin\AdminViewModel, Acme\App\Presentation\Ap...\Editor\EditorViewModel. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
59
            ]
60
        );
61
    }
62
63
    public function countEdges(Connection $connection): int
64
    {
65
        return count($connection->edges);
66
    }
67
}
68