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.

XmlSerializer   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 5
dl 21
loc 91
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setMetadataFactory() 0 4 1
A serializeLinks() 11 14 3
B serializeEmbeddeds() 0 27 5
B getElementName() 0 15 5
A acceptDataAndAppend() 10 10 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\Util\ClassUtils;
7
use JMS\Serializer\SerializationContext;
8
use JMS\Serializer\XmlSerializationVisitor;
9
use Metadata\MetadataFactoryInterface;
10
11
/**
12
 * @author Adrien Brault <[email protected]>
13
 */
14
class XmlSerializer implements XmlSerializerInterface, JMSSerializerMetadataAwareInterface
15
{
16
    /**
17
     * @var MetadataFactoryInterface
18
     */
19
    private $metadataFactory;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
25
    {
26
        $this->metadataFactory = $metadataFactory;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function serializeLinks(array $links, XmlSerializationVisitor $visitor, SerializationContext $context)
33
    {
34 View Code Duplication
        foreach ($links as $link) {
0 ignored issues
show
Duplication introduced by Adrien Brault
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...
35
            $linkNode = $visitor->getDocument()->createElement('link');
36
            $visitor->getCurrentNode()->appendChild($linkNode);
37
38
            $linkNode->setAttribute('rel', $link->getRel());
39
            $linkNode->setAttribute('href', $link->getHref());
40
41
            foreach ($link->getAttributes() as $attributeName => $attributeValue) {
42
                $linkNode->setAttribute($attributeName, $attributeValue);
43
            }
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function serializeEmbeddeds(array $embeddeds, XmlSerializationVisitor $visitor, SerializationContext $context)
51
    {
52
        foreach ($embeddeds as $embedded) {
53
            $entryNode = $visitor->getDocument()->createElement($this->getElementName($embedded->getData(), $embedded));
54
55
            $visitor->getCurrentNode()->appendChild($entryNode);
56
            $visitor->setCurrentNode($entryNode);
57
            $visitor->getCurrentNode()->setAttribute('rel', $embedded->getRel());
58
59
            if ($embedded->getData() instanceof \Traversable || is_array($embedded->getData())) {
60
                foreach ($embedded->getData() as $entry) {
61
                    $entryNode = $visitor->getDocument()->createElement($this->getElementName($entry));
62
63
                    $visitor->getCurrentNode()->appendChild($entryNode);
64
                    $visitor->setCurrentNode($entryNode);
65
66
                    $this->acceptDataAndAppend($embedded, $entry, $visitor, $context);
67
68
                    $visitor->revertCurrentNode();
69
                }
70
            } else {
71
                $this->acceptDataAndAppend($embedded, $embedded->getData(), $visitor, $context);
72
            }
73
74
            $visitor->revertCurrentNode();
75
        }
76
    }
77
78
    private function getElementName($data, Embedded $embedded = null)
79
    {
80
        $elementName = null;
81
82
        if (null !== $embedded) {
83
            $elementName = $embedded->getXmlElementName();
84
        }
85
86
        if (null == $elementName && is_object($data)) {
0 ignored issues
show
Bug introduced by Adrien Brault
It seems like you are loosely comparing $elementName of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
87
            $metadata    = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data));
88
            $elementName = $metadata->xmlRootName;
89
        }
90
91
        return $elementName ?: 'entry';
92
    }
93
94 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...
95
    {
96
        $context->pushPropertyMetadata($embedded->getMetadata());
97
98
        if (null !== $node = $context->accept($data)) {
99
            $visitor->getCurrentNode()->appendChild($node);
100
        }
101
102
        $context->popPropertyMetadata();
103
    }
104
}
105