mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
6c20d257e3
d3d18d442c
keep relative paths
51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace RectorPrefix202209\Symplify\PackageBuilder\Reflection;
|
|
|
|
use ReflectionClass;
|
|
use ReflectionMethod;
|
|
/**
|
|
* @see \Symplify\PackageBuilder\Tests\Reflection\PrivatesCallerTest
|
|
*/
|
|
final class PrivatesCaller
|
|
{
|
|
/**
|
|
* @api
|
|
* @param mixed[] $arguments
|
|
* @param object|string $object
|
|
* @return mixed
|
|
*/
|
|
public function callPrivateMethod($object, string $methodName, array $arguments)
|
|
{
|
|
if (\is_string($object)) {
|
|
$reflectionClass = new ReflectionClass($object);
|
|
$object = $reflectionClass->newInstanceWithoutConstructor();
|
|
}
|
|
$methodReflection = $this->createAccessibleMethodReflection($object, $methodName);
|
|
return $methodReflection->invokeArgs($object, $arguments);
|
|
}
|
|
/**
|
|
* @api
|
|
* @param object|string $object
|
|
* @param mixed $argument
|
|
* @return mixed
|
|
*/
|
|
public function callPrivateMethodWithReference($object, string $methodName, $argument)
|
|
{
|
|
if (\is_string($object)) {
|
|
$reflectionClass = new ReflectionClass($object);
|
|
$object = $reflectionClass->newInstanceWithoutConstructor();
|
|
}
|
|
$methodReflection = $this->createAccessibleMethodReflection($object, $methodName);
|
|
$methodReflection->invokeArgs($object, [&$argument]);
|
|
return $argument;
|
|
}
|
|
private function createAccessibleMethodReflection(object $object, string $methodName) : ReflectionMethod
|
|
{
|
|
$reflectionMethod = new ReflectionMethod(\get_class($object), $methodName);
|
|
$reflectionMethod->setAccessible(\true);
|
|
return $reflectionMethod;
|
|
}
|
|
}
|