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

CommentRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 45.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A findAllByPostId() 20 20 3

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\Core\Component\Blog\Application\Repository\DQL;
16
17
use Acme\App\Core\Component\Blog\Application\Repository\CommentRepositoryInterface;
18
use Acme\App\Core\Component\Blog\Domain\Post\Comment\Comment;
19
use Acme\App\Core\Component\Blog\Domain\Post\PostId;
20
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
21
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
22
use Acme\App\Core\Port\Persistence\ResultCollectionInterface;
23
24
class CommentRepository implements CommentRepositoryInterface
25
{
26
    /**
27
     * @var DqlQueryBuilderInterface
28
     */
29
    private $dqlQueryBuilder;
30
31
    /**
32
     * @var QueryServiceRouterInterface
33
     */
34
    private $queryService;
35
36
    public function __construct(
37
        DqlQueryBuilderInterface $dqlQueryBuilder,
38
        QueryServiceRouterInterface $queryService
39
    ) {
40
        $this->dqlQueryBuilder = $dqlQueryBuilder;
41
        $this->queryService = $queryService;
42
    }
43
44
    /**
45
     * @return Comment[]
46
     */
47 View Code Duplication
    public function findAllByPostId(
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...
48
        PostId $postId,
49
        array $orderByList = ['publishedAt' => 'DESC'],
50
        int $maxResults = null
51
    ): ResultCollectionInterface {
52
        $this->dqlQueryBuilder->create(Comment::class);
53
54
        $this->dqlQueryBuilder->where('Comment.post = :post')
55
            ->setParameter('post', $postId);
56
57
        foreach ($orderByList as $property => $direction) {
58
            $this->dqlQueryBuilder->orderBy('Comment.' . $property, $direction);
59
        }
60
61
        if ($maxResults) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxResults of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
62
            $this->dqlQueryBuilder->setMaxResults($maxResults);
63
        }
64
65
        return $this->queryService->query($this->dqlQueryBuilder->build());
66
    }
67
}
68