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.
Completed
Push — master ( 1978d2...e2f699 )
by William
03:55
created

XmlSerializer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 82
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setMetadataFactory() 0 4 1
A serializeLinks() 0 14 3
C serializeEmbeddeds() 0 29 7
B getElementName() 0 15 5
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
        foreach ($links as $link) {
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
                    if (null !== $node = $context->accept($entry)) {
67
                        $visitor->getCurrentNode()->appendChild($node);
68
                    }
69
70
                    $visitor->revertCurrentNode();
71
                }
72
            } elseif (null !== $node = $context->accept($embedded->getData())) {
73
                $visitor->getCurrentNode()->appendChild($node);
74
            }
75
76
            $visitor->revertCurrentNode();
77
        }
78
    }
79
80
    private function getElementName($data, Embedded $embedded = null)
81
    {
82
        $elementName = null;
83
84
        if (null !== $embedded) {
85
            $elementName = $embedded->getXmlElementName();
86
        }
87
88
        if (null == $elementName && is_object($data)) {
0 ignored issues
show
Bug introduced by
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...
89
            $metadata    = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data));
90
            $elementName = $metadata->xmlRootName;
91
        }
92
93
        return $elementName ?: 'entry';
94
    }
95
}
96