Passed
Branch master (bf85d9)
by Johannes
05:40
created

serializeViolationToXml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer\Handler;
20
21
use JMS\Serializer\GraphNavigatorInterface;
22
use JMS\Serializer\JsonSerializationVisitor;
23
use JMS\Serializer\SerializationContext;
24
use JMS\Serializer\XmlSerializationVisitor;
25
use JMS\Serializer\YamlSerializationVisitor;
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\YamlSerializationVisitor 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...
26
use Symfony\Component\Validator\ConstraintViolation;
27
use Symfony\Component\Validator\ConstraintViolationList;
28
29
final class ConstraintViolationHandler implements SubscribingHandlerInterface
30
{
31 258
    public static function getSubscribingMethods()
32
    {
33 258
        $methods = array();
34 258
        $formats = array('xml', 'json');
35 258
        $types = array('Symfony\Component\Validator\ConstraintViolationList' => 'serializeList', 'Symfony\Component\Validator\ConstraintViolation' => 'serializeViolation');
36
37 258
        foreach ($types as $type => $method) {
38 258
            foreach ($formats as $format) {
39 258
                $methods[] = array(
40 258
                    'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
41 258
                    'type' => $type,
42 258
                    'format' => $format,
43 258
                    'method' => $method . 'To' . $format,
44
                );
45
            }
46
        }
47
48 258
        return $methods;
49
    }
50
51 1
    public function serializeListToXml(XmlSerializationVisitor $visitor, ConstraintViolationList $list, array $type)
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

51
    public function serializeListToXml(XmlSerializationVisitor $visitor, ConstraintViolationList $list, /** @scrutinizer ignore-unused */ array $type)

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...
52
    {
53 1
        $currentNode = $visitor->getCurrentNode();
54 1
        if (!$currentNode) {
55 1
            $visitor->createRoot();
56
        }
57
58 1
        foreach ($list as $violation) {
59 1
            $this->serializeViolationToXml($visitor, $violation);
60
        }
61 1
    }
62
63 1
    public function serializeListToJson(JsonSerializationVisitor $visitor, ConstraintViolationList $list, array $type, SerializationContext $context)
0 ignored issues
show
Unused Code introduced by
The parameter $context 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

63
    public function serializeListToJson(JsonSerializationVisitor $visitor, ConstraintViolationList $list, array $type, /** @scrutinizer ignore-unused */ 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...
64
    {
65 1
        return $visitor->visitArray(iterator_to_array($list), $type);
66
    }
67
68 2
    public function serializeViolationToXml(XmlSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
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

68
    public function serializeViolationToXml(XmlSerializationVisitor $visitor, ConstraintViolation $violation, /** @scrutinizer ignore-unused */ array $type = null)

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...
69
    {
70 2
        $violationNode = $visitor->getDocument()->createElement('violation');
71
72 2
        $parent = $visitor->getCurrentNode();
73 2
        if (!$parent) {
74 1
            $visitor->setCurrentAndRootNode($violationNode);
75
        } else {
76 1
            $parent->appendChild($violationNode);
77
        }
78
79 2
        $violationNode->setAttribute('property_path', $violation->getPropertyPath());
80 2
        $violationNode->appendChild($messageNode = $visitor->getDocument()->createElement('message'));
81
82 2
        $messageNode->appendChild($visitor->getDocument()->createCDATASection($violation->getMessage()));
83 2
    }
84
85 2
    public function serializeViolationToJson(JsonSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)
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

85
    public function serializeViolationToJson(JsonSerializationVisitor $visitor, ConstraintViolation $violation, /** @scrutinizer ignore-unused */ array $type = null)

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...
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

85
    public function serializeViolationToJson(/** @scrutinizer ignore-unused */ JsonSerializationVisitor $visitor, ConstraintViolation $violation, array $type = null)

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...
86
    {
87
        $data = array(
88 2
            'property_path' => $violation->getPropertyPath(),
89 2
            'message' => $violation->getMessage()
90
        );
91
92 2
        return $data;
93
    }
94
}
95