Failed Conditions
Push — master ( 8ded6a...b2fb92 )
by Arnold
03:23
created

PrivateAccessTrait::getPrivateProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\PHPUnit;
6
7
/**
8
 * Trait for accessing private/protected methods and properties.
9
 */
10
trait PrivateAccessTrait
11
{
12
    /**
13
     * Call a private or protected method.
14
     *
15
     * @param object $object
16
     * @param string $method
17
     * @param array  $args
18
     * @return mixed
19
     */
20 6
    protected function callPrivateMethod(object $object, string $method, array $args = [])
21
    {
22 6
        $refl = new \ReflectionMethod(get_class($object), $method);
23 6
        $refl->setAccessible(true);
24
        
25 6
        return $refl->invokeArgs($object, $args);
26
    }
27
    
28
    /**
29
     * Set a private or protected property.
30
     *
31
     * @param object $object
32
     * @param string $property
33
     * @param mixed  $value
34
     */
35 3
    protected function setPrivateProperty(object $object, string $property, $value): void
36
    {
37 3
        $refl = new \ReflectionProperty(get_class($object), $property);
38 3
        $refl->setAccessible(true);
39
        
40 3
        $refl->setValue($object, $value);
41 3
    }
42
43
    /**
44
     * Get the value of a private or protected property.
45
     *
46
     * @param object $object
47
     * @param string $property
48
     * @return mixed
49
     */
50 3
    protected function getPrivateProperty($object, string $property)
51
    {
52 3
        $refl = new \ReflectionProperty(get_class($object), $property);
53 3
        $refl->setAccessible(true);
54
55 3
        return $refl->getValue($object);
56
    }
57
}
58