Completed
Push — master ( b1054d...eba5f2 )
by Christian
07:28 queued 11s
created

ExceptionHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 151
Duplicated Lines 35.76 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 69.09%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 54
loc 151
ccs 38
cts 55
cp 0.6909
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribingMethods() 0 29 1
A serializeToJson() 0 10 1
A serializeErrorToJson() 0 10 1
A serializeToXml() 27 27 4
A serializeErrorToXml() 27 27 4
A convertToArray() 0 4 1
A convertThrowableToArray() 0 12 3

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
/*
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 since 2.8
22
 */
23
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface
24
{
25
    /**
26
     * @return array
27
     */
28 3
    public static function getSubscribingMethods()
29
    {
30
        return [
31
            [
32 3
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
33 3
                'format' => 'json',
34
                'type' => \Error::class,
35 3
                'method' => 'serializeErrorToJson',
36
            ],
37
            [
38 3
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
39 3
                'format' => 'json',
40
                'type' => \Exception::class,
41 3
                'method' => 'serializeToJson',
42
            ],
43
            [
44 3
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
45 3
                'format' => 'xml',
46
                'type' => \Error::class,
47 3
                'method' => 'serializeErrorToXml',
48
            ],
49
            [
50 3
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
51 3
                'format' => 'xml',
52
                'type' => \Exception::class,
53 3
                'method' => 'serializeToXml',
54
            ],
55
        ];
56
    }
57
58
    /**
59
     * @param JsonSerializationVisitor $visitor
60
     * @param \Exception               $exception
61
     * @param array                    $type
62
     * @param Context                  $context
63
     *
64
     * @return array
65
     */
66 2
    public function serializeToJson(
67
        JsonSerializationVisitor $visitor,
68
        \Exception $exception,
69
        array $type,
70
        Context $context
71
    ) {
72 2
        $data = $this->convertToArray($exception, $context);
73
74 2
        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
    public function serializeErrorToJson(
78
        JsonSerializationVisitor $visitor,
79
        \Throwable $exception,
80
        array $type,
81
        Context $context
82
    ) {
83
        $data = $this->convertThrowableToArray($exception, $context);
84
85
        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...
86
    }
87
88
    /**
89
     * @param XmlSerializationVisitor $visitor
90
     * @param \Exception              $exception
91
     * @param array                   $type
92
     * @param Context                 $context
93
     */
94 1 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...
95
        XmlSerializationVisitor $visitor,
96
        \Exception $exception,
97
        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...
98
        Context $context
99
    ) {
100 1
        $data = $this->convertToArray($exception, $context);
101
102 1
        $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...
103
104 1
        if (!$visitor->getCurrentNode()) {
105 1
            $visitor->createRoot();
106
        }
107
108 1
        foreach ($data as $key => $value) {
109 1
            $entryNode = $document->createElement($key);
110 1
            $visitor->getCurrentNode()->appendChild($entryNode);
111 1
            $visitor->setCurrentNode($entryNode);
112
113 1
            $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...
114 1
            if (null !== $node) {
115 1
                $visitor->getCurrentNode()->appendChild($node);
116
            }
117
118 1
            $visitor->revertCurrentNode();
119
        }
120 1
    }
121
122 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...
123
        XmlSerializationVisitor $visitor,
124
        \Throwable $exception,
125
        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...
126
        Context $context
127
    ) {
128
        $data = $this->convertThrowableToArray($exception, $context);
129
130
        $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...
131
132
        if (!$visitor->getCurrentNode()) {
133
            $visitor->createRoot();
134
        }
135
136
        foreach ($data as $key => $value) {
137
            $entryNode = $document->createElement($key);
138
            $visitor->getCurrentNode()->appendChild($entryNode);
139
            $visitor->setCurrentNode($entryNode);
140
141
            $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...
142
            if (null !== $node) {
143
                $visitor->getCurrentNode()->appendChild($node);
144
            }
145
146
            $visitor->revertCurrentNode();
147
        }
148
    }
149
150
    /**
151
     * @param \Exception $exception
152
     * @param Context    $context
153
     *
154
     * @return array
155
     */
156 3
    protected function convertToArray(\Exception $exception, Context $context)
157
    {
158 3
        return $this->convertThrowableToArray($exception, $context);
159
    }
160
161 3
    private function convertThrowableToArray(\Throwable $throwable, Context $context): array
162
    {
163 3
        $data = [];
164
165 3
        if ($context->hasAttribute('status_code')) {
166 3
            $data['code'] = $context->getAttribute('status_code');
167
        }
168
169 3
        $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...
170
171 3
        return $data;
172
    }
173
}
174