-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefineMetheds.php
161 lines (146 loc) · 3.91 KB
/
DefineMetheds.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
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
157
158
159
160
<?php
/**
* 解析和提取源代码中的函数方法
*/
class DefineMetheds
{
/** @var string */
private $fileName;
/** @var string */
private $dir;
/** @var string */
private $fileText;
/** @var string[] */
private $methodCodes = [];
/** @var DefineMethed[] */
private $methodModules = [];
/** @var EntryMethod */
private $entryFunction;
/**
* @param string $fileName
* @param string $dir
*/
public function __construct(string $fileName, string $dir)
{
$this->dir = $dir;
$this->fileName = $fileName;
if (!is_file($fileName)) {
throw new Exception('文件不存在' . $fileName);
}
$this->fileText = file_get_contents($fileName);
$this->entryFunction = new EntryMethod($this->fileName, trim(getEntryFunctionDefine($this->fileName)), $this->dir);
$this->newFunction = new NewMethod(
$this->fileName,
trim(getEntryFunctionNewDefine($this->fileName)),
$this->dir,
getEntryFunctionNewDefineLine($this->fileName)
);
$this->parse();
}
/**
* @return DefineMethed[]
*/
public function getMethods(): array
{
return $this->methodModules;
}
public function getEntry(): EntryMethod
{
return $this->entryFunction;
}
public function getNewFunction(): NewMethod
{
return $this->newFunction;
}
/**
* @return void
*/
private function parse()
{
$lines = explode(PHP_EOL, $this->fileText);
foreach ($lines as $offset => $line) {
// 判断此行是不是定义了函数
if (
$this->startWithType($line)
&& $this->endWithSeparator($line, '{')
&& $this->haveSimbol($line, ['(', ')'])
) {
$this->prepareMethod($offset);
}
}
}
/**
* @param string $line
* @return bool
*/
private function startWithType(string $line): bool
{
if (0 !== strpos($line, ' ')) {
$types = array_keys(getTypesToCTypeMapping($this->fileName, $this->dir));
foreach ($types as $type) {
if (0 === strpos($line, $type . ' ')) {
return true;
}
}
}
return false;
}
/**
* @param string $line
* @param string $separator
* @return bool
*/
private function endWithSeparator(string $line, string $separator): bool
{
$offset = strpos($line, $separator);
if (false === $offset) {
return false;
}
if ($offset + strlen($separator) == strlen($line)) {
return true;
}
return false;
}
/**
* @param string $line
* @param array $simbol
* @return bool
*/
private function haveSimbol(string $line, array $simbol): bool
{
foreach ($simbol as $s) {
if (false === strpos($line, $s)) {
return false;
}
}
return true;
}
/**
* @param int $offset
* @return void
*/
private function prepareMethod(int $offset)
{
$methodCode = '';
$state = false;
$end = 0;
$lines = explode(PHP_EOL, $this->fileText);
foreach ($lines as $i => $line) {
if ($i == $offset) {
$state = true;
}
if ($state) {
if (!empty($methodCode)) {
$methodCode .= PHP_EOL;
}
$methodCode .= $line;
if (0 === strpos($line, '}')) {
$state = false;
break;
}
}
}
$this->methodCodes[] = $methodCode;
$this->methodModules[] = new DefineMethed($this->fileName, $methodCode, $this->dir, $lines[$offset]);
}
}