LuaConstraintViolationHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 8
loc 42
c 0
b 0
f 0
ccs 0
cts 38
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribingMethods() 8 16 2
A serializeList() 0 8 1
A serializeViolation() 0 14 2

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
namespace Vlaswinkel\Lua\JMS;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\GraphNavigator;
7
use JMS\Serializer\Handler\SubscribingHandlerInterface;
8
use Symfony\Component\Validator\ConstraintViolation;
9
use Symfony\Component\Validator\ConstraintViolationList;
10
11
/**
12
 * Class LuaConstraintViolationHandler
13
 *
14
 * @see     https://github.com/schmittjoh/serializer/blob/1.1.0/src/JMS/Serializer/Handler/ConstraintViolationHandler.php
15
 *
16
 * @author  Johannes M. Schmitt <[email protected]>
17
 * @author  Koen Vlaswinkel <[email protected]>
18
 * @package Vlaswinkel\Lua\JMS
19
 */
20
class LuaConstraintViolationHandler implements SubscribingHandlerInterface {
21
    public static function getSubscribingMethods() {
22
        $methods = [];
23
        $types   = [
24
            'Symfony\Component\Validator\ConstraintViolationList' => 'serializeList',
25
            'Symfony\Component\Validator\ConstraintViolation'     => 'serializeViolation',
26
        ];
27 View Code Duplication
        foreach ($types as $type => $method) {
28
            $methods[] = [
29
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
30
                'type'      => $type,
31
                'format'    => 'lua',
32
                'method'    => $method,
33
            ];
34
        }
35
        return $methods;
36
    }
37
38
    public function serializeList(
39
        LuaSerializationVisitor $visitor,
40
        ConstraintViolationList $list,
41
        array $type,
42
        Context $context
43
    ) {
44
        return $visitor->visitArray(iterator_to_array($list), $type, $context);
45
    }
46
47
    public function serializeViolation(
48
        LuaSerializationVisitor $visitor,
49
        ConstraintViolation $violation,
50
        array $type = null
51
    ) {
52
        $data = [
53
            'property_path' => $violation->getPropertyPath(),
54
            'message'       => $violation->getMessage(),
55
        ];
56
        if (null === $visitor->getRoot()) {
57
            $visitor->setRoot($data);
58
        }
59
        return $data;
60
    }
61
}