Test Failed
Push — master ( 5a9473...a9c3a3 )
by Dominik
02:12
created

CallbackLinkNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Psr\Link\LinkInterface;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
10
final class CallbackLinkNormalizer implements LinkNormalizerInterface
11
{
12
    /**
13
     * @var callable
14
     */
15
    private $callback;
16
17
    /**
18
     * @param callable $callback
19
     */
20 2
    public function __construct(callable $callback)
21
    {
22 2
        $this->callback = $callback;
23 2
    }
24
25
    /**
26
     * @param string                     $path
27
     * @param Request                    $request
28
     * @param object                     $object
29
     * @param NormalizerContextInterface $context
30
     *
31
     * @return array|null
32
     */
33 2
    public function normalizeLink(string $path, Request $request, $object, NormalizerContextInterface $context)
34
    {
35 2
        $callback = $this->callback;
36
37 2
        $link = $callback($path, $request, $object, $context);
38
39 2
        if (null === $link) {
40 1
            return null;
41
        }
42
43 1
        if (!$link instanceof LinkInterface) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
44
            // todo: exception
45
        }
46
47
        return [
48 1
            'href' => $link->getHref(),
49 1
            'templated' => $link->isTemplated(),
50 1
            'rel' => $link->getRels(),
51 1
            'attributes' => $link->getAttributes(),
52
        ];
53
    }
54
}
55