Passed
Push — master ( 529e95...b8db06 )
by Smoren
03:38
created

ObjectPropertyProxy::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Smoren\Schemator\Structs;
4
5
use Smoren\Schemator\Helpers\ObjectAccessHelper;
6
use Smoren\Schemator\Interfaces\ProxyInterface;
7
8
/**
9
 * @template T of object
10
 * @implements ProxyInterface<T>
11
 */
12
class ObjectPropertyProxy implements ProxyInterface
13
{
14
    /**
15
     * @var T
0 ignored issues
show
Bug introduced by
The type Smoren\Schemator\Structs\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
     */
17
    protected object $object;
18
    protected string $propertyName;
19
20
    /**
21
     * @param T $object
22
     * @param string $propertyName
23
     */
24
    public function __construct(object $object, string $propertyName)
25
    {
26
        $this->object = $object;
27
        $this->propertyName = $propertyName;
28
    }
29
30
    /**
31
     * @return mixed
32
     */
33
    public function getValue()
34
    {
35
        if (!ObjectAccessHelper::hasReadableProperty($this->object, $this->propertyName)) {
36
            $className = get_class($this->object);
37
            throw new \BadMethodCallException("Property '{$className}::{$this->propertyName}' is not readable");
38
        }
39
        return ObjectAccessHelper::getPropertyValue($this->object, $this->propertyName);
40
    }
41
42
    /**
43
     * @param mixed $value
44
     * @return void
45
     */
46
    public function setValue($value): void
47
    {
48
        if (!ObjectAccessHelper::hasWritableProperty($this->object, $this->propertyName)) {
49
            $className = get_class($this->object);
50
            throw new \BadMethodCallException("Property '{$className}::{$this->propertyName}' is not writable");
51
        }
52
        ObjectAccessHelper::setPropertyValue($this->object, $this->propertyName, $value);
53
    }
54
}
55