GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

XmlHalSerializer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 27.03 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 0
cbo 3
dl 20
loc 74
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B serializeLinks() 0 24 5
B serializeEmbeddeds() 11 30 5
A acceptDataAndAppend() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Hateoas\Serializer;
4
5
use Hateoas\Model\Embedded;
6
use Hateoas\Representation\Resource;
7
use JMS\Serializer\SerializationContext;
8
use JMS\Serializer\XmlSerializationVisitor;
9
10
/**
11
 * @author Adrien Brault <[email protected]>
12
 */
13
class XmlHalSerializer implements XmlSerializerInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function serializeLinks(array $links, XmlSerializationVisitor $visitor, SerializationContext $context)
19
    {
20
        foreach ($links as $link) {
21
            if ('self' === $link->getRel()) {
22
                foreach ($link->getAttributes() as $key => $value) {
23
                    $visitor->getCurrentNode()->setAttribute($key, $value);
24
                }
25
26
                $visitor->getCurrentNode()->setAttribute('href', $link->getHref());
27
28
                continue;
29
            }
30
31
            $linkNode = $visitor->getDocument()->createElement('link');
32
            $visitor->getCurrentNode()->appendChild($linkNode);
33
34
            $linkNode->setAttribute('rel', $link->getRel());
35
            $linkNode->setAttribute('href', $link->getHref());
36
37
            foreach ($link->getAttributes() as $attributeName => $attributeValue) {
38
                $linkNode->setAttribute($attributeName, $attributeValue);
39
            }
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function serializeEmbeddeds(array $embeddeds, XmlSerializationVisitor $visitor, SerializationContext $context)
47
    {
48
        foreach ($embeddeds as $embedded) {
49
            if ($embedded->getData() instanceof \Traversable || is_array($embedded->getData())) {
50 View Code Duplication
                foreach ($embedded->getData() as $data) {
0 ignored issues
show
Duplication introduced by William DURAND
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                    $entryNode = $visitor->getDocument()->createElement('resource');
52
53
                    $visitor->getCurrentNode()->appendChild($entryNode);
54
                    $visitor->setCurrentNode($entryNode);
55
                    $visitor->getCurrentNode()->setAttribute('rel', $embedded->getRel());
56
57
                    $this->acceptDataAndAppend($embedded, $data, $visitor, $context);
58
59
                    $visitor->revertCurrentNode();
60
                }
61
62
                continue;
63
            }
64
65
            $entryNode = $visitor->getDocument()->createElement('resource');
66
67
            $visitor->getCurrentNode()->appendChild($entryNode);
68
            $visitor->setCurrentNode($entryNode);
69
            $visitor->getCurrentNode()->setAttribute('rel', $embedded->getRel());
70
71
            $this->acceptDataAndAppend($embedded, $embedded->getData(), $visitor, $context);
72
73
            $visitor->revertCurrentNode();
74
        }
75
    }
76
77 View Code Duplication
    private function acceptDataAndAppend(Embedded $embedded, $data, XmlSerializationVisitor $visitor, SerializationContext $context)
0 ignored issues
show
Duplication introduced by Asmir Mustafic
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $context->pushPropertyMetadata($embedded->getMetadata());
80
81
        if (null !== $node = $context->accept($data)) {
82
            $visitor->getCurrentNode()->appendChild($node);
83
        }
84
        $context->popPropertyMetadata();
85
    }
86
}
87