PhpParser.php
4.3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Utils\CodeGen;
use Hyperf\Utils\Exception\InvalidArgumentException;
use PhpParser\Node;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use ReflectionClass;
use ReflectionParameter;
class PhpParser
{
public const TYPES = [
'int',
'float',
'string',
'bool',
'array',
'object',
'resource',
'mixed',
];
/**
* @var null|PhpParser
*/
protected static $instance;
/**
* @var Parser
*/
protected $parser;
public function __construct()
{
$parserFactory = new ParserFactory();
$this->parser = $parserFactory->create(ParserFactory::ONLY_PHP7);
}
public static function getInstance(): PhpParser
{
if (static::$instance) {
return static::$instance;
}
return static::$instance = new static();
}
/**
* @return null|Node\Stmt[]
*/
public function getNodesFromReflectionClass(ReflectionClass $reflectionClass): ?array
{
$code = file_get_contents($reflectionClass->getFileName());
return $this->parser->parse($code);
}
public function getNodeFromReflectionParameter(ReflectionParameter $parameter): Node\Param
{
$result = new Node\Param(
new Node\Expr\Variable($parameter->getName())
);
if ($parameter->isDefaultValueAvailable()) {
$result->default = $this->getExprFromValue($parameter->getDefaultValue());
}
if ($parameter->hasType()) {
/** @var \ReflectionNamedType|\ReflectionUnionType $reflection */
$reflection = $parameter->getType();
if ($reflection instanceof \ReflectionUnionType) {
$unionType = [];
foreach ($reflection->getTypes() as $objType) {
$type = $objType->getName();
if (! in_array($type, static::TYPES)) {
$unionType[] = new Node\Name('\\' . $type);
} else {
$unionType[] = new Node\Identifier($type);
}
}
$result->type = new Node\UnionType($unionType);
} else {
$type = $reflection->getName();
if (! in_array($type, static::TYPES)) {
$result->type = new Node\Name('\\' . $type);
} else {
$result->type = new Node\Identifier($type);
}
}
}
if ($parameter->isPassedByReference()) {
$result->byRef = true;
}
if ($parameter->isVariadic()) {
$result->variadic = true;
}
return $result;
}
public function getExprFromValue($value): Node\Expr
{
switch (gettype($value)) {
case 'array':
return new Node\Expr\Array_($value);
case 'string':
return new Node\Scalar\String_($value);
case 'integer':
return new Node\Scalar\LNumber($value);
case 'double':
return new Node\Scalar\DNumber($value);
case 'NULL':
return new Node\Expr\ConstFetch(new Node\Name('null'));
case 'boolean':
return new Node\Expr\ConstFetch(new Node\Name($value ? 'true' : 'false'));
default:
throw new InvalidArgumentException($value . ' is invalid');
}
}
/**
* @return Node\Stmt\ClassMethod[]
*/
public function getAllMethodsFromStmts(array $stmts): array
{
$methods = [];
foreach ($stmts as $namespace) {
if (! $namespace instanceof Node\Stmt\Namespace_) {
continue;
}
foreach ($namespace->stmts as $class) {
if (! $class instanceof Node\Stmt\Class_ && ! $class instanceof Node\Stmt\Interface_) {
continue;
}
foreach ($class->getMethods() as $method) {
$methods[] = $method;
}
}
}
return $methods;
}
}