getSubscribingMethods()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Handler;
6
7
use JMS\Serializer\GraphNavigatorInterface;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\Type\Type;
10
use JMS\Serializer\Visitor\SerializationVisitorInterface;
11
use JMS\Serializer\XmlSerializationVisitor;
12
use Symfony\Component\Validator\ConstraintViolation;
13
use Symfony\Component\Validator\ConstraintViolationList;
14
15
/**
16
 * @phpstan-import-type TypeArray from Type
17 284
 */
18
final class ConstraintViolationHandler implements SubscribingHandlerInterface
19 284
{
20 284
    /**
21 284
     * {@inheritdoc}
22
     */
23 284
    public static function getSubscribingMethods()
24 284
    {
25 284
        $methods = [];
26 284
        $formats = ['xml', 'json'];
27 284
        $types = [ConstraintViolationList::class => 'serializeList', ConstraintViolation::class => 'serializeViolation'];
28 284
29 284
        foreach ($types as $type => $method) {
30
            foreach ($formats as $format) {
31
                $methods[] = [
32
                    'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
33
                    'type' => $type,
34 284
                    'format' => $format,
35
                    'method' => $method . 'To' . $format,
36
                ];
37 1
            }
38
        }
39 1
40 1
        return $methods;
41 1
    }
42
43
    public function serializeListToXml(XmlSerializationVisitor $visitor, ConstraintViolationList $list): void
44 1
    {
45 1
        $currentNode = $visitor->getCurrentNode();
46
        if (!$currentNode) {
0 ignored issues
show
introduced by
$currentNode is of type DOMNode, thus it always evaluated to true.
Loading history...
47 1
            $visitor->createRoot();
48
        }
49 1
50
        foreach ($list as $violation) {
51 1
            $this->serializeViolationToXml($visitor, $violation);
52
        }
53
    }
54 2
55
    /**
56 2
     * @param TypeArray $type
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\Handler\TypeArray 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...
57
     *
58 2
     * @return array|\ArrayObject
59 2
     */
60 1
    public function serializeListToJson(SerializationVisitorInterface $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

60
    public function serializeListToJson(SerializationVisitorInterface $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...
61
    {
62 1
        return $visitor->visitArray(iterator_to_array($list), $type);
63
    }
64
65 2
    public function serializeViolationToXml(XmlSerializationVisitor $visitor, ConstraintViolation $violation): void
66 2
    {
67
        $violationNode = $visitor->getDocument()->createElement('violation');
68 2
69 2
        $parent = $visitor->getCurrentNode();
70
        if (!$parent) {
0 ignored issues
show
introduced by
$parent is of type DOMNode, thus it always evaluated to true.
Loading history...
71 2
            $visitor->setCurrentAndRootNode($violationNode);
72
        } else {
73
            $parent->appendChild($violationNode);
74 2
        }
75 2
76
        $violationNode->setAttribute('property_path', $violation->getPropertyPath());
77
        $violationNode->appendChild($messageNode = $visitor->getDocument()->createElement('message'));
78 2
79
        $messageNode->appendChild($visitor->getDocument()->createCDATASection($violation->getMessage()));
80
    }
81
82
    public function serializeViolationToJson(SerializationVisitorInterface $visitor, ConstraintViolation $violation): array
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

82
    public function serializeViolationToJson(/** @scrutinizer ignore-unused */ SerializationVisitorInterface $visitor, ConstraintViolation $violation): array

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...
83
    {
84
        return [
85
            'property_path' => $violation->getPropertyPath(),
86
            'message' => $violation->getMessage(),
87
        ];
88
    }
89
}
90