-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnavailableMethodsTrait.php
More file actions
63 lines (53 loc) · 1.63 KB
/
Copy pathUnavailableMethodsTrait.php
File metadata and controls
63 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace ollily\Tools\Reflection;
use ReflectionMethod;
trait UnavailableMethodsTrait
{
/**
* Calls hidden method (private, protected, package) without parameters by reflection.
*
* @param mixed $clazzName
* @param string $methodName
* @param mixed $instance
*/
protected function callMethodByReflection(mixed $clazzName, string $methodName, mixed $instance): mixed
{
$result = null;
if (!empty($clazzName)) {
$refObject = new ReflectionMethod($clazzName, $methodName);
$refObject->setAccessible(true); // NOSONAR: php:S3011
$result = $refObject->invoke($instance);
}
return $result;
}
/**
* Calls a hidden method on an object which shall be tested (o2t).
*
* @param string $methodName
*/
protected function callMethodOnO2t(string $methodName): mixed
{
$result = null;
/**
* @psalm-suppress RedundantPropertyInitializationCheck,UndefinedThisPropertyFetch
* @phpstan-ignore isset.property,property.notFound
*/
if (isset($this->o2t)) {
$clazzName = get_class($this->o2t);
/** @psalm-suppress RedundantCondition */
if (!empty($clazzName)) {
$result = $this->callMethodByReflection($clazzName, $methodName, $this->o2t);
}
}
return $result;
}
}