Completed
Pull Request — master (#78)
by
unknown
34:37 queued 09:40
created

GenerateHydratorTest::testClassWithParents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
rs 9.1344
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneratedHydratorTest\ClosureGenerator\FileCache;
6
7
use GeneratedHydrator\ClosureGenerator\FileCache\GenerateFileCacheHydrator;
8
use GeneratedHydratorTestAsset\BaseClass;
9
use GeneratedHydratorTestAsset\ClassWithPrivatePropertiesAndParents;
10
use PHPUnit\Framework\TestCase;
11
use ReflectionClass;
12
use ReflectionException;
13
use function get_class;
14
15
class GenerateHydratorTest extends TestCase
16
{
17
    /** @var GenerateFileCacheHydrator */
18
    private $generateHydrator;
19
20
    public function setUp() : void
21
    {
22
        parent::setUp();
23
        $this->generateHydrator = new GenerateFileCacheHydrator('build/');
24
    }
25
26
    /**
27
     * @throws ReflectionException
28
     */
29
    public function testDefaultBaseClass() : void
30
    {
31
        $hydrate = ($this->generateHydrator)(BaseClass::class);
32
        $object  = new BaseClass();
33
34
        $result = $hydrate(
35
            [
36
                'publicProperty' => 'publicPropertyNew',
37
                'protectedProperty' => 'protectedPropertyNew',
38
                'privateProperty' => 'privatePropertyNew',
39
            ],
40
            $object
41
        );
42
43
        self::assertSame(
44
            [
45
                'publicProperty' => 'publicPropertyNew',
46
                'protectedProperty' => 'protectedPropertyNew',
47
                'privateProperty' => 'privatePropertyNew',
48
            ],
49
            $this->getProperties($result)
50
        );
51
    }
52
53
    /**
54
     * @throws ReflectionException
55
     */
56
    public function testClassWithParents() : void
57
    {
58
        $object  = new ClassWithPrivatePropertiesAndParents();
59
        $hydrate = ($this->generateHydrator)(get_class($object));
60
61
        $hydrate(
62
            [
63
                'property0' => 'property0_new',
64
                'property1' => 'property1_new',
65
                'property2' => 'property2_new',
66
                'property3' => 'property3_new',
67
                'property4' => 'property4_new',
68
                'property5' => 'property5_new',
69
                'property6' => 'property6_new',
70
                'property7' => 'property7_new',
71
                'property8' => 'property8_new',
72
                'property9' => 'property9_new',
73
                'property20' => 'property20_new',
74
                'property21' => 'property21_new',
75
                'property22' => 'property22_new',
76
                'property30' => 'property30_new',
77
                'property31' => 'property31_new',
78
                'property32' => 'property32_new',
79
            ],
80
            $object
81
        );
82
        self::assertSame(
83
            [
84
                'property0' => 'property0_new',
85
                'property1' => 'property1_new',
86
                'property2' => 'property2_new',
87
                'property3' => 'property3_new',
88
                'property4' => 'property4_new',
89
                'property5' => 'property5_new',
90
                'property6' => 'property6_new',
91
                'property7' => 'property7_new',
92
                'property8' => 'property8_new',
93
                'property9' => 'property9_new',
94
                'property20' => 'property20_new',
95
                'property21' => 'property21_new',
96
                'property22' => 'property22_new',
97
                'property30' => 'property30_new',
98
                'property31' => 'property31_new',
99
                'property32' => 'property32_new',
100
            ],
101
            $this->getProperties($object)
0 ignored issues
show
Documentation introduced by
$object is of type object<GeneratedHydrator...tePropertiesAndParents>, but the function expects a object<GeneratedHydrator...rator\FileCache\object>.

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...
102
        );
103
    }
104
105
    /**
106
     * @return mixed[]
107
     *
108
     * @throws ReflectionException
109
     */
110
    private function getProperties(object $object) : array
111
    {
112
        $reflectionClass = new ReflectionClass($object);
113
114
        return $this->getPropertiesWithReflection($object, $reflectionClass);
115
    }
116
117
    /**
118
     * @return mixed[]
119
     */
120
    private function getPropertiesWithReflection(object $object, ReflectionClass $reflectionClass) : array
121
    {
122
        $properties = $reflectionClass->getParentClass()
123
            ? $this->getPropertiesWithReflection($object, $reflectionClass->getParentClass())
124
            : [];
125
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
126
            if ($reflectionProperty->isStatic()) {
127
                continue;
128
            }
129
            if ($reflectionProperty->isPrivate() || $reflectionProperty->isProtected()) {
130
                $reflectionProperty->setAccessible(true);
131
            }
132
            $properties[$reflectionProperty->getName()] = $reflectionProperty->getValue($object);
133
        }
134
135
        return $properties;
136
    }
137
}
138