Completed
Push — master ( 5c0ef9...f34efe )
by Alex
20s queued 11s
created

ModifiedSince::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Debril\RssAtomBundle\Request;
4
5
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
class ModifiedSince
10
{
11
12
    const HTTP_HEADER_NAME = 'If-Modified-Since';
13
14
    private $value;
15
16 7
    public function __construct(RequestStack $requestStack)
17
    {
18 7
        $this->value = $this->getModifiedSince($requestStack->getCurrentRequest());
0 ignored issues
show
Bug introduced by
It seems like $requestStack->getCurrentRequest() can be null; however, getModifiedSince() 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...
19 7
    }
20
21
    /**
22
     * @return \DateTime
23
     */
24 6
    public function getValue(): \DateTime
25
    {
26 6
        return $this->value;
27
    }
28
29 7
    private function getModifiedSince(Request $request):\DateTime
30
    {
31 7
        if ($request->headers->has(self::HTTP_HEADER_NAME)) {
32 2
            $string = $request->headers->get(self::HTTP_HEADER_NAME);
33 2
            return \DateTime::createFromFormat(\DateTime::RSS, $string);
34
        }
35
36 6
        return new \DateTime('@1');
37
    }
38
}
39