Passed
Branch scrutinizer_new_php_analysis (b739aa)
by Donald
01:58
created

VisibilityProxy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 55
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 3 1
A makePropertyAccessible() 0 12 2
A __set() 0 3 1
A __construct() 0 5 1
1
<?php namespace Chekote\Phake\Proxies;
2
3
use InvalidArgumentException;
4
use Phake_Proxies_VisibilityProxy;
5
use ReflectionProperty;
6
7
/**
8
 * Extends the Phake_Proxies_VisibilityProxy to allow accessing private or protected properties.
9
 */
10
class VisibilityProxy extends Phake_Proxies_VisibilityProxy
11
{
12
    /** @var object the object being proxied */
13
    protected $proxied;
14
15
    public function __construct($proxied)
16
    {
17
        parent::__construct($proxied);
18
19
        $this->proxied = $proxied;
20
    }
21
22
    /**
23
     * Attempts to get the value of the specified property.
24
     *
25
     * @param  string                   $property the name of the property to get.
26
     * @throws InvalidArgumentException if the specified property does not exist on the proxied class.
27
     * @return mixed                    the value of the property.
28
     */
29
    public function __get(string $property)
30
    {
31
        return $this->makePropertyAccessible($property)->getValue($this->proxied);
32
    }
33
34
    /**
35
     * Attempts to set the property name to the specified value.
36
     *
37
     * @param  string                   $property the name of the property to set
38
     * @param  mixed                    $value    the value to set
39
     * @throws InvalidArgumentException if the specified property does not exist on the proxied class.
40
     */
41
    public function __set(string $property, $value): void
42
    {
43
        $this->makePropertyAccessible($property)->setValue($this->proxied, $value);
44
    }
45
46
    /**
47
     * Ensures that the specified property on the proxied class is accessible.
48
     *
49
     * @param  string                   $name the property to make accessible.
50
     * @throws InvalidArgumentException if the specified property does not exist on the proxied class.
51
     * @return ReflectionProperty       the property.
52
     */
53
    protected function makePropertyAccessible(string $name): ReflectionProperty
54
    {
55
        if (!property_exists($this->proxied, $name)) {
56
            throw new InvalidArgumentException(
57
                "Property $name does not exist on " . get_class($this->proxied) . '.'
58
            );
59
        }
60
61
        $property = new ReflectionProperty($this->proxied, $name);
62
        $property->setAccessible(true);
63
64
        return $property;
65
    }
66
}
67