Passed
Push — master ( 50749f...7b5f02 )
by Gerrit
12:36
created

isDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Copyright (C) 2019 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\RDMBundle\DataLoader\BlackMagic;
14
15
use ReflectionClass;
16
use ReflectionType;
17
use ReflectionProperty;
18
use Doctrine\ORM\Mapping\ClassMetadata;
19
use Doctrine\DBAL\Schema\Column;
20
use Addiks\RDMBundle\DataLoader\BlackMagic\BlackMagicDataLoader;
21
use Doctrine\ORM\EntityManagerInterface;
22
use ReflectionObject;
23
24
/** @see BlackMagicDataLoader */
25
final class BlackMagicColumnReflectionPropertyMock extends ReflectionProperty
26
{
27
    /** @var EntityManagerInterface */
28
    private $entityManager;
29
30
    /** @var ClassMetadata */
31
    private $classMetadata;
32
33
    /** @var ReflectionClass */
34
    private $classMetadataReflection;
35
36
    /** @var Column */
37
    private $column;
38
39
    /** @var string */
40
    private $fieldName;
41
42
    /** @var BlackMagicDataLoader $dataLoader */
43
    private $dataLoader;
44
45 1
    public function __construct(
46
        EntityManagerInterface $entityManager,
47
        ClassMetadata $classMetadata,
48
        Column $column,
49
        string $fieldName,
50
        BlackMagicDataLoader $dataLoader
51
    ) {
52 1
        $this->entityManager = $entityManager;
53 1
        $this->classMetadata = $classMetadata;
54 1
        $this->classMetadataReflection = new ReflectionObject($classMetadata);
55 1
        $this->column = $column;
56 1
        $this->fieldName = $fieldName;
57 1
        $this->dataLoader = $dataLoader;
58 1
    }
59
60
    public function getDeclaringClass(): ReflectionClass
61
    {
62
        return $this->classMetadata->reflClass;
63
    }
64
65
    public function getName(): string
66
    {
67
        return $this->fieldName;
68
    }
69
70
    public function getValue($object = null)
71
    {
72
        return $this->dataLoader->onColumnValueRequestedFromEntity(
73
            $this->entityManager,
74
            $object,
75
            $this->column->getName()
76
        );
77
    }
78
79
    public function setValue($valueOrObject, $value = null): void
80
    {
81
        if (is_null($value)) {
82
            $this->dataLoader->onColumnValueSetOnEntity(
83
                $this->entityManager,
84
                null,
85
                $this->column->getName(),
86
                $valueOrObject
87
            );
88
89
        } else {
90
            $this->dataLoader->onColumnValueSetOnEntity(
91
                $this->entityManager,
92
                $valueOrObject,
93
                $this->column->getName(),
94
                $value
95
            );
96
        }
97
    }
98
99
    public function getDefaultValue()
100
    {
101
        return null;
102
    }
103
104
    public function getDocComment()
105
    {
106
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method ReflectionProperty::getDocComment of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
    }
108
109
    public function getModifiers(): int
110
    {
111
        return 0;
112
    }
113
114
    public function getType(): ?ReflectionType
115
    {
116
        return null;
117
    }
118
119
    public function hasDefaultValue(): bool
120
    {
121
        return false;
122
    }
123
124
    public function hasType(): bool
125
    {
126
        return false;
127
    }
128
129
    public function isDefault(): bool
130
    {
131
        return false;
132
    }
133
134
    public function isInitialized($object = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        return true;
137
    }
138
139
    public function isPrivate(): bool
140
    {
141
        return true;
142
    }
143
144
    public function isProtected(): bool
145
    {
146
        return false;
147
    }
148
149
    public function isPublic(): bool
150
    {
151
        return false;
152
    }
153
154
    public function isStatic(): bool
155
    {
156
        return false;
157
    }
158
159
    public function setAccessible($accessible): void
160
    {
161
    }
162
163
    public function __toString(): string
164
    {
165
        return "";
166
    }
167
}
168