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

ActionHydrator::extract()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 1
nop 1
crap 12
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\action;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\customer\Customer;
15
use hiqdev\php\billing\action\Action;
16
use hiqdev\php\billing\type\Type;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
20
21
/**
22
 * Action Hydrator.
23
 *
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class ActionHydrator extends GeneratedHydrator
27
{
28
    /** {@inheritdoc} */
29
    public function hydrate(array $data, $object)
30
    {
31
        $data['type'] = $this->hydrator->hydrate($data['type'], Type::class);
32
        $data['target'] = $this->hydrator->hydrate($data['target'], Target::class);
33
        $data['quantity'] = $this->hydrator->hydrate($data['quantity'], Quantity::class);
34
        $data['customer'] = $this->hydrator->hydrate($data['customer'], Customer::class);
35
        $data['time'] = $this->hydrator->hydrate([$data['time']], DateTimeImmutable::class);
36
        if (isset($data['sale'])) {
37
            $data['sale'] = $this->hydrator->hydrate($data['sale'], Sale::class);
0 ignored issues
show
Documentation introduced by
$data['sale'] 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...
38
        }
39
        if (isset($data['parent'])) {
40
            $data['parent'] = $this->hydrator->hydrate($data['parent'], Action::class);
0 ignored issues
show
Documentation introduced by
$data['parent'] 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...
41
        }
42
43
        return parent::hydrate($data, $object);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     * @param object|Action $object
49
     */
50
    public function extract($object)
51
    {
52
        $result = array_filter([
53
            'id'            => $object->getId(),
54
            'type'          => $this->hydrator->extract($object->getType()),
55
            'target'        => $this->hydrator->extract($object->getTarget()),
56
            'quantity'      => $this->hydrator->extract($object->getQuantity()),
57
            'customer'      => $this->hydrator->extract($object->getCustomer()),
58
            'time'          => $this->hydrator->extract($object->getTime()),
59
            'sale'          => $object->getSale() ? $this->hydrator->extract($object->getSale()) : null,
60
            'parent'        => $object->getParent() ? $this->hydrator->extract($object->getParent()) : null,
61
        ]);
62
63
        return $result;
64
    }
65
}
66