-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionHelper.php
More file actions
65 lines (55 loc) · 1.55 KB
/
ReflectionHelper.php
File metadata and controls
65 lines (55 loc) · 1.55 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
64
65
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2017 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\Ioc;
use ReflectionNamedType;
use ReflectionParameter;
/**
* Defines a helper class for reflections
* Keeps compatibility with PHP7.2 and 8.x
*/
class ReflectionHelper
{
/**
* @param ReflectionParameter $parameter
* @return string|null
*/
public static function getParameterClassName(ReflectionParameter $parameter) : ?string
{
$typeName = static::getParameterTypeName($parameter);
if (null === $typeName) {
return null;
}
if (class_exists($typeName) || interface_exists($typeName)) {
return $typeName;
}
return null;
}
/**
* @param ReflectionParameter $parameter
* @return string|null
*/
public static function getParameterTypeName(ReflectionParameter $parameter) : ?string
{
$parameterType = $parameter->getType();
if ($parameterType === null) {
return null;
}
$className = null;
if ($parameterType instanceof ReflectionNamedType) {
return $parameterType->getName();
}
if (method_exists($parameterType, 'getTypes')) {
/** @var ReflectionNamedType $parameterType */
foreach ($parameterType->getTypes() as $parameterType) {
return $parameterType->getName();
}
}
return null;
}
}