Passed
Push — ref ( 45b89b )
by Asmir
02:50
created

HeaderHandler::handleOptions()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 18
rs 9.6111
cc 5
nc 5
nop 2
1
<?php
2
3
namespace GoetasWebservices\SoapServices\Metadata\Arguments\Headers\Handler;
4
5
use GoetasWebservices\SoapServices\Metadata\Arguments\Headers\Header;
6
use GoetasWebservices\SoapServices\Metadata\Arguments\Headers\HeaderBag;
7
use JMS\Serializer\DeserializationContext;
8
use JMS\Serializer\GraphNavigator;
9
use JMS\Serializer\Handler\SubscribingHandlerInterface;
10
use JMS\Serializer\Metadata\StaticPropertyMetadata;
11
use JMS\Serializer\SerializationContext;
12
use JMS\Serializer\XmlDeserializationVisitor;
13
use JMS\Serializer\XmlSerializationVisitor;
14
use Symfony\Component\DependencyInjection\SimpleXMLElement;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...ection\SimpleXMLElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class HeaderHandler implements SubscribingHandlerInterface
17
{
18
    const SOAP = 'http://schemas.xmlsoap.org/soap/envelope/';
19
    const SOAP_12 = 'http://www.w3.org/2003/05/soap-envelope';
20
21
22
    protected $headerData = [];
23
24
    public static function getSubscribingMethods()
25
    {
26
        return array(
27
            array(
28
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
29
                'format' => 'xml',
30
                'type' => HeaderPlaceholder::class,
31
                'method' => 'serializeHeaderPlaceholder'
32
            ),
33
            array(
34
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
35
                'format' => 'xml',
36
                'type' => HeaderPlaceholder::class,
37
                'method' => 'deserializeHeaderPlaceholder'
38
            ),
39
            array(
40
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
41
                'format' => 'xml',
42
                'type' => 'GoetasWebservices\SoapServices\SoapEnvelope\Header',
43
                'method' => 'deserializeHeader'
44
            ),
45
            array(
46
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
47
                'format' => 'xml',
48
                'type' => Header::class,
49
                'method' => 'serializeHeader'
50
            ),
51
        );
52
    }
53
54
    public function deserializeHeaderPlaceholder(XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    public function deserializeHeaderPlaceholder(/** @scrutinizer ignore-unused */ XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $type = array('name' => $type['params'][0], 'params' => []);
57
58
        return $context->getNavigator()->accept($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\GraphNavigatorInterface::accept() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        return $context->getNavigator()->/** @scrutinizer ignore-call */ accept($data, $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
    }
60
61
    public function deserializeHeader(XmlDeserializationVisitor $visitor, \SimpleXMLElement $data, array $type, DeserializationContext $context)
62
    {
63
        $type = array('name' => $type['params'][0], 'params' => []);
64
65
        $return = $context->getNavigator()->accept($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\GraphNavigatorInterface::accept() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $return = $context->getNavigator()->/** @scrutinizer ignore-call */ accept($data, $type, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
67
        $mustUnderstandAttr = $data->attributes(self::SOAP_12)->mustUnderstand ?: $data->attributes(self::SOAP)->mustUnderstand;
68
        $mustUnderstand = $mustUnderstandAttr !== null && $visitor->visitBoolean($mustUnderstandAttr, [], $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\XmlDeseri...Visitor::visitBoolean() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $mustUnderstand = $mustUnderstandAttr !== null && $visitor->/** @scrutinizer ignore-call */ visitBoolean($mustUnderstandAttr, [], $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
69
        /**
70
         * @var HeaderBag $headerBag
71
         */
72
        $headerBag = $context->getAttribute('headers_bag');
73
74
        if ($mustUnderstand) {
75
            $headerBag->addMustUnderstandHeader($return);
76
        } else {
77
            $headerBag->addHeader($return);
78
        }
79
80
        return $return;
81
    }
82
83
    public function serializeHeaderPlaceholder(XmlSerializationVisitor $visitor, HeaderPlaceholder $data, array $type, SerializationContext $context)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

83
    public function serializeHeaderPlaceholder(XmlSerializationVisitor $visitor, HeaderPlaceholder $data, /** @scrutinizer ignore-unused */ array $type, SerializationContext $context)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        /**
86
         * @var HeaderBag $headerBag
87
         */
88
        $headerBag = $context->getAttribute('headers_bag');
0 ignored issues
show
Unused Code introduced by
The assignment to $headerBag is dead and can be removed.
Loading history...
89
90
        $factory = $context->getMetadataFactory();
91
        /**
92
         * @var $header Header
93
         */
94
        foreach ($this->headerData[spl_object_hash($data)] as $header) {
95
            /**
96
             * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
97
             */
98
            $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
99
100
            $name = ($pos = strpos($classMetadata->xmlRootName, ':')) !== false ? substr($classMetadata->xmlRootName, $pos + 1) : $classMetadata->xmlRootName;
101
102
            $metadata = new StaticPropertyMetadata($classMetadata->name, $name, $header->getData());
103
            $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
104
            $metadata->serializedName = $name;
105
106
            $visitor->visitProperty($metadata, $header->getData(), $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\XmlSerial...isitor::visitProperty() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $visitor->/** @scrutinizer ignore-call */ 
107
                      visitProperty($metadata, $header->getData(), $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
107
108
            $this->handleOptions($visitor, $header);
109
        }
110
    }
111
112
113
    public function serializeHeader(XmlSerializationVisitor $visitor, Header $header, array $type, SerializationContext $context)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function serializeHeader(XmlSerializationVisitor $visitor, Header $header, /** @scrutinizer ignore-unused */ array $type, SerializationContext $context)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
116
        $factory = $context->getMetadataFactory();
117
118
        /**
119
         * @var $classMetadata \JMS\Serializer\Metadata\ClassMetadata
120
         */
121
        $classMetadata = $factory->getMetadataForClass(get_class($header->getData()));
122
123
        $name = ($pos = strpos($classMetadata->xmlRootName, ':')) !== false ? substr($classMetadata->xmlRootName, $pos + 1) : $classMetadata->xmlRootName;
124
125
        $metadata = new StaticPropertyMetadata($classMetadata->name, $name, $header->getData());
126
        $metadata->xmlNamespace = $classMetadata->xmlRootNamespace;
127
        $metadata->serializedName = $name;
128
129
        $visitor->visitProperty($metadata, $header->getData(), $context);
0 ignored issues
show
Unused Code introduced by
The call to JMS\Serializer\XmlSerial...isitor::visitProperty() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        $visitor->/** @scrutinizer ignore-call */ 
130
                  visitProperty($metadata, $header->getData(), $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
130
131
        $this->handleOptions($visitor, $header->getOptions());
132
    }
133
134
    private function handleOptions(XmlSerializationVisitor $visitor, $options)
135
    {
136
        if (!count($options)) {
137
            return;
138
        }
139
        /**
140
         * @var $currentNode \DOMNode
141
         */
142
        $currentNode = $visitor->getCurrentNode();
143
        foreach ($options as $option => $value) {
144
            if (in_array($option, ['mustUnderstand', 'required', 'role', 'actor'])) {
145
146
                if ($currentNode->ownerDocument->documentElement->namespaceURI === self::SOAP_12) {
147
                    $envelopeNS = self::SOAP_12;
148
                } else {
149
                    $envelopeNS = self::SOAP;
150
                }
151
                $this->setAttributeOnNode($currentNode->lastChild, $option, $value, $envelopeNS);
152
            }
153
        }
154
    }
155
156
    private function setAttributeOnNode(\DOMElement $node, $name, $value, $namespace)
157
    {
158
        if (!($prefix = $node->lookupPrefix($namespace)) && !($prefix = $node->ownerDocument->lookupPrefix($namespace))) {
159
            $prefix = 'ns-' . substr(sha1($namespace), 0, 8);
160
        }
161
        $node->setAttributeNS($namespace, $prefix . ':' . $name, is_bool($value) || is_null($value) ? ($value ? 'true' : 'false') : $value);
162
    }
163
}
164
165
166
167