Completed
Push — master ( c83831...4ba780 )
by Andrii
02:57
created

TargetHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 17.78 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 8
loc 45
ccs 12
cts 16
cp 0.75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A extract() 0 8 1
A hydrate() 8 8 2
A createEmptyInstance() 0 8 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
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\target;
12
13
use hiqdev\billing\hiapi\models\Target;
14
use hiqdev\php\billing\target\TargetFactoryInterface;
15
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
16
use Zend\Hydrator\HydratorInterface;
17
18
/**
19
 * Class TargetHydrator.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class TargetHydrator extends GeneratedHydrator
24
{
25
    /**
26
     * @var TargetFactoryInterface
27
     */
28
    private $targetFactory;
29
30 1
    public function __construct(HydratorInterface $hydrator, TargetFactoryInterface $targetFactory)
31
    {
32 1
        parent::__construct($hydrator);
33 1
        $this->targetFactory = $targetFactory;
34 1
    }
35
36
    /**
37
     * {@inheritdoc}
38
     * @param object|Target $object
39
     */
40
    public function extract($object)
41
    {
42
        return [
43
            'id'            => $object->getId(),
44
            'type'          => $object->getType(),
45
            'name'          => $object->getName(),
46
        ];
47
    }
48
49
    /** {@inheritdoc} */
50 6 View Code Duplication
    public function hydrate(array $data, $object)
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...
51
    {
52 6
        if (!empty($data['type'])) {
53 6
            $data['type'] = $this->targetFactory->shortenType($data['type']);
54
        }
55
56 6
        return parent::hydrate($data, $object);
57
    }
58
59 5
    public function createEmptyInstance(string $className, array $data = [])
60
    {
61 5
        if (isset($data['type'])) {
62 5
            $className = $this->targetFactory->getClassForType($data['type']);
63
        }
64
65 5
        return parent::createEmptyInstance($className, $data);
66
    }
67
}
68