3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\Test;
18
use ReflectionException;
21
use ReflectionProperty;
29
* Find a private method invoker.
31
* @param object|string $obj object or class name
32
* @param string $method method name
36
* @throws ReflectionException
38
public static function getPrivateMethodInvoker($obj, $method)
40
$refMethod = new ReflectionMethod($obj, $method);
41
$refMethod->setAccessible(true);
42
$obj = (gettype($obj) === 'object') ? $obj : null;
44
return static fn (...$args) => $refMethod->invokeArgs($obj, $args);
48
* Find an accessible property.
50
* @param object|string $obj
51
* @param string $property
53
* @return ReflectionProperty
55
* @throws ReflectionException
57
private static function getAccessibleRefProperty($obj, $property)
59
$refClass = is_object($obj) ? new ReflectionObject($obj) : new ReflectionClass($obj);
61
$refProperty = $refClass->getProperty($property);
62
$refProperty->setAccessible(true);
68
* Set a private property.
70
* @param object|string $obj object or class name
71
* @param string $property property name
72
* @param mixed $value value
74
* @throws ReflectionException
76
public static function setPrivateProperty($obj, $property, $value)
78
$refProperty = self::getAccessibleRefProperty($obj, $property);
80
if (is_object($obj)) {
81
$refProperty->setValue($obj, $value);
83
$refProperty->setValue(null, $value);
88
* Retrieve a private property.
90
* @param object|string $obj object or class name
91
* @param string $property property name
95
* @throws ReflectionException
97
public static function getPrivateProperty($obj, $property)
99
$refProperty = self::getAccessibleRefProperty($obj, $property);
101
return is_string($obj) ? $refProperty->getValue() : $refProperty->getValue($obj);