Completed
Push — master ( 2236d8...f66191 )
by Taosikai
10:51
created

Hydrator::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Hydrator::hydrate() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the slince/shopify-api-php
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Slince\Shopify\Hydrator;
13
14
use JMS\Serializer\Context;
15
use JMS\Serializer\SerializationContext;
16
use JMS\Serializer\Serializer;
17
use JMS\Serializer\SerializerBuilder;
18
19
class Hydrator implements HydratorInterface
20
{
21
    /**
22
     * @var Serializer
23
     */
24
    protected $serializer;
25
26
    /**
27
     * @var Context
28
     */
29
    protected $serializationContext;
30
31
    public function __construct($cacheDir, $metaDir)
32
    {
33
        $this->serializer = SerializerBuilder::create()
34
            ->setCacheDir($cacheDir)
35
            ->addMetadataDir($metaDir)
36
            ->build();
37
        $this->serializationContext = SerializationContext::create()->setSerializeNull(true);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function extract($object)
44
    {
45
        return $this->serializer->toArray($object, $this->serializationContext);
0 ignored issues
show
Documentation introduced by
$this->serializationContext is of type object<JMS\Serializer\Context>, but the function expects a null|object<JMS\Serializer\SerializationContext>.

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...
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function hydrate($target, array $data)
52
    {
53
        return $this->serializer->fromArray($data, $target);
0 ignored issues
show
Bug introduced by
It seems like $target defined by parameter $target on line 51 can also be of type object; however, JMS\Serializer\Serializer::fromArray() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
    }
55
}