Completed
Push — master ( 80ac75...5f10d0 )
by Andrii
05:33
created

ChargeHydrator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 34
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 12 2
A extract() 0 13 2
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\charge;
12
13
use hiqdev\php\billing\action\Action;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\bill\Bill;
16
use hiqdev\php\billing\price\PriceInterface;
17
use hiqdev\php\units\Quantity;
18
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
19
use Money\Money;
20
21
/**
22
 * Charge Hydrator.
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class ChargeHydrator extends GeneratedHydrator
27
{
28
    /** {@inheritdoc} */
29
    public function hydrate(array $data, $object)
30
    {
31
        $data['action'] = $this->hydrator->hydrate($data['action'], Action::class);
32
        $data['price']  = $this->hydrator->hydrate($data['price'], PriceInterface::class);
33
        $data['usage']  = $this->hydrator->hydrate($data['usage'], Quantity::class);
34
        $data['sum']    = $this->hydrator->hydrate($data['sum'], Money::class);
35
        if (isset($data['bill'])) {
36
            $data['bill'] = $this->hydrator->hydrate($data['bill'], Bill::class);
0 ignored issues
show
Documentation introduced by
$data['bill'] is of type object, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        }
38
39
        return parent::hydrate($data, $object);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     * @param object|Charge $object
45
     */
46
    public function extract($object)
47
    {
48
        $result = array_filter([
49
            'id'            => $object->getId(),
50
            'action'        => $this->hydrator->extract($object->getAction()),
51
            'price'         => $this->hydrator->extract($object->getPrice()),
52
            'usage'         => $this->hydrator->extract($object->getUsage()),
53
            'sum'           => $this->hydrator->extract($object->getSum()),
54
            'bill'          => $object->getBill() ? $this->hydrator->extract($object->getBill()) : null,
55
        ]);
56
57
        return $result;
58
    }
59
}
60