Completed
Push — master ( b2db07...6ada9a )
by luca
08:41
created

TransmapHydratable::hydratePreprocess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Audiens\DoubleclickClient\entity;
4
5
use Audiens\DoubleclickClient\exceptions\ClientException;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Doctrine\Common\Annotations\AnnotationRegistry;
8
use Exception;
9
use GiacomoFurlan\ObjectTransmapperValidator\Transmapper;
10
use RuntimeException;
11
12
/**
13
 * Trait TransmapHydratable
14
 */
15
trait TransmapHydratable
16
{
17
    private static $transmapper;
18
19
    private static function getTransmapper(): Transmapper
20
    {
21
        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...
22
            $loader = include __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
23
            AnnotationRegistry::registerLoader([$loader, 'loadClass']);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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