Passed
Push — main ( 1e579d...c0f50c )
by
unknown
08:04 queued 03:57
created

ReflectionHelper::getPrivateMethodInvoker()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Spec;
13
14
use Closure;
15
use ReflectionClass;
16
use ReflectionException;
17
use ReflectionMethod;
18
use ReflectionObject;
19
use ReflectionProperty;
20
21
/**
22
 * Testing helper.
23
 */
24
class ReflectionHelper
25
{
26
    /**
27
     * Recherchez un invocateur de méthode privée.
28
     *
29
     * @param object|string $obj    objet ou nom de classe
30
     * @param string        $method nom de la méthode
31
     *
32
     * @throws ReflectionException
33
     */
34
    public static function getPrivateMethodInvoker(object|string $obj, string $method): Closure
35
    {
36 16
        $refMethod = new ReflectionMethod($obj, $method);
37 16
        $refMethod->setAccessible(true);
38 16
        $obj = (gettype($obj) === 'object') ? $obj : null;
39
40 16
        return static fn (...$args) => $refMethod->invokeArgs($obj, $args);
41
    }
42
43
    /**
44
     * Trouvez une propriété accessible.
45
     *
46
     * @throws ReflectionException
47
     */
48
    public static function getAccessibleRefProperty(object|string $obj, string $property): ReflectionProperty
49
    {
50 24
        $refClass = is_object($obj) ? new ReflectionObject($obj) : new ReflectionClass($obj);
51
52 24
        $refProperty = $refClass->getProperty($property);
53 24
        $refProperty->setAccessible(true);
54
55 24
        return $refProperty;
56
    }
57
58
    /**
59
     * Définir une propriété privée.
60
     *
61
     * @param object|string $obj      objet ou nom de classe
62
     * @param string        $property nom de la propriété
63
     *
64
     * @throws ReflectionException
65
     */
66
    public static function setPrivateProperty(object|string $obj, string $property, mixed $value): void
67
    {
68 4
        $refProperty = self::getAccessibleRefProperty($obj, $property);
69
70
        if (is_object($obj)) {
71 4
            $refProperty->setValue($obj, $value);
72
        } else {
73
            $refProperty->setValue(null, $value);
74
        }
75
    }
76
77
    /**
78
     * Récupérer une propriété privée.
79
     *
80
     * @param object|string $obj      objet ou nom de classe
81
     * @param string        $property nom de la propriété
82
     *
83
     * @throws ReflectionException
84
     */
85
    public static function getPrivateProperty(object|string $obj, string $property): mixed
86
    {
87 24
        $refProperty = self::getAccessibleRefProperty($obj, $property);
88
89 24
        return is_string($obj) ? $refProperty->getValue() : $refProperty->getValue($obj);
90
    }
91
}
92