Completed
Push — master ( 957717...7a0390 )
by Christian
02:31 queued 11s
created

ExceptionHandler::serializeErrorToJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Serializer\Normalizer;
13
14
use JMS\Serializer\Context;
15
use JMS\Serializer\GraphNavigatorInterface;
16
use JMS\Serializer\Handler\SubscribingHandlerInterface;
17
use JMS\Serializer\JsonSerializationVisitor;
18
use JMS\Serializer\XmlSerializationVisitor;
19
20
/**
21
 * @internal
22
 */
23
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface
24
{
25
    public static function getSubscribingMethods(): array
26
    {
27
        return [
28
            [
29
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
30
                'format' => 'json',
31
                'type' => \Error::class,
32
                'method' => 'serializeErrorToJson',
33
            ],
34
            [
35
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
36
                'format' => 'json',
37
                'type' => \Exception::class,
38
                'method' => 'serializeToJson',
39
            ],
40
            [
41
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
42
                'format' => 'xml',
43
                'type' => \Error::class,
44
                'method' => 'serializeErrorToXml',
45
            ],
46
            [
47
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
48
                'format' => 'xml',
49
                'type' => \Exception::class,
50
                'method' => 'serializeToXml',
51
            ],
52
        ];
53
    }
54
55
    public function serializeToJson(
56
        JsonSerializationVisitor $visitor,
57
        \Exception $exception,
58
        array $type,
59
        Context $context
60
    ) {
61
        $data = $this->convertToArray($exception, $context);
62
63
        return $visitor->visitArray($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JsonSerializationVisitor::visitArray() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
64
    }
65
66
    public function serializeErrorToJson(
67
        JsonSerializationVisitor $visitor,
68
        \Throwable $exception,
69
        array $type,
70
        Context $context
71
    ) {
72
        $data = $this->convertToArray($exception, $context);
73
74
        return $visitor->visitArray($data, $type, $context);
0 ignored issues
show
Unused Code introduced by
The call to JsonSerializationVisitor::visitArray() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
    }
76
77 View Code Duplication
    public function serializeToXml(
0 ignored issues
show
Duplication introduced by
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
        XmlSerializationVisitor $visitor,
79
        \Exception $exception,
80
        array $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

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

Loading history...
81
        Context $context
82
    ) {
83
        $data = $this->convertToArray($exception, $context);
84
85
        $document = $visitor->getDocument(true);
0 ignored issues
show
Unused Code introduced by
The call to XmlSerializationVisitor::getDocument() has too many arguments starting with true.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
87
        if (!$visitor->getCurrentNode()) {
88
            $visitor->createRoot();
89
        }
90
91
        foreach ($data as $key => $value) {
92
            $entryNode = $document->createElement($key);
93
            $visitor->getCurrentNode()->appendChild($entryNode);
94
            $visitor->setCurrentNode($entryNode);
95
96
            $node = $context->getNavigator()->accept($value, null, $context);
0 ignored issues
show
Unused Code introduced by
The call to GraphNavigatorInterface::accept() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97
            if (null !== $node) {
98
                $visitor->getCurrentNode()->appendChild($node);
99
            }
100
101
            $visitor->revertCurrentNode();
102
        }
103
    }
104
105 View Code Duplication
    public function serializeErrorToXml(
0 ignored issues
show
Duplication introduced by
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...
106
        XmlSerializationVisitor $visitor,
107
        \Throwable $exception,
108
        array $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

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

Loading history...
109
        Context $context
110
    ) {
111
        $data = $this->convertToArray($exception, $context);
112
113
        $document = $visitor->getDocument(true);
0 ignored issues
show
Unused Code introduced by
The call to XmlSerializationVisitor::getDocument() has too many arguments starting with true.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
115
        if (!$visitor->getCurrentNode()) {
116
            $visitor->createRoot();
117
        }
118
119
        foreach ($data as $key => $value) {
120
            $entryNode = $document->createElement($key);
121
            $visitor->getCurrentNode()->appendChild($entryNode);
122
            $visitor->setCurrentNode($entryNode);
123
124
            $node = $context->getNavigator()->accept($value, null, $context);
0 ignored issues
show
Unused Code introduced by
The call to GraphNavigatorInterface::accept() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
125
            if (null !== $node) {
126
                $visitor->getCurrentNode()->appendChild($node);
127
            }
128
129
            $visitor->revertCurrentNode();
130
        }
131
    }
132
133
    private function convertToArray(\Throwable $throwable, Context $context): array
134
    {
135
        $data = [];
136
137
        if ($context->hasAttribute('status_code')) {
138
            $data['code'] = $context->getAttribute('status_code');
139
        }
140
141
        $data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode : null);
0 ignored issues
show
Bug introduced by
The variable $statusCode seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
142
143
        return $data;
144
    }
145
}
146