Test Failed
Push — master ( 1f8fc2...506f14 )
by Dominik
02:22
created

CallbackLinkNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Psr\Link\LinkInterface;
8
9
final class CallbackLinkNormalizer implements LinkNormalizerInterface
10
{
11
    /**
12
     * @var callable
13
     */
14
    private $callback;
15
16
    /**
17
     * @param callable $callback
18
     */
19
    public function __construct(callable $callback)
20
    {
21
        $this->callback = $callback;
22
    }
23
24
    /**
25
     * @param string                     $path
26
     * @param object              $object
27
     * @param NormalizerContextInterface $context
28
     *
29
     * @return array|null
30
     */
31
    public function normalizeLink(string $path, $object, NormalizerContextInterface $context)
32
    {
33
        $callback = $this->callback;
34
35
        $link = $callback($path, $object, $context);
36
37
        if (null === $link) {
38
            return null;
39
        }
40
41
        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...
42
            // todo: exception
43
        }
44
45
        return [
46
            'href' => $link->getHref(),
47
            'templated' => $link->isTemplated(),
48
            'rel' => $link->getRels(),
49
            'attributes' => $link->getAttributes()
50
        ];
51
    }
52
}
53