Passed
Push — master ( d743e2...20969e )
by Jhao
01:54
created

Instantiator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of RussianPost SDK package.
5
 *
6
 * © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Appwilio\RussianPostSDK\Dispatching;
15
16
use Appwilio\RussianPostSDK\Dispatching\Http\ArrayOf;
17
18
class Instantiator
19
{
20
    /** @var ArrayOf|string $class */
21
    private $class;
22
23
    /** @var bool */
24
    private $array = false;
25
26
    /** @var \ReflectionClass */
27
    private $reflector;
28
29 4
    public function __construct($class)
30
    {
31 4
        if ($class instanceof ArrayOf) {
32 2
            $this->array = true;
33 2
            $this->class = $class->getClass();
34
        } else {
35 4
            $this->class = $class;
36
        }
37
38 4
        $this->reflector = new \ReflectionClass($this->class);
39 4
    }
40
41
    /**
42
     * @param  ArrayOf|string  $class
43
     * @param  mixed           $data
44
     *
45
     * @return mixed
46
     */
47 3
    public static function instantiate($class, $data)
48
    {
49 3
        if (null === $data) {
50 1
            return null;
51
        }
52
53 2
        return (new self($class))->fill($data);
54
    }
55
56
    /**
57
     * @param  string  $class
58
     * @param  object  $source
59
     * @param  array   $unset
60
     *
61
     * @throws \ReflectionException
62
     *
63
     * @return mixed
64
     */
65 1
    public static function instantiateFrom(string $class, $source, array $unset = [])
66
    {
67 1
        $sourceReflector = (new \ReflectionClass($source))->getProperty('data');
68 1
        $sourceReflector->setAccessible(true);
69
70 1
        $destination = (new \ReflectionClass($class))->newInstanceWithoutConstructor();
71
72 1
        $destinationReflector = (new \ReflectionClass($destination))->getProperty('data');
73 1
        $destinationReflector->setAccessible(true);
74
75 1
        $data = $sourceReflector->getValue($source);
76
77 1
        foreach ($unset as $item) {
78 1
            unset($data[$item]);
79
        }
80
81 1
        $destinationReflector->setValue($destination, $data);
82
83 1
        return $destination;
84
    }
85
86 3
    public function fill($data)
87
    {
88 3
        if (null === $data) {
89 1
            return null;
90
        }
91
92 2
        if ($this->array) {
93 1
            $objects = [];
94
95 1
            foreach ($data as $item) {
96 1
                $objects[] = $this->build($this->class, $item);
0 ignored issues
show
Bug introduced by
It seems like $this->class can also be of type Appwilio\RussianPostSDK\Dispatching\Http\ArrayOf; however, parameter $class of Appwilio\RussianPostSDK\...g\Instantiator::build() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
                $objects[] = $this->build(/** @scrutinizer ignore-type */ $this->class, $item);
Loading history...
97
            }
98
99 1
            return $objects;
100
        }
101
102 2
        return $this->build($this->class, $data);
103
    }
104
105 2
    private function build(string $class, array $data)
106
    {
107 2
        $object = (new \ReflectionClass($class))->newInstanceWithoutConstructor();
108
109 2
        $property = $this->reflector->getProperty('data');
110
111 1
        $property->setAccessible(true);
112
113 1
        $property->setValue($object, $data);
114
115 1
        return $object;
116
    }
117
}
118