Completed
Pull Request — 1.x (#8)
by Dorian
46:47
created

SimpleHydratingHandler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 12.05 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 10
loc 83
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 1 1
A setKey() 0 1 1
A getParser() 0 1 1
A setParser() 0 1 1
A getValidators() 0 1 1
A setValidators() 0 1 1
A addValidator() 0 1 1
A getDefaultValue() 0 1 1
A setDefaultValue() 0 1 1
A __construct() 0 7 1
A handle() 0 20 4
A validate() 10 10 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
namespace MetaHydrator\Handler;
3
4
use MetaHydrator\Exception\HydratingException;
5
use MetaHydrator\Exception\ValidationException;
6
use MetaHydrator\Exception\ParsingException;
7
use MetaHydrator\Parser\ParserInterface;
8
use MetaHydrator\Validator\ValidatorInterface;
9
10
/**
11
 * An implementation of HydratingHandlerInterface, as a one to one value parsing/validation.
12
 *
13
 * Class SimpleHydratingHandler
14
 * @package MetaHhydrator\Handler
15
 */
16
class SimpleHydratingHandler implements HydratingHandlerInterface
17
{
18
    /** @var string */
19
    private $key;
20
    public function getKey() { return $this->key; }
21
    public function setKey(string $key) { $this->key = $key; }
22
23
    /** @var ParserInterface */
24
    private $parser;
25
    public function getParser() { return $this->parser; }
26
    public function setParser(ParserInterface $parser) { $this->parser = $parser; }
27
28
    /** @var ValidatorInterface[] */
29
    private $validators;
30
    public function getValidators() { return $this->validators; }
31
    public function setValidators(array $validators) { $this->validators = $validators; }
32
    public function addValidator(ValidatorInterface $validator) { $this->validators[] = $validator; }
33
34
    /** @var mixed */
35
    private $defaultValue;
36
    public function getDefaultValue() { return $this->defaultValue; }
37
    public function setDefaultValue($defaultValue) { $this->defaultValue = $defaultValue; }
38
39
    /**
40
     * SimpleHydratingHandler constructor.
41
     * @param string $key
42
     * @param ParserInterface $parser
43
     * @param ValidatorInterface[] $validators
44
     * @param mixed $default
0 ignored issues
show
Documentation introduced by
There is no parameter named $default. Did you maybe mean $defaultValue?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
45
     */
46
    public function __construct(string $key, ParserInterface $parser, array $validators = [], $defaultValue = null)
47
    {
48
        $this->key = $key;
49
        $this->parser = $parser;
50
        $this->validators = $validators;
51
        $this->defaultValue = $defaultValue;
52
    }
53
54
    /**
55
     * @param array $data
56
     * @param array $targetData
57
     * @param $object
58
     *
59
     * @throws HydratingException
60
     */
61
    public function handle(array $data, array &$targetData, $object = null)
62
    {
63
        if (array_key_exists($this->key, $data)) {
64
            $rawValue = $data[$this->key];
65
        } else if ($object === null) {
66
            $rawValue = $this->defaultValue;
67
        } else {
68
            return;
69
        }
70
71
        try {
72
            $parsedValue = $this->parser->parse($rawValue);
73
        } catch (ParsingException $exception) {
74
            throw new HydratingException([ $this->key => $exception->getInnerError() ]);
75
        }
76
77
        $this->validate($parsedValue, $object);
78
79
        $targetData[$this->key] = $parsedValue;
80
    }
81
82
    /**
83
     * @param mixed $parsedValue
84
     * @param mixed $contextObject
85
     *
86
     * @throws HydratingException
87
     */
88 View Code Duplication
    private function validate($parsedValue, $contextObject = null)
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...
89
    {
90
        try {
91
            foreach ($this->validators as $validator) {
92
                $validator->validate($parsedValue, $contextObject);
93
            }
94
        } catch (ValidationException $exception) {
95
            throw new HydratingException([ $this->key => $exception->getInnerError() ]);
96
        }
97
    }
98
}
99