CommentRepository::findByUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Kreta\Component\Comment\Repository;
14
15
use Kreta\Component\Core\Repository\EntityRepository;
16
use Kreta\Component\Issue\Model\Interfaces\IssueInterface;
17
use Kreta\Component\User\Model\Interfaces\UserInterface;
18
19
/**
20
 * Class CommentRepository.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 * @author Gorka Laucirica <[email protected]>
24
 */
25
class CommentRepository extends EntityRepository
26
{
27
    /**
28
     * Finds all the comments of issue given ordering by createdAt.
29
     * The result can be more strict adding user and createAt criteria.
30
     * Can do limit and offset.
31
     *
32
     * @param \Kreta\Component\Issue\Model\Interfaces\IssueInterface $issue     The issue
33
     * @param \DateTime                                              $createdAt The created at
0 ignored issues
show
Documentation introduced by
Should the type for parameter $createdAt not be null|\DateTime?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     * @param string                                                 $writtenBy The email of user
0 ignored issues
show
Documentation introduced by
Should the type for parameter $writtenBy not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
35
     * @param int                                                    $limit     The limit
0 ignored issues
show
Documentation introduced by
Should the type for parameter $limit not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
36
     * @param int                                                    $offset    The offset
0 ignored issues
show
Documentation introduced by
Should the type for parameter $offset not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
     *
38
     * @return \Kreta\Component\Comment\Model\Interfaces\CommentInterface[]
39
     */
40
    public function findByIssue(
41
        IssueInterface $issue,
42
        \DateTime $createdAt = null,
43
        $writtenBy = null,
44
        $limit = null,
45
        $offset = null
46
    ) {
47
        $queryBuilder = $this->getQueryBuilder();
48
        if ($createdAt instanceof \DateTime) {
49
            $this->addCriteria($queryBuilder, ['between' => ['createdAt' => $createdAt]]);
0 ignored issues
show
Documentation introduced by
array('between' => array...atedAt' => $createdAt)) is of type array<string,array<strin...object<DateTime>\"}>"}>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
        }
51
        if ($writtenBy !== null) {
52
            $this->addCriteria($queryBuilder, ['wb.email' => $writtenBy]);
53
        }
54
        $this->addCriteria($queryBuilder, ['issue' => $issue]);
0 ignored issues
show
Documentation introduced by
array('issue' => $issue) is of type array<string,object<Kret...ces\\IssueInterface>"}>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        $this->orderBy($queryBuilder, ['createdAt' => 'ASC']);
56
57
        if ($limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null 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...
58
            $queryBuilder->setMaxResults($limit);
59
        }
60
        if ($offset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type integer|null 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...
61
            $queryBuilder->setFirstResult($offset);
62
        }
63
64
        return $queryBuilder->getQuery()->getResult();
65
    }
66
67
    /**
68
     * Finds the comments of given id if the user given is its writer.
69
     *
70
     * @param string                                               $commentId The comment id
71
     * @param \Kreta\Component\User\Model\Interfaces\UserInterface $user      The user
72
     *
73
     * @return \Kreta\Component\Comment\Model\Interfaces\CommentInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be \Kreta\Component\Comment...s\CommentInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function findByUser($commentId, UserInterface $user)
76
    {
77
        return $this->findOneBy(['id' => $commentId, 'writtenBy' => $user], false);
0 ignored issues
show
Documentation introduced by
array('id' => $commentId, 'writtenBy' => $user) is of type array<string,string|obje...aces\\UserInterface>"}>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function getQueryBuilder()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
84
    {
85
        return parent::getQueryBuilder()
86
            ->addSelect(['i', 'wb'])
87
            ->join('c.issue', 'i')
88
            ->join('c.writtenBy', 'wb');
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function getAlias()
95
    {
96
        return 'c';
97
    }
98
}
99