testSetValueNullOnNullableProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Doctrine\Tests_PHP74\Common\Reflection;
4
5
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty;
6
use PHPUnit\Framework\TestCase;
7
8
class TypedNoDefaultReflectionPropertyTest extends TestCase
9
{
10
    public function testGetValue() : void
11
    {
12
        $object = new TypedNoDefaultReflectionPropertyTestClass();
13
14
        $reflProperty = new TypedNoDefaultReflectionProperty(TypedNoDefaultReflectionPropertyTestClass::class, 'test');
15
16
        self::assertNull($reflProperty->getValue($object));
17
18
        $object->test = 'testValue';
19
20
        self::assertSame('testValue', $reflProperty->getValue($object));
21
22
        unset($object->test);
23
24
        self::assertNull($reflProperty->getValue($object));
25
    }
26
27
    public function testSetValueNull() : void
28
    {
29
        $reflection = new TypedNoDefaultReflectionProperty(TypedFoo::class, 'id');
30
        $reflection->setAccessible(true);
31
32
        $object = new TypedFoo();
33
        $object->setId(1);
34
35
        self::assertTrue($reflection->isInitialized($object));
0 ignored issues
show
Bug introduced by
The method isInitialized() does not exist on Doctrine\Common\Reflecti...faultReflectionProperty. ( Ignorable by Annotation )

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

35
        self::assertTrue($reflection->/** @scrutinizer ignore-call */ isInitialized($object));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
37
        $reflection->setValue($object, null);
38
39
        self::assertNull($reflection->getValue($object));
40
        self::assertFalse($reflection->isInitialized($object));
41
    }
42
43
    public function testSetValueNullOnNullableProperty() : void
44
    {
45
        $reflection = new TypedNoDefaultReflectionProperty(TypedNullableFoo::class, 'value');
46
        $reflection->setAccessible(true);
47
48
        $object = new TypedNullableFoo();
49
50
        $reflection->setValue($object, null);
51
52
        self::assertNull($reflection->getValue($object));
53
        self::assertTrue($reflection->isInitialized($object));
54
        self::assertNull($object->getValue());
55
    }
56
}
57
58
class TypedNoDefaultReflectionPropertyTestClass
59
{
60
    public string $test;
61
}
62
63
class TypedFoo
64
{
65
    private int $id;
66
67
    public function setId($id)
68
    {
69
        $this->id = $id;
70
    }
71
}
72
73
class TypedNullableFoo
74
{
75
    private ?string $value;
76
77
    public function setValue($value)
78
    {
79
        $this->value = $value;
80
    }
81
82
    public function getValue()
83
    {
84
        return $this->value;
85
    }
86
}
87