-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathClassReader.php
82 lines (79 loc) · 2.51 KB
/
ClassReader.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Code\Reader;
class ClassReader implements ClassReaderInterface
{
/**
* Read class constructor signature
*
* @param string $className
* @return array|null
* @throws \ReflectionException
*/
public function getConstructor($className)
{
$class = new \ReflectionClass($className);
$result = null;
$constructor = $class->getConstructor();
if ($constructor) {
$result = [];
/** @var $parameter \ReflectionParameter */
foreach ($constructor->getParameters() as $parameter) {
try {
$result[] = [
$parameter->getName(),
$parameter->getClass() !== null ? $parameter->getClass()->getName() : null,
!$parameter->isOptional(),
$parameter->isOptional()
? ($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null)
: null,
];
} catch (\ReflectionException $e) {
$message = $e->getMessage();
throw new \ReflectionException($message, 0, $e);
}
}
}
return $result;
}
/**
* Retrieve parent relation information for type in a following format
* array(
* 'Parent_Class_Name',
* 'Interface_1',
* 'Interface_2',
* ...
* )
*
* @param string $className
* @return string[]
*/
public function getParents($className)
{
$parentClass = get_parent_class($className);
if ($parentClass) {
$result = [];
$interfaces = class_implements($className);
if ($interfaces) {
$parentInterfaces = class_implements($parentClass);
if ($parentInterfaces) {
$result = array_values(array_diff($interfaces, $parentInterfaces));
} else {
$result = array_values($interfaces);
}
}
array_unshift($result, $parentClass);
} else {
$result = array_values(class_implements($className));
if ($result) {
array_unshift($result, null);
} else {
$result = [];
}
}
return $result;
}
}