Completed
Push — master ( 51af85...b489fc )
by Pavel
06:35
created

MasterSeoRequest::setDestination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Bankiru\Seo\Listener;
4
5
use Bankiru\Seo\DestinationInterface;
6
use Bankiru\Seo\DestinationMatcherInterface;
7
use Bankiru\Seo\Exception\MatchingException;
8
use Bankiru\Seo\Page\SeoPageInterface;
9
10
final class MasterSeoRequest
11
{
12
    /** @var  DestinationInterface */
13
    private $destination;
14
    /** @var  SeoPageInterface */
15
    private $page;
16
    /** @var  DestinationMatcherInterface */
17
    private $matcher;
18
19
    /**
20
     * MasterSeoRequest constructor.
21
     *
22
     * @param DestinationMatcherInterface $matcher
23
     */
24
    public function __construct(DestinationMatcherInterface $matcher)
25
    {
26
        $this->matcher = $matcher;
27
    }
28
29
30
    /**
31
     * @return DestinationInterface|null
32
     * @throws \LogicException
33
     */
34
    public function getDestination()
35
    {
36
        if (null === $this->destination) {
37
            throw new \LogicException('Destination is not set');
38
        }
39
40
        return $this->destination;
41
    }
42
43
    /**
44
     * @param DestinationInterface $destination
45
     */
46
    public function setDestination(DestinationInterface $destination)
47
    {
48
        $this->destination = $destination;
49
    }
50
51
    /**
52
     * @return SeoPageInterface
53
     * @throws \LogicException
54
     * @throws MatchingException
55
     */
56
    public function getPage()
57
    {
58
        if (null === $this->page) {
59
            $this->page = $this->matcher->match($this->getDestination());
0 ignored issues
show
Bug introduced by
It seems like $this->getDestination() can be null; however, match() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
60
        }
61
62
        return $this->page;
63
    }
64
}
65