TransmapHydratable   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransmapper() 0 8 2
A hydratePreprocess() 0 4 1
A fromArray() 0 10 2
A toArray() 0 4 1
1
<?php
2
3
namespace Audiens\DoubleclickClient\entity;
4
5
use Audiens\DoubleclickClient\exceptions\ClientException;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Exception;
8
use GiacomoFurlan\ObjectTransmapperValidator\Transmapper;
9
use RuntimeException;
10
11
/**
12
 * Trait TransmapHydratable
13
 */
14
trait TransmapHydratable
15
{
16
    private static $transmapper;
17
18
    private static function getTransmapper(): Transmapper
19
    {
20
        if (!static::$transmapper) {
0 ignored issues
show
Bug introduced by
Since $transmapper is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $transmapper to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
21
            self::$transmapper = new Transmapper(new AnnotationReader());
22
        }
23
24
        return self::$transmapper;
25
    }
26
27
    /**
28
     * @param array $objectArray
29
     * @return array
30
     */
31
    protected static function hydratePreprocess(array $objectArray): array
32
    {
33
        return $objectArray;
34
    }
35
36
    /**
37
     * @param array $objectArray
38
     *
39
     * @return static
40
     * @throws \Audiens\DoubleclickClient\exceptions\ClientException
41
     */
42
    public static function fromArray(array $objectArray)
43
    {
44
45
        $object = (object) static::hydratePreprocess($objectArray);
46
        try {
47
            return static::getTransmapper()->map($object, static::class);
48
        } catch (Exception $exception) {
49
            throw new ClientException($exception->getMessage(), $exception->getCode(), $exception);
50
        }
51
    }
52
53
    /**
54
     * @return array
55
     * @throws \RuntimeException
56
     */
57
    public function toArray()
58
    {
59
        throw new RuntimeException('Not implemented yet');
60
    }
61
}
62