-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Parser.php
200 lines (177 loc) · 5.97 KB
/
Parser.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace Codeception\Lib;
use Codeception\Exception\TestParseException;
use Codeception\Scenario;
use Codeception\Step\Action;
use Codeception\Step\Comment;
use Codeception\Test\Metadata;
use Exception;
use ParseError;
class Parser
{
protected string $code;
public function __construct(protected Scenario $scenario, protected Metadata $metadata)
{
}
public function prepareToRun(string $code): void
{
$this->parseFeature($code);
$this->parseScenarioOptions($code);
}
public function parseFeature(string $code): void
{
$matches = [];
$code = $this->stripComments($code);
$res = preg_match("#\\\$I->wantTo\\(\\s*?['\"](.*?)['\"]\\s*?\\);#", $code, $matches);
if ($res) {
$this->scenario->setFeature($matches[1]);
return;
}
$res = preg_match("#\\\$I->wantToTest\\(['\"](.*?)['\"]\\);#", $code, $matches);
if ($res) {
$this->scenario->setFeature("test " . $matches[1]);
}
}
public function parseScenarioOptions(string $code): void
{
$this->metadata->setParamsFromAnnotations($this->matchComments($code));
}
public function parseSteps(string $code): void
{
// parse per line
$friends = [];
$lines = explode("\n", $code);
$isFriend = false;
foreach ($lines as $line) {
// friends
if (preg_match("#\\\$I->haveFriend\\((.*?)\\);#", $line, $matches)) {
$friends[] = trim($matches[1], '\'"');
}
// friend's section start
if (preg_match("#\\\$(.*?)->does\\(#", $line, $matches)) {
$friend = $matches[1];
if (!in_array($friend, $friends)) {
continue;
}
$isFriend = true;
$this->addCommentStep("\n----- {$friend} does -----");
continue;
}
// actions
if (preg_match("#\\\$I->(.*)\\((.*?)\\);#", $line, $matches)) {
$this->addStep($matches);
}
// friend's section ends
if ($isFriend && str_contains($line, '}')) {
$this->addCommentStep("-------- back to me\n");
$isFriend = false;
}
}
}
protected function addStep(array $matches): void
{
[$m, $action, $params] = $matches;
if (in_array($action, ['wantTo', 'wantToTest'])) {
return;
}
$this->scenario->addStep(new Action($action, explode(',', $params)));
}
protected function addCommentStep(string $comment): void
{
$this->scenario->addStep(new Comment($comment, []));
}
public static function load(string $file): void
{
try {
self::includeFile($file);
} catch (ParseError $e) {
throw new TestParseException($e->getFile(), $e->getMessage(), $e->getLine(), $file);
} catch (Exception) {
// file is valid otherwise
}
}
/**
* @return string[]
*/
public static function getClassesFromFile(string $file): array
{
$sourceCode = file_get_contents($file);
$classes = [];
$tokens = token_get_all($sourceCode, TOKEN_PARSE);
$tokenCount = count($tokens);
$namespace = '';
for ($i = 0; $i < $tokenCount; ++$i) {
if ($tokens[$i][0] === T_NAMESPACE) {
$namespace = '';
for ($j = $i + 1; $j < $tokenCount; ++$j) {
if ($tokens[$j] === '{' || $tokens[$j] === ';') {
break;
}
if ($tokens[$j][0] === T_STRING || $tokens[$j][0] === T_NAME_QUALIFIED) {
$namespace .= $tokens[$j][1] . '\\';
}
}
}
if ($tokens[$i][0] === T_CLASS) {
// class at the beginning of file
if (!isset($tokens[$i - 2])) {
$classes[] = $namespace . $tokens[$i + 2][1];
continue;
}
// new class
if ($tokens[$i - 2][0] === T_NEW) {
continue;
}
// :: class
if ($tokens[$i - 1][0] === T_WHITESPACE && $tokens[$i - 2][0] === T_DOUBLE_COLON) {
continue;
}
// ::class
if ($tokens[$i - 1][0] === T_DOUBLE_COLON) {
continue;
}
// class{
if (isset($tokens[$i + 1]) && ($tokens[$i + 1] === '{')) {
continue;
}
// class {
if (isset($tokens[$i + 2]) && $tokens[$i + 1][0] === T_WHITESPACE && $tokens[$i + 2] === '{') {
continue;
}
$classes[] = $namespace . $tokens[$i + 2][1];
}
}
$tokens = null;
gc_mem_caches();
return $classes;
}
/*
* Include in different scope to prevent included file from affecting $file variable
*/
private static function includeFile(string $file): void
{
include_once $file;
}
protected function stripComments(string $code): string
{
$code = preg_replace('#//.*?$#m', '', $code); // remove inline comments
return preg_replace('#/*\*.*?\*/#ms', '', $code); // remove block comment
}
protected function matchComments(string $code): string
{
$matches = [];
$comments = '';
$hasLineComment = preg_match_all('#//(.*?)$#m', $code, $matches);
if ($hasLineComment) {
foreach ($matches[1] as $line) {
$comments .= $line . "\n";
}
}
$hasBlockComment = preg_match('#/*\*(.*?)\*/#ms', $code, $matches);
if ($hasBlockComment) {
$comments .= $matches[1] . "\n";
}
return $comments;
}
}