Passed
Push — master ( fdf037...88d356 )
by Adrien
07:14
created

getSubscriptionLastReviewNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Traits;
6
7
use Application\Model\Product;
8
use Application\Repository\ProductRepository;
9
use Doctrine\ORM\Mapping as ORM;
10
use InvalidArgumentException;
11
12
trait HasSubscriptionLastReview
13
{
14
    /**
15
     * @var null|Product
16
     *
17
     * @ORM\ManyToOne(targetEntity="Product")
18
     * @ORM\JoinColumns({
19
     *     @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
20
     * })
21
     */
22
    private $subscriptionLastReview;
23
24
    /**
25
     * Get last review number available through a subscription, bypassing all ACL so it also work even if review is not active yet
26
     */
27 2
    public function getSubscriptionLastReviewNumber(): ?int
28
    {
29
        /** @var ProductRepository $productRepository */
30 2
        $productRepository = _em()->getRepository(Product::class);
31
32 2
        return $productRepository->getSubscriptionLastReviewNumber($this);
1 ignored issue
show
Bug introduced by
$this of type Application\Traits\HasSubscriptionLastReview is incompatible with the type Application\Model\AbstractModel expected by parameter $hasSubscriptionLastReview of Application\Repository\P...ptionLastReviewNumber(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        return $productRepository->getSubscriptionLastReviewNumber(/** @scrutinizer ignore-type */ $this);
Loading history...
33
    }
34
35
    /**
36
     * Set last review available through a subscription
37
     *
38
     * @API\Exclude
39
     */
40
    public function setSubscriptionLastReview(?Product $subscriptionLastReview): void
41
    {
42
        if ($subscriptionLastReview && !$subscriptionLastReview->getReviewNumber()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subscriptionLastReview->getReviewNumber() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. 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...
43
            throw new InvalidArgumentException('The last review of a subscription must be a review, not an article');
44
        }
45
46
        $this->subscriptionLastReview = $subscriptionLastReview;
47
    }
48
}
49